Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 289 lines (253 sloc) 10.215 kb
6690d66 José Valim Rename config.cookie_secret to config.secret_token and pass it as config...
josevalim authored
1 require 'active_support/core_ext/hash/reverse_merge'
772c2b0 José Valim Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
josevalim authored
2 require 'active_support/file_update_checker'
02c5137 José Valim Add view paths to Engine setup.
josevalim authored
3 require 'fileutils'
b17e358 José Valim Move configuration to subfolders.
josevalim authored
4 require 'rails/plugin'
2fde9d7 José Valim Solve some pendencies.
josevalim authored
5 require 'rails/engine'
dc677f7 tests pass with requiring the frameworks in rails.rb
Carlhuda authored
6
accd9b4 Joshua Peek Restore "Start Rails::Application object
josh authored
7 module Rails
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
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
eb716f6 Matt Jankowski grammar/tense correction to rails application doc
mjankowski authored
14 # initializers. It also executes some bootstrap initializers (check
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
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",
9b19a6f José Valim A few changes were done in this commit:
josevalim authored
23 # "logger", "reload_plugins" and so forth.
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
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 #
45e6028 Yehuda Katz Removing Metal from Rails 3.
wycats authored
32 # == Middlewares
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
33 #
45e6028 Yehuda Katz Removing Metal from Rails 3.
wycats authored
34 # The Application is also responsible for building the middleware stack.
9cfeefb Yehuda Katz Reorganized initializers a bit to enable better hooks for common cases w...
wycats authored
35 #
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
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.
49 # One by one, each engine sets up its load paths, routes and runs its initializer files.
50 # 8) Build the middleware stack and run to_prepare callbacks
51 # 9) Run config.before_eager_load and eager_load if cache classes is true
52 # 10) Run config.after_initialize callbacks
53 #
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
54 class Application < Engine
4f03603 José Valim Break application.rb file in smaller chunks.
josevalim authored
55 autoload :Bootstrap, 'rails/application/bootstrap'
b17e358 José Valim Move configuration to subfolders.
josevalim authored
56 autoload :Configuration, 'rails/application/configuration'
4f03603 José Valim Break application.rb file in smaller chunks.
josevalim authored
57 autoload :Finisher, 'rails/application/finisher'
13d66cd José Valim Extract Railtie load from application.
josevalim authored
58 autoload :Railties, 'rails/application/railties'
3049e64 Piotr Sarnacki Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
59 autoload :RoutesReloader, 'rails/application/routes_reloader'
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
60
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
61 class << self
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
62 def inherited(base)
2fde9d7 José Valim Solve some pendencies.
josevalim authored
63 raise "You cannot have more than one Rails::Application" if Rails.application
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
64 super
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
65 Rails.application = base.instance
6f83a50 Xavier Noria renames load_(once_)paths to autoload_(once_)paths in dependencies and c...
fxn authored
66 Rails.application.add_lib_to_load_path!
9cfeefb Yehuda Katz Reorganized initializers a bit to enable better hooks for common cases w...
wycats authored
67 ActiveSupport.run_load_hooks(:before_configuration, base.instance)
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
68 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
69 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
70
52e01fc José Valim Everyone receives app as argument for consistency.
josevalim authored
71 attr_accessor :assets, :sandbox
72 alias_method :sandbox?, :sandbox
73
eedbf87 Piotr Sarnacki New way of generating urls for Application from Engine.
drogus authored
74 delegate :default_url_options, :default_url_options=, :to => :routes
75
44f0709 Aaron Patterson dealing with some -w warnings on startup
tenderlove authored
76 def initialize
77 super
78 @initialized = false
79 end
80
9e081ca José Valim Improve documentation for add_lib_to_load_paths!
josevalim authored
81 # This method is called just after an application inherits from Rails::Application,
82 # allowing the developer to load classes in lib and use them during application
83 # configuration.
84 #
85 # class MyApplication < Rails::Application
86 # require "my_backend" # in lib/my_backend
87 # config.i18n.backend = MyBackend
88 # end
89 #
90 # Notice this method takes into consideration the default root path. So if you
91 # are changing config.root inside your application definition or having a custom
92 # Rails application, you will need to add lib to $LOAD_PATH on your own in case
93 # you need to load files in lib/ during the application configuration as well.
6f83a50 Xavier Noria renames load_(once_)paths to autoload_(once_)paths in dependencies and c...
fxn authored
94 def add_lib_to_load_path! #:nodoc:
5a0d73f José Valim Add lib to load paths when application is inherited to be able to load l...
josevalim authored
95 path = config.root.join('lib').to_s
96 $LOAD_PATH.unshift(path) if File.exists?(path)
97 end
98
9e081ca José Valim Improve documentation for add_lib_to_load_paths!
josevalim authored
99 def require_environment! #:nodoc:
d649bf1 José Valim Provide a cleaner syntax for paths configuration that does not rely on m...
josevalim authored
100 environment = paths["config/environment"].existent.first
13d66cd José Valim Extract Railtie load from application.
josevalim authored
101 require environment if environment
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored
102 end
103
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
104 # Reload application routes regardless if they changed or not.
7b0c592 José Valim reload_routes! is part of the public API and should not be removed.
josevalim authored
105 def reload_routes!
106 routes_reloader.reload!
107 end
108
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
109 def routes_reloader #:nodoc:
3049e64 Piotr Sarnacki Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
110 @routes_reloader ||= RoutesReloader.new
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
111 end
5f8e48c Joshua Peek Move route reloading into railties
josh authored
112
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
113 # A routes reloader hook that is used to setup to_prepare callbacks.
114 # A plugin may override this if they desire to provide a more
115 # exquisite route reloading.
116 # :api: plugin
117 def routes_reloader_hook
118 app = self
119 lambda { app.routes_reloader.execute_if_updated }
120 end
121
122 # An app reloader hook that is used to setup to_cleanup callbacks.
123 # A plugin may override this if they desire to provide a more exquisite app reloading.
124 # :api: plugin
125 def app_reloader_hook
126 lambda {
127 ActiveSupport::DescendantsTracker.clear
128 ActiveSupport::Dependencies.clear
129 }
130 end
131
132 # Initialize the application passing the given group. By default, the
133 # group is :default but sprockets precompilation passes group equals
134 # to assets if initialize_on_precompile is false to avoid booting the
135 # whole app.
136 def initialize!(group=:default) #:nodoc:
abeb0ff Piotr Sarnacki Ensure that Rails.application.initialize! is called only once
drogus authored
137 raise "Application has been already initialized." if @initialized
eb367af José Valim `rake assets:precompile` loads the application but does not initialize i...
josevalim authored
138 run_initializers(group, self)
abeb0ff Piotr Sarnacki Ensure that Rails.application.initialize! is called only once
drogus authored
139 @initialized = true
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
140 self
5f8e48c Joshua Peek Move route reloading into railties
josh authored
141 end
142
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
143 # Load the application and its railties tasks and invoke the registered hooks.
144 # Check <tt>Rails::Railtie.rake_tasks</tt> for more info.
52e01fc José Valim Everyone receives app as argument for consistency.
josevalim authored
145 def load_tasks(app=self)
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
146 initialize_tasks
e461e1b José Valim Ensure application rake tasks and generators are loaded after the ones s...
josevalim authored
147 super
13d66cd José Valim Extract Railtie load from application.
josevalim authored
148 self
94bb331 Jeremy Kemper Shift more responsibility from application class to its singleton instan...
jeremy authored
149 end
150
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
151 # Load the application console and invoke the registered hooks.
152 # Check <tt>Rails::Railtie.console</tt> for more info.
52e01fc José Valim Everyone receives app as argument for consistency.
josevalim authored
153 def load_console(app=self)
594b749 José Valim Streamline generators initialization flow.
josevalim authored
154 initialize_console
52e01fc José Valim Everyone receives app as argument for consistency.
josevalim authored
155 super
fa98eca José Valim Add console hook to force ActiveRecord::Base to be loaded when console s...
josevalim authored
156 self
157 end
158
ea649de Franck Verrot Rails.application#env_config is now public API. [Closes #1924]
franckverrot authored
159 # Rails.application.env_config stores some of the Rails initial environment parameters.
160 # Currently stores:
161 #
162 # * action_dispatch.parameter_filter" => config.filter_parameters,
163 # * action_dispatch.secret_token" => config.secret_token,
164 # * action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions
165 #
166 # These parameters will be used by middlewares and engines to configure themselves.
167 #
a132229 Piotr Sarnacki Added ability to set asset_path for engines
drogus authored
168 def env_config
169 @env_config ||= super.merge({
6690d66 José Valim Rename config.cookie_secret to config.secret_token and pass it as config...
josevalim authored
170 "action_dispatch.parameter_filter" => config.filter_parameters,
a132229 Piotr Sarnacki Added ability to set asset_path for engines
drogus authored
171 "action_dispatch.secret_token" => config.secret_token,
cd9d28d Sergey Nartimov middlewares should use logger from env
lest authored
172 "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
fe7d4f0 Sergey Nartimov put backtrace_cleaner to env
lest authored
173 "action_dispatch.logger" => Rails.logger,
174 "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner
a132229 Piotr Sarnacki Added ability to set asset_path for engines
drogus authored
175 })
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
176 end
177
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
178 # Returns the ordered railties for this application considering railties_order.
179 def ordered_railties #:nodoc:
40b19e0 Piotr Sarnacki Allow to change engine's loading priority with config.railties_order=
drogus authored
180 @ordered_railties ||= begin
181 order = config.railties_order.map do |railtie|
182 if railtie == :main_app
183 self
184 elsif railtie.respond_to?(:instance)
185 railtie.instance
186 else
187 railtie
188 end
189 end
190
191 all = (railties.all - order)
192 all.push(self) unless all.include?(self)
193 order.push(:all) unless order.include?(:all)
194
195 index = order.index(:all)
196 order[index] = all
197 order.reverse.flatten
198 end
199 end
200
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
201 def initializers #:nodoc:
e9793c6 Santiago Pastorino Makes this code a bit more clear
spastorino authored
202 Bootstrap.initializers_for(self) +
203 super +
204 Finisher.initializers_for(self)
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
205 end
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
206
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
207 def config #:nodoc:
32a5b49 Piotr Sarnacki Move singleton pattern to Railtie and remove Engine::Configurable and Ap...
drogus authored
208 @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
209 end
210
8496be7 Guillermo Iguaran Fixing Rails::Server#app under Rack::URLMap
guilleiguaran authored
211 def to_app
212 self
213 end
214
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
215 def helpers_paths #:nodoc:
fd86a1b José Valim Rely on a public contract between railties instead of accessing railtie ...
josevalim authored
216 config.helpers_paths
217 end
218
2fe70c1 José Valim Booya, sprockets now works from Engines.
josevalim authored
219 protected
220
221 alias :build_middleware_stack :app
612454e Joshua Peek Move sprockets initializers back to application
josh authored
222
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
223 def default_middleware_stack
224 ActionDispatch::MiddlewareStack.new.tap do |middleware|
2c0c4d7 Prem Sichanugrist Add `config.force_ssl` configuration which will load `Rack::SSL` middlew...
sikachu authored
225 if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
226 require "action_dispatch/http/rack_cache"
227 middleware.use ::Rack::Cache, rack_cache
228 end
254ab7d Yehuda Katz First pass at Rack::Cache
wycats authored
229
2c0c4d7 Prem Sichanugrist Add `config.force_ssl` configuration which will load `Rack::SSL` middlew...
sikachu authored
230 if config.force_ssl
231 require "rack/ssl"
cb5c39f Justin S. Leitgeb Make the Rack::SSL middleware configurable
jsl authored
232 middleware.use ::Rack::SSL, config.ssl_options
2c0c4d7 Prem Sichanugrist Add `config.force_ssl` configuration which will load `Rack::SSL` middlew...
sikachu authored
233 end
eeb3246 Only add Rack::Cache to the middleware stack if config.action_controller...
Sparky authored
234
e6369bc Piotr Sarnacki Application's assets should have higher priority than engine's ones
drogus authored
235 if config.serve_static_assets
1ca8123 José Valim Add static_cache_control as configuration option and set a default for t...
josevalim authored
236 middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
e6369bc Piotr Sarnacki Application's assets should have higher priority than engine's ones
drogus authored
237 end
2c0c4d7 Prem Sichanugrist Add `config.force_ssl` configuration which will load `Rack::SSL` middlew...
sikachu authored
238
ea14396 Neeraj Singh replace if ! with unless
neerajdotname authored
239 middleware.use ::Rack::Lock unless config.allow_concurrency
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
240 middleware.use ::Rack::Runtime
17a91a6 David Lee Logs should show overridden method; Issue 426
dlee authored
241 middleware.use ::Rack::MethodOverride
afde6fd David Heinemeier Hansson Added X-Request-Id tracking and TaggedLogging to easily log that and oth...
dhh authored
242 middleware.use ::ActionDispatch::RequestId
c83d9a1 José Valim Unify logger and taggedlogging middleware as both address logging concer...
josevalim authored
243 middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
a9e8cf7 Sergey Nartimov add ActionController::Metal#show_detailed_exceptions?
lest authored
244 middleware.use ::ActionDispatch::ShowExceptions
750bb5c José Valim Split ShowExceptions responsibilities in two middlewares.
josevalim authored
245 middleware.use ::ActionDispatch::DebugExceptions
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
246 middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
750bb5c José Valim Split ShowExceptions responsibilities in two middlewares.
josevalim authored
247
19ac034 Santiago Pastorino Don't use Rack::Sendfile middleware if x_sendfile_header is not present
spastorino authored
248 if config.action_dispatch.x_sendfile_header.present?
249 middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
250 end
750bb5c José Valim Split ShowExceptions responsibilities in two middlewares.
josevalim authored
251
0f7c970 Introduce ActionDispatch::Reloader
John Firebaugh authored
252 middleware.use ::ActionDispatch::Reloader unless config.cache_classes
435bccd Replace AD::Callbacks.to_prepare with AD::Reloader.to_prepare
John Firebaugh authored
253 middleware.use ::ActionDispatch::Callbacks
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
254 middleware.use ::ActionDispatch::Cookies
255
256 if config.session_store
257 middleware.use config.session_store, config.session_options
258 middleware.use ::ActionDispatch::Flash
259 end
260
261 middleware.use ::ActionDispatch::ParamsParser
262 middleware.use ::ActionDispatch::Head
74dd8a3 José Valim Move ETag and ConditionalGet logic from AD::Response to the middleware s...
josevalim authored
263 middleware.use ::Rack::ConditionalGet
264 middleware.use ::Rack::ETag, "no-cache"
2c0c4d7 Prem Sichanugrist Add `config.force_ssl` configuration which will load `Rack::SSL` middlew...
sikachu authored
265
266 if config.action_dispatch.best_standards_support
267 middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support
268 end
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
269 end
270 end
271
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
272 def initialize_tasks #:nodoc:
e290835 Patrick Quinn Make Rake::DSL methods available within Railtie rake_tasks block
bilts authored
273 self.class.rake_tasks do
274 require "rails/tasks"
275 task :environment do
276 $rails_rake_task = true
277 require_environment!
278 end
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
279 end
280 end
281
27f0add José Valim Improve docs for Rails::Application and add routes_reloader_hook and app...
josevalim authored
282 def initialize_console #:nodoc:
d54c778 Akira Matsuda always enable `pp` on the Rails console
amatsuda authored
283 require "pp"
fa98eca José Valim Add console hook to force ActiveRecord::Base to be loaded when console s...
josevalim authored
284 require "rails/console/app"
285 require "rails/console/helpers"
286 end
accd9b4 Joshua Peek Restore "Start Rails::Application object
josh authored
287 end
17a91a6 David Lee Logs should show overridden method; Issue 426
dlee authored
288 end
Something went wrong with that request. Please try again.