From a44033e623f15524ce87235c79c9e547a54c8e69 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 19 Jun 2015 22:29:43 +0200 Subject: Use a perform_action router to handle incoming data Similar to a controller in ActionPack. --- lib/action_cable/channel/base.rb | 12 ++++++++---- lib/action_cable/connection/base.rb | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index cc9580ec52..c18593bf6f 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -35,12 +35,16 @@ module ActionCable run_subscribe_callbacks end - def receive_data(data) + def perform_action(data) if authorized? - if respond_to?(:receive) - receive(data) + action = (data['action'].presence || :receive).to_sym + signature = "#{self.class.name}##{action}: #{data}" + + if self.class.instance_methods(false).include?(action) + logger.info "Processing #{signature}" + public_send action, data else - logger.error "#{self.class.name} received data (#{data}) but #{self.class.name}#receive callback is not defined" + logger.error "Failed to process #{signature}" end else unauthorized diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index 5c2ee14258..e5ed07b5cc 100644 --- a/lib/action_cable/connection/base.rb +++ b/lib/action_cable/connection/base.rb @@ -150,7 +150,7 @@ module ActionCable def process_message(message) if @subscriptions[message['identifier']] - @subscriptions[message['identifier']].receive_data(ActiveSupport::JSON.decode message['data']) + @subscriptions[message['identifier']].perform_action(ActiveSupport::JSON.decode message['data']) else raise "Unable to process message because no subscription was found (#{message.inspect})" end -- cgit v1.2.3