Skip to content

Commit

Permalink
Support dependency injection for routes engine
Browse files Browse the repository at this point in the history
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](#138)
  • Loading branch information
Damian Galarza and Tute Costa authored and Damian Galarza committed Jun 12, 2014
1 parent 88906ee commit 3f80403
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 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
Expand Down
1 change: 1 addition & 0 deletions lib/high_voltage/configuration.rb
Expand Up @@ -9,6 +9,7 @@ module Configuration
:content_path,
:home_page,
:layout,
:parent_engine,
:route_drawer,
:routes,
)
Expand Down
4 changes: 4 additions & 0 deletions 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'

Expand Down
8 changes: 8 additions & 0 deletions spec/high_voltage/configuration_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 3f80403

Please sign in to comment.