Skip to content
Newer
Older
100644 432 lines (370 sloc) 15.6 KB
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 23, 2009
1 require 'active_support/core_ext/module/delegation'
2
accd9b4 @josh Restore "Start Rails::Application object
josh authored Sep 25, 2009
3 module Rails
4 class Application
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 2, 2009
5 include Initializable
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored Oct 7, 2009
6
e4d7e50 Moving more initializers into the application object
Carl Lerche authored Oct 8, 2009
7 class << self
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
8 attr_writer :config
9 alias configure class_eval
10 delegate :initialize!, :load_tasks, :to => :instance
d859402 @josh Use Rails.initialize! where we just want to run the initializers and …
josh authored Oct 16, 2009
11
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
12 private :new
13 def instance
14 @instance ||= new
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
15 end
16
e4d7e50 Moving more initializers into the application object
Carl Lerche authored Oct 8, 2009
17 def config
1b27f5c Add the ability for plugins to set default configuration settings.
Carl Lerche authored Dec 15, 2009
18 @config ||= begin
19 config = Configuration.new
20 Plugin.plugins.each { |p| config.merge(p.config) }
21 config
22 end
e4d7e50 Moving more initializers into the application object
Carl Lerche authored Oct 8, 2009
23 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored Sep 28, 2009
24
426348b @josh Update routes.rb template to use App name
josh authored Dec 21, 2009
25 def routes
26 ActionController::Routing::Routes
27 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
28 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored Sep 29, 2009
29
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
30 delegate :config, :routes, :to => :'self.class'
31 delegate :root, :middleware, :to => :config
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
32 attr_reader :route_configuration_files
33
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
34 def initialize
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
35 require_environment
530b8ff Have script/* and Rakefile use the application object
Carlhuda authored Nov 24, 2009
36 Rails.application ||= self
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
37 @route_configuration_files = []
530b8ff Have script/* and Rakefile use the application object
Carlhuda authored Nov 24, 2009
38 end
39
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
40 def initialize!
41 run_initializers(self)
42 self
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
43 end
44
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
45 def require_environment
46 require config.environment_path
47 rescue LoadError
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
48 end
49
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
50 def routes_changed_at
51 routes_changed_at = nil
52
53 route_configuration_files.each do |config|
54 config_changed_at = File.stat(config).mtime
55
56 if routes_changed_at.nil? || config_changed_at > routes_changed_at
57 routes_changed_at = config_changed_at
58 end
59 end
60
61 routes_changed_at
62 end
63
64 def reload_routes!
65 routes.disable_clear_and_finalize = true
66
67 routes.clear!
68 route_configuration_files.each { |config| load(config) }
69 routes.finalize!
70
71 nil
72 ensure
73 routes.disable_clear_and_finalize = false
74 end
75
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
76 def load_tasks
77 require "rails/tasks"
78 Dir["#{root}/vendor/plugins/*/**/tasks/**/*.rake"].sort.each { |ext| load ext }
79 Dir["#{root}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
80 task :environment do
81 $rails_rake_task = true
82 initialize!
83 end
84 end
85
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored Nov 6, 2009
86 def initializers
87 initializers = super
88 plugins.each { |p| initializers += p.initializers }
89 initializers
90 end
91
92 def plugins
93 @plugins ||= begin
1b27f5c Add the ability for plugins to set default configuration settings.
Carl Lerche authored Dec 16, 2009
94 plugin_names = config.plugins || [:all]
95 Plugin.plugins.select { |p| plugin_names.include?(p.plugin_name) } +
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored Nov 7, 2009
96 Plugin::Vendored.all(config.plugins || [:all], config.paths.vendor.plugins)
97 end
98 end
99
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
100 def call(env)
101 @app ||= middleware.build(routes)
102 @app.call(env)
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored Oct 7, 2009
103 end
104
105 # Set the <tt>$LOAD_PATH</tt> based on the value of
106 # Configuration#load_paths. Duplicates are removed.
107 initializer :set_load_path do
108 config.paths.add_to_load_path
109 $LOAD_PATH.uniq!
110 end
111
e4d7e50 Moving more initializers into the application object
Carl Lerche authored Oct 8, 2009
112 # Requires all frameworks specified by the Configuration#frameworks
113 # list. By default, all frameworks (Active Record, Active Support,
114 # Action Pack, Action Mailer, and Active Resource) are loaded.
115 initializer :require_frameworks do
3903499 Add support for bare ActiveSupport via config.active_support.bare
Carlhuda authored Dec 2, 2009
116 require 'active_support/all' unless config.active_support.bare
117 config.frameworks.each { |framework| require(framework.to_s) }
e4d7e50 Moving more initializers into the application object
Carl Lerche authored Oct 8, 2009
118 end
119
120 # Set the paths from which Rails will automatically load source files, and
121 # the load_once paths.
122 initializer :set_autoload_paths do
123 require 'active_support/dependencies'
124 ActiveSupport::Dependencies.load_paths = config.load_paths.uniq
125 ActiveSupport::Dependencies.load_once_paths = config.load_once_paths.uniq
126
127 extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths
128 unless extra.empty?
129 abort <<-end_error
130 load_once_paths must be a subset of the load_paths.
131 Extra items in load_once_paths: #{extra * ','}
132 end_error
133 end
134
135 # Freeze the arrays so future modifications will fail rather than do nothing mysteriously
136 config.load_once_paths.freeze
137 end
138
feb04bf Move the ensure_tmp_directories_exist initializer to the application …
Carl Lerche authored Oct 8, 2009
139 # Create tmp directories
140 initializer :ensure_tmp_directories_exist do
141 %w(cache pids sessions sockets).each do |dir_to_make|
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
142 FileUtils.mkdir_p(File.join(root, 'tmp', dir_to_make))
ed77e84 Ported over more initializers
Carl Lerche authored Oct 8, 2009
143 end
144 end
145
146 # Preload all frameworks specified by the Configuration#frameworks.
147 # Used by Passenger to ensure everything's loaded before forking and
148 # to avoid autoload race conditions in JRuby.
149 initializer :preload_frameworks do
150 if config.preload_frameworks
151 config.frameworks.each do |framework|
152 # String#classify and #constantize aren't available yet.
153 toplevel = Object.const_get(framework.to_s.gsub(/(?:^|_)(.)/) { $1.upcase })
154 toplevel.load_all! if toplevel.respond_to?(:load_all!)
155 end
feb04bf Move the ensure_tmp_directories_exist initializer to the application …
Carl Lerche authored Oct 8, 2009
156 end
157 end
5c42157 Even more initializers ported over
Carl Lerche authored Oct 8, 2009
158
159 # This initialization routine does nothing unless <tt>:active_record</tt>
160 # is one of the frameworks to load (Configuration#frameworks). If it is,
161 # this sets the database configuration from Configuration#database_configuration
162 # and then establishes the connection.
163 initializer :initialize_database do
164 if config.frameworks.include?(:active_record)
165 ActiveRecord::Base.configurations = config.database_configuration
166 ActiveRecord::Base.establish_connection
167 end
168 end
169
170 # Include middleware to serve up static assets
171 initializer :initialize_static_server do
172 if config.frameworks.include?(:action_controller) && config.serve_static_assets
173 config.middleware.use(ActionDispatch::Static, Rails.public_path)
174 end
175 end
176
177 initializer :initialize_middleware_stack do
178 if config.frameworks.include?(:action_controller)
b9d4ceb @josh Need to lazy realize middleware options so they can be modified in
josh authored Dec 16, 2009
179 config.middleware.use(::Rack::Lock, :if => lambda { ActionController::Base.allow_concurrency })
2d0c703 @josh Use Rack::Runtime middleware so the reported time includes the entire…
josh authored Dec 22, 2009
180 config.middleware.use(::Rack::Runtime)
b9d4ceb @josh Need to lazy realize middleware options so they can be modified in
josh authored Dec 16, 2009
181 config.middleware.use(ActionDispatch::ShowExceptions, lambda { ActionController::Base.consider_all_requests_local })
182 config.middleware.use(ActionDispatch::Callbacks, lambda { ActionController::Dispatcher.prepare_each_request })
5c42157 Even more initializers ported over
Carl Lerche authored Oct 8, 2009
183 config.middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
184 config.middleware.use(ActionDispatch::ParamsParser)
185 config.middleware.use(::Rack::MethodOverride)
186 config.middleware.use(::Rack::Head)
187 config.middleware.use(ActionDispatch::StringCoercion)
188 end
189 end
190
191 initializer :initialize_cache do
192 unless defined?(RAILS_CACHE)
193 silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) }
194
195 if RAILS_CACHE.respond_to?(:middleware)
196 # Insert middleware to setup and teardown local cache for each request
197 config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware)
198 end
199 end
200 end
201
202 initializer :initialize_framework_caches do
203 if config.frameworks.include?(:action_controller)
204 ActionController::Base.cache_store ||= RAILS_CACHE
205 end
206 end
207
208 initializer :initialize_logger do
209 # if the environment has explicitly defined a logger, use it
210 next if Rails.logger
211
212 unless logger = config.logger
213 begin
214 logger = ActiveSupport::BufferedLogger.new(config.log_path)
215 logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
216 if RAILS_ENV == "production"
217 logger.auto_flushing = false
218 end
219 rescue StandardError => e
220 logger = ActiveSupport::BufferedLogger.new(STDERR)
221 logger.level = ActiveSupport::BufferedLogger::WARN
222 logger.warn(
223 "Rails Error: Unable to access log file. Please ensure that #{config.log_path} exists and is chmod 0666. " +
224 "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
225 )
226 end
227 end
228
229 # TODO: Why are we silencing warning here?
230 silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
231 end
232
233 # Sets the logger for Active Record, Action Controller, and Action Mailer
234 # (but only for those frameworks that are to be loaded). If the framework's
235 # logger is already set, it is not changed, otherwise it is set to use
236 # RAILS_DEFAULT_LOGGER.
237 initializer :initialize_framework_logging do
238 for framework in ([ :active_record, :action_controller, :action_mailer ] & config.frameworks)
239 framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
240 end
241
242 ActiveSupport::Dependencies.logger ||= Rails.logger
243 Rails.cache.logger ||= Rails.logger
244 end
245
246 # Sets the dependency loading mechanism based on the value of
247 # Configuration#cache_classes.
248 initializer :initialize_dependency_mechanism do
249 # TODO: Remove files from the $" and always use require
250 ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
251 end
252
253 # Loads support for "whiny nil" (noisy warnings when methods are invoked
254 # on +nil+ values) if Configuration#whiny_nils is true.
255 initializer :initialize_whiny_nils do
256 require('active_support/whiny_nil') if config.whiny_nils
257 end
258
259 # Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
260 # If assigned value cannot be matched to a TimeZone, an exception will be raised.
261 initializer :initialize_time_zone do
262 if config.time_zone
36a9644 @jeremy Clarify date/time dependencies
jeremy authored Nov 1, 2009
263 require 'active_support/core_ext/time/zones'
5c42157 Even more initializers ported over
Carl Lerche authored Oct 8, 2009
264 zone_default = Time.__send__(:get_zone, config.time_zone)
265
266 unless zone_default
267 raise \
268 'Value assigned to config.time_zone not recognized.' +
269 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
270 end
271
272 Time.zone_default = zone_default
273
274 if config.frameworks.include?(:active_record)
275 ActiveRecord::Base.time_zone_aware_attributes = true
276 ActiveRecord::Base.default_timezone = :utc
277 end
278 end
279 end
280
281 # Set the i18n configuration from config.i18n but special-case for the load_path which should be
282 # appended to what's already set instead of overwritten.
283 initializer :initialize_i18n do
284 config.i18n.each do |setting, value|
285 if setting == :load_path
286 I18n.load_path += value
287 else
288 I18n.send("#{setting}=", value)
289 end
290 end
291 end
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 8, 2009
292
293 # Initializes framework-specific settings for each of the loaded frameworks
294 # (Configuration#frameworks). The available settings map to the accessors
295 # on each of the corresponding Base classes.
296 initializer :initialize_framework_settings do
297 config.frameworks.each do |framework|
298 base_class = framework.to_s.camelize.constantize.const_get("Base")
299
300 config.send(framework).each do |setting, value|
301 base_class.send("#{setting}=", value)
302 end
303 end
304 end
305
306 # Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
307 # (but only for those frameworks that are to be loaded). If the framework's
308 # paths have already been set, it is not changed, otherwise it is
309 # set to use Configuration#view_path.
310 initializer :initialize_framework_views do
311 if config.frameworks.include?(:action_view)
312 view_path = ActionView::PathSet.type_cast(config.view_path, config.cache_classes)
313 ActionMailer::Base.template_root = view_path if config.frameworks.include?(:action_mailer) && ActionMailer::Base.view_paths.blank?
314 ActionController::Base.view_paths = view_path if config.frameworks.include?(:action_controller) && ActionController::Base.view_paths.blank?
315 end
316 end
317
318 initializer :initialize_metal do
319 # TODO: Make Rails and metal work without ActionController
320 if config.frameworks.include?(:action_controller)
321 Rails::Rack::Metal.requested_metals = config.metals
322
323 config.middleware.insert_before(
324 :"ActionDispatch::ParamsParser",
325 Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
326 end
327 end
328
329 # # bail out if gems are missing - note that check_gem_dependencies will have
330 # # already called abort() unless $gems_rake_task is set
331 # return unless gems_dependencies_loaded
332 initializer :load_application_initializers do
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
333 Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer|
d0f4d93 Remove some remnants of config.gem
Carl Lerche authored Oct 17, 2009
334 load(initializer)
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
335 end
336 end
337
338 # Fires the user-supplied after_initialize block (Configuration#after_initialize)
339 initializer :after_initialize do
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
340 config.after_initialize_blocks.each do |block|
d0f4d93 Remove some remnants of config.gem
Carl Lerche authored Oct 17, 2009
341 block.call
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
342 end
343 end
344
345 # # Setup database middleware after initializers have run
346 initializer :initialize_database_middleware do
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
347 if config.frameworks.include?(:active_record)
348 if config.frameworks.include?(:action_controller) && ActionController::Base.session_store &&
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
349 ActionController::Base.session_store.name == 'ActiveRecord::SessionStore'
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
350 config.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
351 config.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
352 else
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
353 config.middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
354 config.middleware.use ActiveRecord::QueryCache
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
355 end
356 end
357 end
358
359 # TODO: Make a DSL way to limit an initializer to a particular framework
360
361 # # Prepare dispatcher callbacks and run 'prepare' callbacks
362 initializer :prepare_dispatcher do
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
363 next unless config.frameworks.include?(:action_controller)
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
364 require 'rails/dispatcher' unless defined?(::Dispatcher)
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
365 Dispatcher.define_dispatcher_callbacks(config.cache_classes)
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
366
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
367 unless config.cache_classes
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
368 # Setup dev mode route reloading
369 routes_last_modified = routes_changed_at
370 reload_routes = lambda do
371 unless routes_changed_at == routes_last_modified
372 routes_last_modified = routes_changed_at
373 reload_routes!
374 end
375 end
f0bbc64 @josh Procs don't call themselves
josh authored Dec 15, 2009
376 ActionDispatch::Callbacks.before_dispatch { |callbacks| reload_routes.call }
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
377 end
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
378 end
379
380 # Routing must be initialized after plugins to allow the former to extend the routes
381 # ---
382 # If Action Controller is not one of the loaded frameworks (Configuration#frameworks)
383 # this does nothing. Otherwise, it loads the routing definitions and sets up
384 # loading module used to lazily load controllers (Configuration#controller_paths).
385 initializer :initialize_routing do
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
386 next unless config.frameworks.include?(:action_controller)
387 route_configuration_files << config.routes_configuration_file
388 route_configuration_files << config.builtin_routes_configuration_file
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
389 reload_routes!
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
390 end
391 #
392 # # Observers are loaded after plugins in case Observers or observed models are modified by plugins.
393 initializer :load_observers do
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
394 if config.frameworks.include?(:active_record)
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
395 ActiveRecord::Base.instantiate_observers
396 end
397 end
398
399 # Eager load application classes
400 initializer :load_application_classes do
401 next if $rails_rake_task
402
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
403 if config.cache_classes
404 config.eager_load_paths.each do |load_path|
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
405 matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
406 Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
407 require_dependency file.sub(matcher, '\1')
408 end
409 end
410 end
411 end
412
413 # Disable dependency loading during request cycle
414 initializer :disable_dependency_loading do
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
415 if config.cache_classes && !config.dependency_loading
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored Oct 9, 2009
416 ActiveSupport::Dependencies.unhook!
417 end
418 end
419
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored Oct 15, 2009
420 # For each framework, search for instrument file with Notifications hooks.
aeaabc6 @josevalim Configure Orchestra on initialization.
josevalim authored Oct 9, 2009
421 #
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored Oct 15, 2009
422 initializer :load_notifications_hooks do
aeaabc6 @josevalim Configure Orchestra on initialization.
josevalim authored Oct 9, 2009
423 config.frameworks.each do |framework|
424 begin
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored Oct 15, 2009
425 require "#{framework}/notifications"
aeaabc6 @josevalim Configure Orchestra on initialization.
josevalim authored Oct 9, 2009
426 rescue LoadError => e
427 end
428 end
429 end
accd9b4 @josh Restore "Start Rails::Application object
josh authored Sep 26, 2009
430 end
431 end
Something went wrong with that request. Please try again.