提交 66129817 编写于 作者: K Kasper Timm Hansen

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.
上级 f611e59c
......@@ -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
......
......@@ -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
......
......@@ -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)
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册