aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/channel/base.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-07-11 11:25:11 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-07-11 11:25:11 +0200
commitcbc73069788abc62fca5cf33ae5a0a4f63937fb1 (patch)
tree158d9f95384df1d1651d407a7319a7f175e904e9 /lib/action_cable/channel/base.rb
parent763d499d9e7e5502c945e3bacfb02f573e9c2fdd (diff)
downloadrails-cbc73069788abc62fca5cf33ae5a0a4f63937fb1.tar.gz
rails-cbc73069788abc62fca5cf33ae5a0a4f63937fb1.tar.bz2
rails-cbc73069788abc62fca5cf33ae5a0a4f63937fb1.zip
Add automatic delegations from channel to connection identifiers
Diffstat (limited to 'lib/action_cable/channel/base.rb')
-rw-r--r--lib/action_cable/channel/base.rb13
1 files changed, 13 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