Skip to content

Commit

Permalink
Inject Rails' channel paths in engine.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kaspth committed Feb 14, 2016
1 parent f611e59 commit 6612981
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 26 deletions.
2 changes: 2 additions & 0 deletions actioncable/lib/action_cable/engine.rb
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions actioncable/lib/action_cable/server/configuration.rb
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions actioncable/test/client_test.rb
Expand Up @@ -16,20 +16,16 @@ 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: [])

server.config.cable = { adapter: 'async' }.with_indifferent_access

# 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?
Expand All @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions actioncable/test/subscription_adapter/common.rb
Expand Up @@ -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: [])

Expand All @@ -39,10 +35,6 @@ def teardown
::Object.send(:remove_const, :ApplicationCable)
rescue NameError
end
begin
::Object.send(:remove_const, :Rails)
rescue NameError
end
end


Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/engine/configuration.rb
Expand Up @@ -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
Expand Down

0 comments on commit 6612981

Please sign in to comment.