From 3f8040332da4d87b5f1460cc91712bf11a2f3ff9 Mon Sep 17 00:00:00 2001 From: Damian Galarza and Tute Costa Date: Fri, 9 May 2014 15:39:28 -0400 Subject: [PATCH] Support dependency injection for routes engine Allow a user to define which Rails engine to define routes on when using multiple engines within a Rails application. This will ensure that path helpers will correctly be available in the desired engine. This will default to the default main Rails application if another engine is not specified. Resolves issue [#138](https://github.com/thoughtbot/high_voltage/issues/138) --- config/routes.rb | 2 +- lib/high_voltage/configuration.rb | 1 + lib/high_voltage/engine.rb | 4 ++++ spec/high_voltage/configuration_spec.rb | 8 ++++++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 3ad8c87..33888a3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ -Rails.application.routes.draw do +HighVoltage.parent_engine.routes.draw do if HighVoltage.home_page get "/#{HighVoltage.home_page}", to: redirect('/') root to: 'high_voltage/pages#show', id: HighVoltage.home_page diff --git a/lib/high_voltage/configuration.rb b/lib/high_voltage/configuration.rb index 9c6328f..25821f6 100644 --- a/lib/high_voltage/configuration.rb +++ b/lib/high_voltage/configuration.rb @@ -9,6 +9,7 @@ module Configuration :content_path, :home_page, :layout, + :parent_engine, :route_drawer, :routes, ) diff --git a/lib/high_voltage/engine.rb b/lib/high_voltage/engine.rb index 5633bfb..2075802 100644 --- a/lib/high_voltage/engine.rb +++ b/lib/high_voltage/engine.rb @@ -1,5 +1,9 @@ module HighVoltage class Engine < Rails::Engine + initializer 'Set up default parent engine' do |app| + HighVoltage.parent_engine ||= Rails.application + end + initializer 'Require concerns path' do |app| concerns_path = 'app/controllers/concerns' diff --git a/spec/high_voltage/configuration_spec.rb b/spec/high_voltage/configuration_spec.rb index c5e68ca..cd0cbe9 100644 --- a/spec/high_voltage/configuration_spec.rb +++ b/spec/high_voltage/configuration_spec.rb @@ -12,17 +12,25 @@ config.content_path = config_value config.layout = config_value config.page_caching = config_value + config.parent_engine = config_value config.route_drawer = config_value config.routes = config_value end end end + after(:each) do + HighVoltage.configure do |config| + config.parent_engine = Rails.application + end + end + it { expect(HighVoltage.action_caching).to eq config_value } it { expect(HighVoltage.action_caching_layout).to eq config_value } it { expect(HighVoltage.content_path).to eq config_value } it { expect(HighVoltage.layout).to eq config_value } it { expect(HighVoltage.page_caching).to eq config_value } + it { expect(HighVoltage.parent_engine).to eq config_value } it { expect(HighVoltage.route_drawer).to eq config_value } it { expect(HighVoltage.routes).to eq config_value } end