From 96455349ffb26f437bbe6fdaa8119f47f3f25985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 15 Dec 2015 15:59:00 -0200 Subject: Configure the Action Cable's redis in the engine This will decouple Action Cable from Rails. --- actioncable/README.md | 2 +- actioncable/lib/action_cable/engine.rb | 5 +++++ .../lib/action_cable/server/configuration.rb | 23 +--------------------- 3 files changed, 7 insertions(+), 23 deletions(-) (limited to 'actioncable') diff --git a/actioncable/README.md b/actioncable/README.md index 6d4be32be9..da312b2264 100644 --- a/actioncable/README.md +++ b/actioncable/README.md @@ -322,7 +322,7 @@ This format allows you to specify one configuration per Rails environment. You c a Rails initializer with something like: ```ruby -ActionCable.server.config.redis_path = Rails.root('somewhere/else/cable.yml') +Rails.application.paths.add "config/redis/cable", with: "somewhere/else/cable.yml" ``` ### Allowed Request Origins diff --git a/actioncable/lib/action_cable/engine.rb b/actioncable/lib/action_cable/engine.rb index e395500eed..fbe8038a57 100644 --- a/actioncable/lib/action_cable/engine.rb +++ b/actioncable/lib/action_cable/engine.rb @@ -18,11 +18,16 @@ module ActionCable end initializer "action_cable.set_configs" do |app| + app.paths.add "config/redis/cable", with: "config/redis/cable.yml" + options = app.config.action_cable options.allowed_request_origins ||= "http://localhost:3000" if ::Rails.env.development? ActiveSupport.on_load(:action_cable) do + path = Pathname.new(paths["config/redis/cable"].existent.first) + self.redis = Rails.application.config_for(redis_path).with_indifferent_access + 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 f7fcee019b..add71a7f1e 100644 --- a/actioncable/lib/action_cable/server/configuration.rb +++ b/actioncable/lib/action_cable/server/configuration.rb @@ -7,7 +7,7 @@ module ActionCable class Configuration attr_accessor :logger, :log_tags attr_accessor :connection_class, :worker_pool_size - attr_accessor :redis_path, :channels_path + attr_accessor :redis, :channels_path attr_accessor :disable_request_forgery_protection, :allowed_request_origins attr_accessor :url @@ -18,7 +18,6 @@ module ActionCable @connection_class = ApplicationCable::Connection @worker_pool_size = 100 - @redis_path = Rails.root.join('config/redis/cable.yml') @channels_path = Rails.root.join('app/channels') @disable_request_forgery_protection = false @@ -41,26 +40,6 @@ module ActionCable Pathname.new(channel_path).basename.to_s.split('.').first.camelize end end - - def redis - @redis ||= config_for(redis_path).with_indifferent_access - end - - private - # FIXME: Extract this from Rails::Application in a way it can be used here. - def config_for(path) - if path.exist? - require "yaml" - require "erb" - (YAML.load(ERB.new(path.read).result) || {})[Rails.env] || {} - else - raise "Could not load configuration. No such file - #{path}" - end - rescue Psych::SyntaxError => e - raise "YAML syntax error occurred while parsing #{path}. " \ - "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ - "Error: #{e.message}" - end end end end -- cgit v1.2.3