aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/initializable.rb29
-rw-r--r--railties/lib/rails/plugin.rb2
-rw-r--r--railties/test/initializable_test.rb43
-rw-r--r--railties/test/railties/plugin_test.rb2
4 files changed, 55 insertions, 21 deletions
diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb
index d91f67823f..9a82e051e7 100644
--- a/railties/lib/rails/initializable.rb
+++ b/railties/lib/rails/initializable.rb
@@ -1,3 +1,5 @@
+require 'tsort'
+
module Rails
module Initializable
def self.included(base)
@@ -30,29 +32,21 @@ module Rails
end
class Collection < Array
+ include TSort
+
+ alias :tsort_each_node :each
+ def tsort_each_child(initializer, &block)
+ select { |i| i.before == initializer.name || i.name == initializer.after }.each(&block)
+ end
+
def initialize(initializers = [])
- super()
- initializers.each do |initializer|
- if initializer.before
- index = index_for(initializer.before)
- elsif initializer.after
- index = index_for(initializer.after)
- index += 1 if index
- else
- index = length
- end
- insert(index || -1, initializer)
- end
+ super(initializers)
+ replace(tsort)
end
def +(other)
Collection.new(to_a + other.to_a)
end
-
- def index_for(name)
- initializer = find { |i| i.name == name }
- initializer && index(initializer)
- end
end
def run_initializers(*args)
@@ -87,6 +81,7 @@ module Rails
def initializer(name, opts = {}, &blk)
raise ArgumentError, "A block must be passed when defining an initializer" unless blk
+ opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] }
initializers << Initializer.new(name, nil, opts, &blk)
end
diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb
index 881c97f02d..64d03e7599 100644
--- a/railties/lib/rails/plugin.rb
+++ b/railties/lib/rails/plugin.rb
@@ -43,7 +43,7 @@ module Rails
@config ||= Engine::Configuration.new
end
- initializer :load_init_rb do |app|
+ initializer :load_init_rb, :before => :load_application_initializers do |app|
file = Dir["#{root}/{rails/init,init}.rb"].first
config = app.config
eval(File.read(file), binding, file) if file && File.file?(file)
diff --git a/railties/test/initializable_test.rb b/railties/test/initializable_test.rb
index 0c7378cb64..6a7e4dcc25 100644
--- a/railties/test/initializable_test.rb
+++ b/railties/test/initializable_test.rb
@@ -50,7 +50,7 @@ module InitializableTests
$arr << 3
end
- initializer :four, :after => :one do
+ initializer :four, :after => :one, :before => :two do
$arr << 4
end
end
@@ -97,7 +97,7 @@ module InitializableTests
$arr << 3
end
- initializer :terminate, :after => :first do
+ initializer :terminate, :after => :first, :before => :startup do
$arr << two
end
@@ -121,6 +121,39 @@ module InitializableTests
end
end
+ module Interdependent
+ class PluginA
+ include Rails::Initializable
+
+ initializer "plugin_a.startup" do
+ $arr << 1
+ end
+
+ initializer "plugin_a.terminate" do
+ $arr << 4
+ end
+ end
+
+ class PluginB
+ include Rails::Initializable
+
+ initializer "plugin_b.startup", :after => "plugin_a.startup" do
+ $arr << 2
+ end
+
+ initializer "plugin_b.terminate", :before => "plugin_a.terminate" do
+ $arr << 3
+ end
+ end
+
+ class Application
+ include Rails::Initializable
+ def self.initializers
+ PluginB.initializers + PluginA.initializers
+ end
+ end
+ end
+
class Basic < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
@@ -174,6 +207,12 @@ module InitializableTests
Child.run_initializers
assert_equal [5, 3, 1, 4, 2], $arr
end
+
+ test "handles dependencies introduced before all initializers are loaded" do
+ $arr = []
+ Interdependent::Application.run_initializers
+ assert_equal [1, 2, 3, 4], $arr
+ end
end
class InstanceTest < ActiveSupport::TestCase
diff --git a/railties/test/railties/plugin_test.rb b/railties/test/railties/plugin_test.rb
index 997b692e49..09b859dcdd 100644
--- a/railties/test/railties/plugin_test.rb
+++ b/railties/test/railties/plugin_test.rb
@@ -47,7 +47,7 @@ module RailtiesTest
assert_equal :debug, LEVEL
end
- test "plugin_init_is_ran_before_application_ones" do
+ test "plugin_init_is_run_before_application_ones" do
plugin "foo", "$foo = true" do |plugin|
plugin.write "lib/foo.rb", "module Foo; end"
end