提交 40b19e06 编写于 作者: P Piotr Sarnacki

Allow to change engine's loading priority with config.railties_order=

上级 8549f7a4
......@@ -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 +
......
......@@ -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
......@@ -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
......
......@@ -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 @@ 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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册