Skip to content
This repository
Browse code

Run railtie and plugin initializers before app specific ones

  • Loading branch information...
commit 8fe66f1af01a744751344b83a9952e0cd91a1692 1 parent 19e7c52
Joshua Peek authored January 07, 2010
2  railties/lib/rails.rb
@@ -32,6 +32,8 @@
32 32
 RAILS_ENV = (ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development").dup unless defined?(RAILS_ENV)
33 33
 
34 34
 module Rails
  35
+  autoload :Bootstrap, 'rails/bootstrap'
  36
+
35 37
   # Needs to be duplicated from Active Support since its needed before Active
36 38
   # Support is available. Here both Options and Hash are namespaced to prevent
37 39
   # conflicts with other implementations AND with the classes residing in Active Support.
132  railties/lib/rails/application.rb
@@ -83,8 +83,9 @@ def load_tasks
83 83
     end
84 84
 
85 85
     def initializers
86  
-      initializers = super
  86
+      initializers = Bootstrap.new(self).initializers
87 87
       plugins.each { |p| initializers += p.initializers }
  88
+      initializers += super
88 89
       initializers
89 90
     end
90 91
 
@@ -103,135 +104,6 @@ def call(env)
103 104
       @app.call(env)
104 105
     end
105 106
 
106  
-    initializer :load_all_active_support do
107  
-      require "active_support/all" unless config.active_support.bare
108  
-    end
109  
-
110  
-    # Set the <tt>$LOAD_PATH</tt> based on the value of
111  
-    # Configuration#load_paths. Duplicates are removed.
112  
-    initializer :set_load_path do
113  
-      config.paths.add_to_load_path
114  
-      $LOAD_PATH.uniq!
115  
-    end
116  
-
117  
-    # Set the paths from which Rails will automatically load source files, and
118  
-    # the load_once paths.
119  
-    initializer :set_autoload_paths do
120  
-      require 'active_support/dependencies'
121  
-      ActiveSupport::Dependencies.load_paths = config.load_paths.uniq
122  
-      ActiveSupport::Dependencies.load_once_paths = config.load_once_paths.uniq
123  
-
124  
-      extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths
125  
-      unless extra.empty?
126  
-        abort <<-end_error
127  
-          load_once_paths must be a subset of the load_paths.
128  
-          Extra items in load_once_paths: #{extra * ','}
129  
-        end_error
130  
-      end
131  
-
132  
-      # Freeze the arrays so future modifications will fail rather than do nothing mysteriously
133  
-      config.load_once_paths.freeze
134  
-    end
135  
-
136  
-    # Create tmp directories
137  
-    initializer :ensure_tmp_directories_exist do
138  
-      %w(cache pids sessions sockets).each do |dir_to_make|
139  
-        FileUtils.mkdir_p(File.join(root, 'tmp', dir_to_make))
140  
-      end
141  
-    end
142  
-
143  
-    # Preload all frameworks specified by the Configuration#frameworks.
144  
-    # Used by Passenger to ensure everything's loaded before forking and
145  
-    # to avoid autoload race conditions in JRuby.
146  
-    initializer :preload_frameworks do
147  
-      ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
148  
-    end
149  
-
150  
-    initializer :initialize_cache do
151  
-      unless defined?(RAILS_CACHE)
152  
-        silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) }
153  
-
154  
-        if RAILS_CACHE.respond_to?(:middleware)
155  
-          # Insert middleware to setup and teardown local cache for each request
156  
-          config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware)
157  
-        end
158  
-      end
159  
-    end
160  
-
161  
-    initializer :initialize_logger do
162  
-      # if the environment has explicitly defined a logger, use it
163  
-      next if Rails.logger
164  
-
165  
-      unless logger = config.logger
166  
-        begin
167  
-          logger = ActiveSupport::BufferedLogger.new(config.log_path)
168  
-          logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
169  
-          if RAILS_ENV == "production"
170  
-            logger.auto_flushing = false
171  
-          end
172  
-        rescue StandardError => e
173  
-          logger = ActiveSupport::BufferedLogger.new(STDERR)
174  
-          logger.level = ActiveSupport::BufferedLogger::WARN
175  
-          logger.warn(
176  
-            "Rails Error: Unable to access log file. Please ensure that #{config.log_path} exists and is chmod 0666. " +
177  
-            "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
178  
-          )
179  
-        end
180  
-      end
181  
-
182  
-      # TODO: Why are we silencing warning here?
183  
-      silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
184  
-    end
185  
-
186  
-    # Sets the logger for Active Record, Action Controller, and Action Mailer
187  
-    # (but only for those frameworks that are to be loaded). If the framework's
188  
-    # logger is already set, it is not changed, otherwise it is set to use
189  
-    # RAILS_DEFAULT_LOGGER.
190  
-    initializer :initialize_framework_logging do
191  
-      ActiveSupport::Dependencies.logger ||= Rails.logger
192  
-      Rails.cache.logger ||= Rails.logger
193  
-    end
194  
-
195  
-    # Sets the dependency loading mechanism based on the value of
196  
-    # Configuration#cache_classes.
197  
-    initializer :initialize_dependency_mechanism do
198  
-      # TODO: Remove files from the $" and always use require
199  
-      ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
200  
-    end
201  
-
202  
-    # Loads support for "whiny nil" (noisy warnings when methods are invoked
203  
-    # on +nil+ values) if Configuration#whiny_nils is true.
204  
-    initializer :initialize_whiny_nils do
205  
-      require('active_support/whiny_nil') if config.whiny_nils
206  
-    end
207  
-
208  
-    # Sets the default value for Time.zone
209  
-    # If assigned value cannot be matched to a TimeZone, an exception will be raised.
210  
-    initializer :initialize_time_zone do
211  
-      require 'active_support/core_ext/time/zones'
212  
-      zone_default = Time.__send__(:get_zone, config.time_zone)
213  
-
214  
-      unless zone_default
215  
-        raise \
216  
-          'Value assigned to config.time_zone not recognized.' +
217  
-          'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
218  
-      end
219  
-
220  
-      Time.zone_default = zone_default
221  
-    end
222  
-
223  
-    # Set the i18n configuration from config.i18n but special-case for the load_path which should be
224  
-    # appended to what's already set instead of overwritten.
225  
-    initializer :initialize_i18n do
226  
-      config.i18n.each do |setting, value|
227  
-        if setting == :load_path
228  
-          I18n.load_path += value
229  
-        else
230  
-          I18n.send("#{setting}=", value)
231  
-        end
232  
-      end
233  
-    end
234  
-
235 107
     # # bail out if gems are missing - note that check_gem_dependencies will have
