aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/initializable.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-31 14:32:26 +0000
committerPratik Naik <pratiknaik@gmail.com>2010-01-31 14:32:26 +0000
commited60021f39f7913537dcad181e4061e423dc6c3d (patch)
tree86532c43aabe9125422f84b48a899f2672900bf5 /railties/lib/rails/initializable.rb
parentc6af337d2d6c86792cdc8132224ebe9294d35774 (diff)
parentb3a028259f373fd58fea2171a1e9e8b2fe3e253a (diff)
downloadrails-ed60021f39f7913537dcad181e4061e423dc6c3d.tar.gz
rails-ed60021f39f7913537dcad181e4061e423dc6c3d.tar.bz2
rails-ed60021f39f7913537dcad181e4061e423dc6c3d.zip
Merge remote branch 'mainstream/master'
Conflicts: activemodel/lib/active_model/state_machine.rb
Diffstat (limited to 'railties/lib/rails/initializable.rb')
-rw-r--r--railties/lib/rails/initializable.rb29
1 files changed, 12 insertions, 17 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