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