Skip to content

HTTPS clone URL

Subversion checkout URL

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