mirror of https://github.com/rails/rails
Allow to change engine's loading priority with config.railties_order=
This commit is contained in:
parent
8549f7a4f0
commit
40b19e0635
|
@ -127,6 +127,28 @@ module Rails
|
|||
})
|
||||
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 +
|
||||
|
|
|
@ -11,7 +11,7 @@ module Rails
|
|||
: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
|
||||
:time_zone, :whiny_nils, :railties_order
|
||||
|
||||
attr_writer :log_level
|
||||
attr_reader :encoding
|
||||
|
@ -35,6 +35,7 @@ module Rails
|
|||
@middleware = app_middleware
|
||||
@generators = app_generators
|
||||
@cache_store = [ :file_store, "#{root}/tmp/cache/" ]
|
||||
@railties_order = [:all]
|
||||
|
||||
@assets = ActiveSupport::OrderedOptions.new
|
||||
@assets.enabled = false
|
||||
|
|
|
@ -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"
|
||||
|
@ -480,10 +491,19 @@ module Rails
|
|||
@routes
|
||||
end
|
||||
|
||||
def ordered_railties
|
||||
railties.all + [self]
|
||||
end
|
||||
|
||||
def initializers
|
||||
initializers = []
|
||||
railties.all { |r| initializers += r.initializers }
|
||||
ordered_railties.each do |r|
|
||||
if r == self
|
||||
initializers += super
|
||||
else
|
||||
initializers += r.initializers
|
||||
end
|
||||
end
|
||||
initializers
|
||||
end
|
||||
|
||||
|
|
|
@ -667,6 +667,132 @@ module RailtiesTest
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue