diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/action_cable/channel/base.rb | 13 | ||||
-rw-r--r-- | lib/action_cable/connection/identification.rb | 3 |
2 files changed, 16 insertions, 0 deletions
diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 554aca7ffb..87ae3a1211 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -61,6 +61,9 @@ module ActionCable # In this example, subscribed/unsubscribed are not callable methods, as they were already declared in ActionCable::Channel::Base, but #appear/away # are. #generate_connection_token is also not callable as its a private method. You'll see that appear accepts a data parameter, which it then # uses as part of its model call. #away does not, it's simply a trigger action. + # + # Also note that in this example, current_user is available because it was marked as an identifying attribute on the connection. + # All such identifiers will automatically create a delegation method of the same name on the channel instance. class Base include Callbacks include PeriodicTimers @@ -77,6 +80,7 @@ module ActionCable @identifier = identifier @params = params + delegate_connection_identifiers subscribe_to_channel end @@ -123,6 +127,15 @@ module ActionCable private + def delegate_connection_identifiers + connection.identifiers.each do |identifier| + define_singleton_method(identifier) do + connection.send(identifier) + end + end + end + + def subscribe_to_channel logger.info "#{self.class.name} subscribing" run_subscribe_callbacks diff --git a/lib/action_cable/connection/identification.rb b/lib/action_cable/connection/identification.rb index 3ea3b77e56..113e41ca3f 100644 --- a/lib/action_cable/connection/identification.rb +++ b/lib/action_cable/connection/identification.rb @@ -11,6 +11,9 @@ module ActionCable class_methods do # Mark a key as being a connection identifier index that can then used to find the specific connection again later. # Common identifiers are current_user and current_account, but could be anything really. + # + # Note that anything marked as an identifier will automatically create a delegate by the same name on any + # channel instances created off the connection. def identified_by(*identifiers) Array(identifiers).each { |identifier| attr_accessor identifier } self.identifiers += identifiers |