From 661298176c72eefc6d0f42b58210119e76962364 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Fri, 5 Feb 2016 22:48:48 +0100 Subject: [PATCH] Inject Rails' channel paths in engine. We were explicitly referencing Rails.root in ActionCable::Server::Configuration.initialize, thereby coupling ourselves to Rails. Instead add `app/channels` to Rails' app paths and assign the existent files to `channel_paths`. Users can still append to those load paths with `<<` and `push` in `config/application.rb`. This means we can remove the custom `Dir` lookup in `channel_paths` and the Rails and root definitions in the tests. --- actioncable/lib/action_cable/engine.rb | 2 ++ actioncable/lib/action_cable/server/configuration.rb | 11 ++--------- actioncable/test/client_test.rb | 10 +--------- actioncable/test/subscription_adapter/common.rb | 8 -------- railties/lib/rails/engine/configuration.rb | 1 + 5 files changed, 6 insertions(+), 26 deletions(-) diff --git a/actioncable/lib/action_cable/engine.rb b/actioncable/lib/action_cable/engine.rb index f5e233e091..acca3961d8 100644 --- a/actioncable/lib/action_cable/engine.rb +++ b/actioncable/lib/action_cable/engine.rb @@ -31,6 +31,8 @@ class Railtie < Rails::Engine # :nodoc: self.cable = Rails.application.config_for(config_path).with_indifferent_access end + self.channel_paths = Rails.application.paths['app/channels'].existent + options.each { |k,v| send("#{k}=", v) } end end diff --git a/actioncable/lib/action_cable/server/configuration.rb b/actioncable/lib/action_cable/server/configuration.rb index 9a248933c4..019a374734 100644 --- a/actioncable/lib/action_cable/server/configuration.rb +++ b/actioncable/lib/action_cable/server/configuration.rb @@ -5,27 +5,20 @@ module Server class Configuration attr_accessor :logger, :log_tags attr_accessor :connection_class, :worker_pool_size - attr_accessor :channel_load_paths attr_accessor :disable_request_forgery_protection, :allowed_request_origins attr_accessor :cable, :url + attr_accessor :channel_paths # :nodoc: + def initialize @log_tags = [] @connection_class = ApplicationCable::Connection @worker_pool_size = 100 - @channel_load_paths = [Rails.root.join('app/channels')] - @disable_request_forgery_protection = false end - def channel_paths - @channel_paths ||= channel_load_paths.flat_map do |path| - Dir["#{path}/**/*_channel.rb"] - end - end - def channel_class_names @channel_class_names ||= channel_paths.collect do |channel_path| Pathname.new(channel_path).basename.to_s.split('.').first.camelize diff --git a/actioncable/test/client_test.rb b/actioncable/test/client_test.rb index 4ade9832e0..f2b090cc52 100644 --- a/actioncable/test/client_test.rb +++ b/actioncable/test/client_test.rb @@ -16,12 +16,8 @@ def setup ::Object.const_set(:ApplicationCable, Module.new) ::ApplicationCable.const_set(:Connection, Class.new(ActionCable::Connection::Base)) - ::Object.const_set(:Rails, Module.new) - ::Rails.singleton_class.send(:define_method, :root) { Pathname.new(__dir__) } - ActionCable.instance_variable_set(:@server, nil) server = ActionCable.server - server.config = ActionCable::Server::Configuration.new inner_logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN } server.config.logger = ActionCable::Connection::TaggedLoggerProxy.new(inner_logger, tags: []) @@ -29,7 +25,7 @@ def setup # and now the "real" setup for our test: server.config.disable_request_forgery_protection = true - server.config.channel_load_paths = [File.expand_path('client', __dir__)] + server.config.channel_paths = [ File.expand_path('client/echo_channel.rb', __dir__) ] Thread.new { EventMachine.run } unless EventMachine.reactor_running? Thread.pass until EventMachine.reactor_running? @@ -45,10 +41,6 @@ def teardown ::Object.send(:remove_const, :ApplicationCable) rescue NameError end - begin - ::Object.send(:remove_const, :Rails) - rescue NameError - end end def with_puma_server(rack_app = ActionCable.server, port = 3099) diff --git a/actioncable/test/subscription_adapter/common.rb b/actioncable/test/subscription_adapter/common.rb index 361858784e..1b6f8c70bf 100644 --- a/actioncable/test/subscription_adapter/common.rb +++ b/actioncable/test/subscription_adapter/common.rb @@ -13,11 +13,7 @@ def setup ::Object.const_set(:ApplicationCable, Module.new) ::ApplicationCable.const_set(:Connection, Class.new(ActionCable::Connection::Base)) - ::Object.const_set(:Rails, Module.new) - ::Rails.singleton_class.send(:define_method, :root) { Pathname.new(__dir__) } - server = ActionCable::Server::Base.new - server.config = ActionCable::Server::Configuration.new inner_logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN } server.config.logger = ActionCable::Connection::TaggedLoggerProxy.new(inner_logger, tags: []) @@ -39,10 +35,6 @@ def teardown ::Object.send(:remove_const, :ApplicationCable) rescue NameError end - begin - ::Object.send(:remove_const, :Rails) - rescue NameError - end end diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index 8cadbc3ddd..294d07446f 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -39,6 +39,7 @@ def paths paths.add "app", eager_load: true, glob: "{*,*/concerns}" paths.add "app/assets", glob: "*" paths.add "app/controllers", eager_load: true + paths.add "app/channels", eager_load: true, glob: "**/*_channel.rb" paths.add "app/helpers", eager_load: true paths.add "app/models", eager_load: true paths.add "app/mailers", eager_load: true -- GitLab