236 108
     # # already called abort() unless $gems_rake_task is set
237 109
     # return unless gems_dependencies_loaded
140  railties/lib/rails/bootstrap.rb
... ...
@@ -0,0 +1,140 @@
  1
+module Rails
  2
+  class Bootstrap #< Railtie
  3
+    include Initializable
  4
+
  5
+    def initialize(application)
  6
+      @application = application
  7
+    end
  8
+
  9
+    delegate :config, :root, :to => :'@application'
  10
+
  11
+    initializer :load_all_active_support do
  12
+      require "active_support/all" unless config.active_support.bare
  13
+    end
  14
+
  15
+    # Set the <tt>$LOAD_PATH</tt> based on the value of
  16
+    # Configuration#load_paths. Duplicates are removed.
  17
+    initializer :set_load_path do
  18
+      config.paths.add_to_load_path
  19
+      $LOAD_PATH.uniq!
  20
+    end
  21
+
  22
+    # Set the paths from which Rails will automatically load source files, and
  23
+    # the load_once paths.
  24
+    initializer :set_autoload_paths do
  25
+      require 'active_support/dependencies'
  26
+      ActiveSupport::Dependencies.load_paths = config.load_paths.uniq
  27
+      ActiveSupport::Dependencies.load_once_paths = config.load_once_paths.uniq
  28
+
  29
+      extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths
  30
+      unless extra.empty?
  31
+        abort <<-end_error
  32
+          load_once_paths must be a subset of the load_paths.
  33
+          Extra items in load_once_paths: #{extra * ','}
  34
+        end_error
  35
+      end
  36
+
  37
+      # Freeze the arrays so future modifications will fail rather than do nothing mysteriously
  38
+      config.load_once_paths.freeze
  39
+    end
  40
+
  41
+    # Create tmp directories
  42
+    initializer :ensure_tmp_directories_exist do
  43
+      %w(cache pids sessions sockets).each do |dir_to_make|
  44
+        FileUtils.mkdir_p(File.join(root, 'tmp', dir_to_make))
  45
+      end
  46
+    end
  47
+
  48
+    # Preload all frameworks specified by the Configuration#frameworks.
  49
+    # Used by Passenger to ensure everything's loaded before forking and
  50
+    # to avoid autoload race conditions in JRuby.
  51
