Skip to content
This repository
Newer
Older
100644 327 lines (286 sloc) 11.591 kb
6690d662 » josevalim
2010-04-05 Rename config.cookie_secret to config.secret_token and pass it as con…
1 require 'active_support/core_ext/hash/reverse_merge'
772c2b0b » josevalim
2010-06-20 Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
2 require 'active_support/file_update_checker'
02c5137e » josevalim
2010-01-22 Add view paths to Engine setup.
3 require 'fileutils'
b17e358e » josevalim
2010-01-23 Move configuration to subfolders.
4 require 'rails/plugin'
2fde9d77 » josevalim
2010-01-24 Solve some pendencies.
5 require 'rails/engine'
dc677f76 » Carlhuda
2009-12-22 tests pass with requiring the frameworks in rails.rb
6
accd9b46 » josh
2009-09-25 Restore "Start Rails::Application object
7 module Rails
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
8 # In Rails 3.0, a Rails::Application object was introduced which is nothing more than
9 # an Engine but with the responsibility of coordinating the whole boot process.
10 #
11 # == Initialization
12 #
13 # Rails::Application is responsible for executing all railties, engines and plugin
eb716f63 » mjankowski
2011-07-12 grammar/tense correction to rails application doc
14 # initializers. It also executes some bootstrap initializers (check
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
15 # Rails::Application::Bootstrap) and finishing initializers, after all the others
16 # are executed (check Rails::Application::Finisher).
17 #
18 # == Configuration
19 #
20 # Besides providing the same configuration as Rails::Engine and Rails::Railtie,
21 # the application object has several specific configurations, for example
22 # "allow_concurrency", "cache_classes", "consider_all_requests_local", "filter_parameters",
9b19a6f1 » josevalim
2010-06-28 A few changes were done in this commit:
23 # "logger", "reload_plugins" and so forth.
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
24 #
25 # Check Rails::Application::Configuration to see them all.
26 #
27 # == Routes
28 #
29 # The application object is also responsible for holding the routes and reloading routes
30 # whenever the files change in development.
31 #
45e60283 » wycats
2010-05-29 Removing Metal from Rails 3.
32 # == Middlewares
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
33 #
45e60283 » wycats
2010-05-29 Removing Metal from Rails 3.
34 # The Application is also responsible for building the middleware stack.
9cfeefb6 » wycats
2010-05-15 Reorganized initializers a bit to enable better hooks for common case…
35 #
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
36 # == Booting process
37 #
38 # The application is also responsible for setting up and executing the booting
39 # process. From the moment you require "config/application.rb" in your app,
40 # the booting process goes like this:
41 #
42 # 1) require "config/boot.rb" to setup load paths
43 # 2) require railties and engines
44 # 3) Define Rails.application as "class MyApp::Application < Rails::Application"
45 # 4) Run config.before_configuration callbacks
46 # 5) Load config/environments/ENV.rb
47 # 6) Run config.before_initialize callbacks
48 # 7) Run Railtie#initializer defined by railties, engines and application.
c2e3ce8d » josevalim
2011-12-12 Provide a unique point for running initializers.
49 # One by one, each engine sets up its load paths, routes, locales and so on.
50 # 8) Runs all registered config/initializers/*, executing the engines one first
51 # 9) Build the middleware stack and run to_prepare callbacks
52 # 10) Run config.before_eager_load and eager_load if cache classes is true
53 # 11) Run config.after_initialize callbacks
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
54 #
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
55 class Application < Engine
4f036032 » josevalim
2010-01-23 Break application.rb file in smaller chunks.
56 autoload :Bootstrap, 'rails/application/bootstrap'
b17e358e » josevalim
2010-01-23 Move configuration to subfolders.
57 autoload :Configuration, 'rails/application/configuration'
4f036032 » josevalim
2010-01-23 Break application.rb file in smaller chunks.
58 autoload :Finisher, 'rails/application/finisher'
13d66cdf » josevalim
2010-01-23 Extract Railtie load from application.
59 autoload :Railties, 'rails/application/railties'
3049e645 » drogus
2010-09-29 Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
60 autoload :RoutesReloader, 'rails/application/routes_reloader'
80130d12 » josevalim
2010-01-23 Extract routes reloading responsibilities from application and load t…
61
e4d7e509 » Carl Lerche
2009-10-08 Moving more initializers into the application object
62 class << self
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
63 def inherited(base)
2fde9d77 » josevalim
2010-01-24 Solve some pendencies.
64 raise "You cannot have more than one Rails::Application" if Rails.application
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
65 super
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
66 Rails.application = base.instance
6f83a503 » fxn
2010-06-22 renames load_(once_)paths to autoload_(once_)paths in dependencies an…
67 Rails.application.add_lib_to_load_path!
9cfeefb6 » wycats
2010-05-15 Reorganized initializers a bit to enable better hooks for common case…
68 ActiveSupport.run_load_hooks(:before_configuration, base.instance)
e4d7e509 » Carl Lerche
2009-10-08 Moving more initializers into the application object
69 end
897164dd » Yehuda Katz + Carl Lerche
2009-11-02 Conceptually unify instance & global initializers
70 end
6f6a589d » Yehuda Katz + Carl Lerche
2009-09-28 Create the application object from config/environment.rb
71
52e01fc5 » josevalim
2011-05-25 Everyone receives app as argument for consistency.
72 attr_accessor :assets, :sandbox
73 alias_method :sandbox?, :sandbox
fa1d9a88 » josevalim
2011-12-12 Speed up development by only reloading classes if dependencies files …
74 attr_reader :reloaders
52e01fc5 » josevalim
2011-05-25 Everyone receives app as argument for consistency.
75
eedbf87d » drogus
2010-07-08 New way of generating urls for Application from Engine.
76 delegate :default_url_options, :default_url_options=, :to => :routes
77
44f07098 » tenderlove
2011-08-24 dealing with some -w warnings on startup
78 def initialize
79 super
80 @initialized = false
fa1d9a88 » josevalim
2011-12-12 Speed up development by only reloading classes if dependencies files …
81 @reloaders = []
44f07098 » tenderlove
2011-08-24 dealing with some -w warnings on startup
82 end
83
9e081cae » josevalim
2010-06-20 Improve documentation for add_lib_to_load_paths!
84 # This method is called just after an application inherits from Rails::Application,
85 # allowing the developer to load classes in lib and use them during application
86 # configuration.
87 #
88 # class MyApplication < Rails::Application
89 # require "my_backend" # in lib/my_backend
90 # config.i18n.backend = MyBackend
91 # end
92 #
93 # Notice this method takes into consideration the default root path. So if you
94 # are changing config.root inside your application definition or having a custom
95 # Rails application, you will need to add lib to $LOAD_PATH on your own in case
96 # you need to load files in lib/ during the application configuration as well.
6f83a503 » fxn
2010-06-22 renames load_(once_)paths to autoload_(once_)paths in dependencies an…
97 def add_lib_to_load_path! #:nodoc:
5a0d73f1 » josevalim
2010-06-02 Add lib to load paths when application is inherited to be able to loa…
98 path = config.root.join('lib').to_s
99 $LOAD_PATH.unshift(path) if File.exists?(path)
100 end
101
9e081cae » josevalim
2010-06-20 Improve documentation for add_lib_to_load_paths!
102 def require_environment! #:nodoc:
d649bf15 » josevalim
2010-10-06 Provide a cleaner syntax for paths configuration that does not rely o…
103 environment = paths["config/environment"].existent.first
13d66cdf » josevalim
2010-01-23 Extract Railtie load from application.
104 require environment if environment
d982fe2b » Carlhuda
2009-12-21 Replace reopening the class with App.configure as an alias to class_eval
105 end
106
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
107 # Reload application routes regardless if they changed or not.
7b0c592e » josevalim
2010-10-02 reload_routes! is part of the public API and should not be removed.
108 def reload_routes!
109 routes_reloader.reload!
110 end
111
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
112 def routes_reloader #:nodoc:
3049e645 » drogus
2010-09-29 Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
113 @routes_reloader ||= RoutesReloader.new
80130d12 » josevalim
2010-01-23 Extract routes reloading responsibilities from application and load t…
114 end
5f8e48cb » josh
2009-12-14 Move route reloading into railties
115
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
116 # A routes reloader hook that is used to setup to_prepare callbacks.
117 # A plugin may override this if they desire to provide a more
118 # exquisite route reloading.
119 # :api: plugin
c0466603 » josevalim
2011-12-12 Give hooks the flexibility to choose the type of callback.
120 def set_routes_reloader_hook
121 reloader = routes_reloader
122 hook = lambda { reloader.execute_if_updated }
123 hook.call
fa1d9a88 » josevalim
2011-12-12 Speed up development by only reloading classes if dependencies files …
124 self.reloaders << reloader
c0466603 » josevalim
2011-12-12 Give hooks the flexibility to choose the type of callback.
125 ActionDispatch::Reloader.to_prepare(&hook)
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
126 end
127
c0466603 » josevalim
2011-12-12 Give hooks the flexibility to choose the type of callback.
128 # An app dependencies hook that is used to setup to_cleanup callbacks.
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
129 # A plugin may override this if they desire to provide a more exquisite app reloading.
130 # :api: plugin
c0466603 » josevalim
2011-12-12 Give hooks the flexibility to choose the type of callback.
131 def set_dependencies_hook
fa1d9a88 » josevalim
2011-12-12 Speed up development by only reloading classes if dependencies files …
132 callback = lambda do
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
133 ActiveSupport::DescendantsTracker.clear
134 ActiveSupport::Dependencies.clear
c0466603 » josevalim
2011-12-12 Give hooks the flexibility to choose the type of callback.
135 end
fa1d9a88 » josevalim
2011-12-12 Speed up development by only reloading classes if dependencies files …
136
137 if config.reload_classes_only_on_change
138 reloader = ActiveSupport::FileUpdateChecker.new(watchable_args, true, &callback)
139 self.reloaders << reloader
140 # We need to set a to_prepare callback regardless of the reloader result, i.e.
141 # models should be reloaded if any of the reloaders (i18n, routes) were updated.
142 ActionDispatch::Reloader.to_prepare(:prepend => true, &callback)
143 else
144 ActionDispatch::Reloader.to_cleanup(&callback)
145 end
146 end
147
148 # Returns an array of file paths appended with a hash of directories-extensions
149 # suitable for ActiveSupport::FileUpdateChecker API.
150 def watchable_args
151 files = []
152 files.concat config.watchable_files
153
154 dirs = {}
155 dirs.merge! config.watchable_dirs
156 ActiveSupport::Dependencies.autoload_paths.each do |path|
157 dirs[path.to_s] = [:rb]
158 end
159
160 files << dirs
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
161 end
162
163 # Initialize the application passing the given group. By default, the
164 # group is :default but sprockets precompilation passes group equals
165 # to assets if initialize_on_precompile is false to avoid booting the
166 # whole app.
167 def initialize!(group=:default) #:nodoc:
abeb0ff2 » drogus
2010-07-22 Ensure that Rails.application.initialize! is called only once
168 raise "Application has been already initialized." if @initialized
eb367afe » josevalim
2011-09-24 `rake assets:precompile` loads the application but does not initializ…
169 run_initializers(group, self)
abeb0ff2 » drogus
2010-07-22 Ensure that Rails.application.initialize! is called only once
170 @initialized = true
80130d12 » josevalim
2010-01-23 Extract routes reloading responsibilities from application and load t…
171 self
5f8e48cb » josh
2009-12-14 Move route reloading into railties
172 end
173
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
174 # Load the application and its railties tasks and invoke the registered hooks.
175 # Check <tt>Rails::Railtie.rake_tasks</tt> for more info.
52e01fc5 » josevalim
2011-05-25 Everyone receives app as argument for consistency.
176 def load_tasks(app=self)
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
177 initialize_tasks
e461e1bc » josevalim
2010-04-26 Ensure application rake tasks and generators are loaded after the one…
178 super
13d66cdf » josevalim
2010-01-23 Extract Railtie load from application.
179 self
94bb3316 » jeremy
2009-12-23 Shift more responsibility from application class to its singleton ins…
180 end
181
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
182 # Load the application console and invoke the registered hooks.
183 # Check <tt>Rails::Railtie.console</tt> for more info.
52e01fc5 » josevalim
2011-05-25 Everyone receives app as argument for consistency.
184 def load_console(app=self)
594b749f » josevalim
2011-05-25 Streamline generators initialization flow.
185 initialize_console
52e01fc5 » josevalim
2011-05-25 Everyone receives app as argument for consistency.
186 super
fa98eca7 » josevalim
2010-07-17 Add console hook to force ActiveRecord::Base to be loaded when consol…
187 self
188 end
189
ea649de6 » franckverrot
2011-07-11 Rails.application#env_config is now public API. [Closes #1924]
190 # Rails.application.env_config stores some of the Rails initial environment parameters.
191 # Currently stores:
192 #
193 # * action_dispatch.parameter_filter" => config.filter_parameters,
194 # * action_dispatch.secret_token" => config.secret_token,
195 # * action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions
196 #
197 # These parameters will be used by middlewares and engines to configure themselves.
198 #
a132229d » drogus
2010-07-22 Added ability to set asset_path for engines
199 def env_config
200 @env_config ||= super.merge({
6690d662 » josevalim
2010-04-05 Rename config.cookie_secret to config.secret_token and pass it as con…
201 "action_dispatch.parameter_filter" => config.filter_parameters,
a132229d » drogus
2010-07-22 Added ability to set asset_path for engines
202 "action_dispatch.secret_token" => config.secret_token,
cd9d28d6 » lest
2011-11-24 middlewares should use logger from env
203 "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
fe7d4f09 » lest
2011-11-28 put backtrace_cleaner to env
204 "action_dispatch.logger" => Rails.logger,
205 "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner
a132229d » drogus
2010-07-22 Added ability to set asset_path for engines
206 })
6d6ae084 » Carl Lerche
2009-10-07 Start moving the initializers into the application object
207 end
208
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
209 # Returns the ordered railties for this application considering railties_order.
210 def ordered_railties #:nodoc:
40b19e06 » drogus
2011-11-22 Allow to change engine's loading priority with config.railties_order=
211 @ordered_railties ||= begin
212 order = config.railties_order.map do |railtie|
213 if railtie == :main_app
214 self
215 elsif railtie.respond_to?(:instance)
216 railtie.instance
217 else
218 railtie
219 end
220 end
221
222 all = (railties.all - order)
223 all.push(self) unless all.include?(self)
224 order.push(:all) unless order.include?(:all)
225
226 index = order.index(:all)
227 order[index] = all
228 order.reverse.flatten
229 end
230 end
231
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
232 def initializers #:nodoc:
e9793c60 » spastorino
2010-10-08 Makes this code a bit more clear
233 Bootstrap.initializers_for(self) +
234 super +
235 Finisher.initializers_for(self)
98240c49 » josevalim
2010-01-23 Get rid of initializers global and create i18n railtie.
236 end
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
237
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
238 def config #:nodoc:
32a5b499 » drogus
2010-07-19 Move singleton pattern to Railtie and remove Engine::Configurable and…
239 @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
240 end
241
8496be7e » guilleiguaran
2011-06-06 Fixing Rails::Server#app under Rack::URLMap
242 def to_app
243 self
244 end
245
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
246 def helpers_paths #:nodoc:
fd86a1b6 » josevalim
2011-11-23 Rely on a public contract between railties instead of accessing railt…
247 config.helpers_paths
248 end
249
2fe70c18 » josevalim
2011-04-15 Booya, sprockets now works from Engines.
250 protected
251
252 alias :build_middleware_stack :app
612454e0 » josh
2011-03-29 Move sprockets initializers back to application
253
fa1d9a88 » josevalim
2011-12-12 Speed up development by only reloading classes if dependencies files …
254 def reload_dependencies?
255 config.reload_classes_only_on_change != true || reloaders.map(&:updated?).any?
256 end
257
19d8c8cb » josevalim
2010-05-29 Remove the laziness from the middleware stack.
258 def default_middleware_stack
259 ActionDispatch::MiddlewareStack.new.tap do |middleware|
2c0c4d75 » sikachu
2011-03-27 Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
260 if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
261 require "action_dispatch/http/rack_cache"
262 middleware.use ::Rack::Cache, rack_cache
263 end
254ab7d9 » wycats
2010-09-13 First pass at Rack::Cache
264
2c0c4d75 » sikachu
2011-03-27 Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
265 if config.force_ssl
266 require "rack/ssl"
cb5c39f8 » jsl
2011-09-26 Make the Rack::SSL middleware configurable
267 middleware.use ::Rack::SSL, config.ssl_options
2c0c4d75 » sikachu
2011-03-27 Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
268 end
eeb32469 » Sparky
2010-09-15 Only add Rack::Cache to the middleware stack if config.action_control…
269
e6369bc9 » drogus
2011-02-09 Application's assets should have higher priority than engine's ones
270 if config.serve_static_assets
1ca81238 » josevalim
2011-05-03 Add static_cache_control as configuration option and set a default fo…
271 middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
e6369bc9 » drogus
2011-02-09 Application's assets should have higher priority than engine's ones
272 end
2c0c4d75 » sikachu
2011-03-27 Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
273
ea14396c » neerajdotname
2010-10-16 replace if ! with unless
274 middleware.use ::Rack::Lock unless config.allow_concurrency
19d8c8cb » josevalim
2010-05-29 Remove the laziness from the middleware stack.
275 middleware.use ::Rack::Runtime
17a91a6e » dlee
2011-05-07 Logs should show overridden method; Issue 426
276 middleware.use ::Rack::MethodOverride
afde6fdd » dhh
2011-10-19 Added X-Request-Id tracking and TaggedLogging to easily log that and …
277 middleware.use ::ActionDispatch::RequestId
c83d9a11 » josevalim
2011-10-19 Unify logger and taggedlogging middleware as both address logging con…
278 middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
a9e8cf78 » lest
2011-11-21 add ActionController::Metal#show_detailed_exceptions?
279 middleware.use ::ActionDispatch::ShowExceptions
750bb5c8 » josevalim
2011-12-01 Split ShowExceptions responsibilities in two middlewares.
280 middleware.use ::ActionDispatch::DebugExceptions
19d8c8cb » josevalim
2010-05-29 Remove the laziness from the middleware stack.
281 middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
750bb5c8 » josevalim
2011-12-01 Split ShowExceptions responsibilities in two middlewares.
282
19ac034b » spastorino
2011-07-25 Don't use Rack::Sendfile middleware if x_sendfile_header is not present
283 if config.action_dispatch.x_sendfile_header.present?
284 middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
285 end
750bb5c8 » josevalim
2011-12-01 Split ShowExceptions responsibilities in two middlewares.
286
fa1d9a88 » josevalim
2011-12-12 Speed up development by only reloading classes if dependencies files …
287 unless config.cache_classes
288 app = self
289 middleware.use ::ActionDispatch::Reloader, lambda { app.reload_dependencies? }
290 end
291
435bccda » John Firebaugh
2010-12-19 Replace AD::Callbacks.to_prepare with AD::Reloader.to_prepare
292 middleware.use ::ActionDispatch::Callbacks
19d8c8cb » josevalim
2010-05-29 Remove the laziness from the middleware stack.
293 middleware.use ::ActionDispatch::Cookies
294
295 if config.session_store
296 middleware.use config.session_store, config.session_options
297 middleware.use ::ActionDispatch::Flash
298 end
299
300 middleware.use ::ActionDispatch::ParamsParser
301 middleware.use ::ActionDispatch::Head
74dd8a36 » josevalim
2010-09-22 Move ETag and ConditionalGet logic from AD::Response to the middlewar…
302 middleware.use ::Rack::ConditionalGet
303 middleware.use ::Rack::ETag, "no-cache"
2c0c4d75 » sikachu
2011-03-27 Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
304
305 if config.action_dispatch.best_standards_support
306 middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support
307 end
19d8c8cb » josevalim
2010-05-29 Remove the laziness from the middleware stack.
308 end
309 end
310
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
311 def initialize_tasks #:nodoc:
e2908356 » bilts
2011-05-20 Make Rake::DSL methods available within Railtie rake_tasks block
312 self.class.rake_tasks do
313 require "rails/tasks"
314 task :environment do
315 $rails_rake_task = true
316 require_environment!
317 end
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
318 end
319 end
320
27f0add3 » josevalim
2011-12-12 Improve docs for Rails::Application and add routes_reloader_hook and …
321 def initialize_console #:nodoc:
d54c7781 » amatsuda
2011-05-18 always enable `pp` on the Rails console
322 require "pp"
fa98eca7 » josevalim
2010-07-17 Add console hook to force ActiveRecord::Base to be loaded when consol…
323 require "rails/console/app"
324 require "rails/console/helpers"
325 end
accd9b46 » josh
2009-09-25 Restore "Start Rails::Application object
326 end
17a91a6e » dlee
2011-05-07 Logs should show overridden method; Issue 426
327 end
Something went wrong with that request. Please try again.