From 2f57353023705917369ad417080757c0f2b71885 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Tue, 28 Dec 2010 13:23:50 +1000 Subject: Init guide: finish covering initializers for i18n_railtie, moving on to covering after_initialize --- railties/guides/source/initialization.textile | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index 72f1ca8f5d..5013d9d7ec 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -809,6 +809,47 @@ The +Collection+ class in +railties/lib/rails/initializable.rb+ inherits from +A The +initializers_chain+ method referenced in the +initializers_for+ method is defined like this: + + def initializers_chain + initializers = Collection.new + ancestors.reverse_each do | klass | + next unless klass.respond_to?(:initializers) + initializers = initializers + klass.initializers + end + initializers + end + + +This method collects the initializers from the ancestors of this class and adds them to a new +Collection+ object using the + method which is defined like this for the Collection class: + + + def +(other) + Collection.new(to_a + other.to_a) + end + + +So this + method is overriden to return a new collection comprising of the existing collection as an array and then using the Array#+ method combines these two collections, returning a "super" +Collection+ object. In this case, the only initializer that's going to be in this new +Collection+ object is the +i18n.callbacks+ initializer. + +The next method to be called after this +initializer+ method is the +after_initialize+ method on the +config+ object, which is defined like this: + + + def after_initialize(&block) + ActiveSupport.on_load(:after_initialize, :yield => true, &block) + end + + +The +on_load+ method here is provided by the +active_support/lazy_load_hooks+ file which was required earlier and is defined like this: + + + def self.on_load(name, options = {}, &block) + if base = @loaded[name] + execute_hook(base, options, block) + else + @load_hooks[name] << [block, options] + end + end + + **** REVIEW IS HERE **** This defines two methods on the module itself by using the familiar +class << self+ syntax. This allows you to call them as if they were class methods: +ActiveSupport.on_load_all+ and +ActiveSupport.load_all!+ respectively. The first method simply adds loading hooks to save them up for loading later on when +load_all!+ is called. By +call+'ing the block, the classes will be loaded. (NOTE: kind of guessing, I feel 55% about this). -- cgit v1.2.3