+    initializer :preload_frameworks do
  52
+      ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
  53
+    end
  54
+
  55
+    initializer :initialize_cache do
  56
+      unless defined?(RAILS_CACHE)
  57
+        silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) }
  58
+
  59
+        if RAILS_CACHE.respond_to?(:middleware)
  60
+          # Insert middleware to setup and teardown local cache for each request
  61
+          config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware)
  62
+        end
  63
+      end
  64
+    end
  65
+
  66
+    initializer :initialize_logger do
  67
+      # if the environment has explicitly defined a logger, use it
  68
+      next if Rails.logger
  69
+
  70
+      unless logger = config.logger
  71
+        begin
  72
+          logger = ActiveSupport::BufferedLogger.new(config.log_path)
  73
+          logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
  74
+          if RAILS_ENV == "production"
  75
+            logger.auto_flushing = false
  76
+          end
  77
+        rescue StandardError => e
  78
+          logger = ActiveSupport::BufferedLogger.new(STDERR)
  79
+          logger.level = ActiveSupport::BufferedLogger::WARN
  80
+          logger.warn(
  81
+            "Rails Error: Unable to access log file. Please ensure that #{config.log_path} exists and is chmod 0666. " +
  82
+            "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
  83
+          )
  84
+        end
  85
+      end
  86
+
  87
+      # TODO: Why are we silencing warning here?
  88
+      silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
  89
+    end
  90
+
  91
+    # Sets the logger for Active Record, Action Controller, and Action Mailer
  92
+    # (but only for those frameworks that are to be loaded). If the framework's
  93
+    # logger is already set, it is not changed, otherwise it is set to use
  94
+    # RAILS_DEFAULT_LOGGER.
  95
+    initializer :initialize_framework_logging do
  96
+      ActiveSupport::Dependencies.logger ||= Rails.logger
  97
+      Rails.cache.logger ||= Rails.logger
  98
+    end
  99
+
  100
+    # Sets the dependency loading mechanism based on the value of
  101
+    # Configuration#cache_classes.
  102
+    initializer :initialize_dependency_mechanism do
  103
+      # TODO: Remove files from the $" and always use require
  104
+      ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
  105
+    end
  106
+
  107
+    # Loads support for "whiny nil" (noisy warnings when methods are invoked
  108
+    # on +nil+ values) if Configuration#whiny_nils is true.
  109
+    initializer :initialize_whiny_nils do
  110
+      require('active_support/whiny_nil') if config.whiny_nils
  111
+    end
  112
+
  113
+    # Sets the default value for Time.zone
  114
+    # If assigned value cannot be matched to a TimeZone, an exception will be raised.
  115
+    initializer :initialize_time_zone do
  116
+      require 'active_support/core_ext/time/zones'
  117
+      zone_default = Time.__send__(:get_zone, config.time_zone)
  118
+
  119
+      unless zone_default
  120
+        raise \
  121
+          'Value assigned to config.time_zone not recognized.' +
  122
+          'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
  123
+      end
  124
+
  125
+      Time.zone_default = zone_default
  126
+    end
  127
+
  128
+    # Set the i18n configuration from config.i18n but special-case for the load_path which should be
  129
+    # appended to what's already set instead of overwritten.
  130
+    initializer :initialize_i18n do
  131
+      config.i18n.each do |setting, value|
  132
+        if setting == :load_path
  133
+          I18n.load_path += value
  134
+        else
  135
+          I18n.send("#{setting}=", value)
  136
+        end
  137
+      end
  138
+    end
  139
+  end
  140
+end
11  railties/test/application/initializer_test.rb
@@ -83,6 +83,17 @@ module Zoo::ReptileHouse ; end
83 83
       assert_equal "congratulations", $test_after_initialize_block2
84 84
     end
85 85
 
  86
+    test "after_initialize runs after frameworks have been initialized" do
  87
+      $activerecord_configurations = nil
  88
+      add_to_config <<-RUBY
  89
+        config.after_initialize { $activerecord_configurations = ActiveRecord::Base.configurations }
  90
+      RUBY
  91
+
  92
+      require "#{app_path}/config/environment"
  93
+      assert $activerecord_configurations
  94
+      assert $activerecord_configurations['development']
  95
+    end
  96
+
86 97
     # i18n
87 98
     test "setting another default locale" do
88 99
       add_to_config <<-RUBY

0 notes on commit 8fe66f1

Please sign in to comment.
Something went wrong with that request. Please try again.