Skip to content

Commit

Permalink
Allow to change engine's loading priority with config.railties_order=
Browse files Browse the repository at this point in the history
  • Loading branch information
drogus committed Nov 23, 2011
1 parent 8549f7a commit 40b19e0
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 4 deletions.
22 changes: 22 additions & 0 deletions railties/lib/rails/application.rb
Expand Up @@ -127,6 +127,28 @@ def env_config
})
end

def ordered_railties
@ordered_railties ||= begin
order = config.railties_order.map do |railtie|
if railtie == :main_app
self
elsif railtie.respond_to?(:instance)
railtie.instance
else
railtie
end
end

all = (railties.all - order)
all.push(self) unless all.include?(self)
order.push(:all) unless order.include?(:all)

index = order.index(:all)
order[index] = all
order.reverse.flatten
end
end

def initializers
Bootstrap.initializers_for(self) +
super +
Expand Down
5 changes: 3 additions & 2 deletions railties/lib/rails/application/configuration.rb
Expand Up @@ -10,8 +10,8 @@ class Configuration < ::Rails::Engine::Configuration
:dependency_loading, :filter_parameters,
:force_ssl, :helpers_paths, :logger, :log_tags, :preload_frameworks,
:reload_plugins, :secret_token, :serve_static_assets,
:ssl_options, :static_cache_control, :session_options,
:time_zone, :whiny_nils
:ssl_options, :static_cache_control, :session_options,
:time_zone, :whiny_nils, :railties_order

attr_writer :log_level
attr_reader :encoding
Expand All @@ -35,6 +35,7 @@ def initialize(*)
@middleware = app_middleware
@generators = app_generators
@cache_store = [ :file_store, "#{root}/tmp/cache/" ]
@railties_order = [:all]

@assets = ActiveSupport::OrderedOptions.new
@assets.enabled = false
Expand Down
24 changes: 22 additions & 2 deletions railties/lib/rails/engine.rb
Expand Up @@ -330,6 +330,17 @@ module Rails
#
# MyEngine::Engine.load_seed
#
# == Loading priority
#
# In order to change engine's priority you can use config.railties_order in main application.
# It will affect the priority of loading views, helpers, assets and all the other files
# related to engine or application.
#
# Example:
#
# # load Blog::Engine with highest priority, followed by application and other railties
# config.railties_order = [Blog::Engine, :main_app, :all]
#
class Engine < Railtie
autoload :Configuration, "rails/engine/configuration"
autoload :Railties, "rails/engine/railties"
Expand Down Expand Up @@ -480,10 +491,19 @@ def routes
@routes
end

def ordered_railties
railties.all + [self]
end

def initializers
initializers = []
railties.all { |r| initializers += r.initializers }
initializers += super
ordered_railties.each do |r|
if r == self
initializers += super
else
initializers += r.initializers
end
end
initializers
end

Expand Down
126 changes: 126 additions & 0 deletions railties/test/railties/engine_test.rb
Expand Up @@ -667,6 +667,132 @@ def baz
assert_equal expected, methods
end

test "setting priority for engines with config.railties_order" do
@blog = engine "blog" do |plugin|
plugin.write "lib/blog.rb", <<-RUBY
module Blog
class Engine < ::Rails::Engine
end
end
RUBY
end

@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
isolate_namespace Bukkits
end
end
RUBY

controller "main", <<-RUBY
class MainController < ActionController::Base
def foo
render :inline => '<%= render :partial => "shared/foo" %>'
end
def bar
render :inline => '<%= render :partial => "shared/bar" %>'
end
end
RUBY

app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
match "/foo" => "main#foo"
match "/bar" => "main#bar"
end
RUBY

@plugin.write "app/views/shared/_foo.html.erb", <<-RUBY
Bukkit's foo partial
RUBY

app_file "app/views/shared/_foo.html.erb", <<-RUBY
App's foo partial
RUBY

@blog.write "app/views/shared/_bar.html.erb", <<-RUBY
Blog's bar partial
RUBY

app_file "app/views/shared/_bar.html.erb", <<-RUBY
App's bar partial
RUBY

@plugin.write "app/assets/javascripts/foo.js", <<-RUBY
// Bukkit's foo js
RUBY

app_file "app/assets/javascripts/foo.js", <<-RUBY
// App's foo js
RUBY

@blog.write "app/assets/javascripts/bar.js", <<-RUBY
// Blog's bar js
RUBY

app_file "app/assets/javascripts/bar.js", <<-RUBY
// App's bar js
RUBY

add_to_config("config.railties_order = [:all, :main_app, Blog::Engine]")

boot_rails
require "#{rails_root}/config/environment"

get("/foo")
assert_equal "Bukkit's foo partial", last_response.body.strip

get("/bar")
assert_equal "App's bar partial", last_response.body.strip

get("/assets/foo.js")
assert_equal "// Bukkit's foo js\n;", last_response.body.strip

get("/assets/bar.js")
assert_equal "// App's bar js\n;", last_response.body.strip
end

test "railties_order adds :all with lowest priority if not given" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
end
end
RUBY

controller "main", <<-RUBY
class MainController < ActionController::Base
def foo
render :inline => '<%= render :partial => "shared/foo" %>'
end
end
RUBY

app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
match "/foo" => "main#foo"
end
RUBY

@plugin.write "app/views/shared/_foo.html.erb", <<-RUBY
Bukkit's foo partial
RUBY

app_file "app/views/shared/_foo.html.erb", <<-RUBY
App's foo partial
RUBY

add_to_config("config.railties_order = [Bukkits::Engine]")

boot_rails
require "#{rails_root}/config/environment"

get("/foo")
assert_equal "Bukkit's foo partial", last_response.body.strip
end

private
def app
Rails.application
Expand Down

0 comments on commit 40b19e0

Please sign in to comment.