Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 316 lines (271 sloc) 10.76 kB
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
1 require 'fileutils'
2fde9d7 @josevalim Solve some pendencies.
josevalim authored
2 require 'rails/engine'
dc677f7 tests pass with requiring the frameworks in rails.rb
Carlhuda authored
3
accd9b4 @josh Restore "Start Rails::Application object
josh authored
4 module Rails
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
5 # In Rails 3.0, a Rails::Application object was introduced which is nothing more than
6 # an Engine but with the responsibility of coordinating the whole boot process.
7 #
8 # == Initialization
9 #
dad7fdc @spastorino Rails::Plugin has gone
spastorino authored
10 # Rails::Application is responsible for executing all railties and engines
eb716f6 @mjankowski grammar/tense correction to rails application doc
mjankowski authored
11 # initializers. It also executes some bootstrap initializers (check
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
12 # Rails::Application::Bootstrap) and finishing initializers, after all the others
13 # are executed (check Rails::Application::Finisher).
14 #
15 # == Configuration
16 #
17 # Besides providing the same configuration as Rails::Engine and Rails::Railtie,
18 # the application object has several specific configurations, for example
19 # "allow_concurrency", "cache_classes", "consider_all_requests_local", "filter_parameters",
dad7fdc @spastorino Rails::Plugin has gone
spastorino authored
20 # "logger" and so forth.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
21 #
22 # Check Rails::Application::Configuration to see them all.
23 #
24 # == Routes
25 #
26 # The application object is also responsible for holding the routes and reloading routes
27 # whenever the files change in development.
28 #
45e6028 @wycats Removing Metal from Rails 3.
wycats authored
29 # == Middlewares
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
30 #
45e6028 @wycats Removing Metal from Rails 3.
wycats authored
31 # The Application is also responsible for building the middleware stack.
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
32 #
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
33 # == Booting process
34 #
35 # The application is also responsible for setting up and executing the booting
36 # process. From the moment you require "config/application.rb" in your app,
37 # the booting process goes like this:
38 #
39 # 1) require "config/boot.rb" to setup load paths
40 # 2) require railties and engines
41 # 3) Define Rails.application as "class MyApp::Application < Rails::Application"
42 # 4) Run config.before_configuration callbacks
43 # 5) Load config/environments/ENV.rb
44 # 6) Run config.before_initialize callbacks
45 # 7) Run Railtie#initializer defined by railties, engines and application.
11aa167 @josevalim Revert "Provide a unique point for running initializers."
josevalim authored
46 # One by one, each engine sets up its load paths, routes and runs its config/initializers/* files.
47 # 9) Custom Railtie#initializers added by railties, engines and applications are executed
48 # 10) Build the middleware stack and run to_prepare callbacks
49 # 11) Run config.before_eager_load and eager_load if cache classes is true
50 # 12) Run config.after_initialize callbacks
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
51 #
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
52 class Application < Engine
4f03603 @josevalim Break application.rb file in smaller chunks.
josevalim authored
53 autoload :Bootstrap, 'rails/application/bootstrap'
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
54 autoload :Configuration, 'rails/application/configuration'
4f03603 @josevalim Break application.rb file in smaller chunks.
josevalim authored
55 autoload :Finisher, 'rails/application/finisher'
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
56 autoload :Railties, 'rails/application/railties'
3049e64 @drogus Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
57 autoload :RoutesReloader, 'rails/application/routes_reloader'
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
58
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
59 class << self
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
60 def inherited(base)
2fde9d7 @josevalim Solve some pendencies.
josevalim authored
61 raise "You cannot have more than one Rails::Application" if Rails.application
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
62 super
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
63 Rails.application = base.instance
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
64 Rails.application.add_lib_to_load_path!
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
65 ActiveSupport.run_load_hooks(:before_configuration, base.instance)
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
66 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
67 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
68
2dab86c @carlosantoniodasilva Define only writer method for queue, as reader is declared
carlosantoniodasilva authored
69 attr_accessor :assets, :sandbox
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
70 alias_method :sandbox?, :sandbox
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
71 attr_reader :reloaders
2dab86c @carlosantoniodasilva Define only writer method for queue, as reader is declared
carlosantoniodasilva authored
72 attr_writer :queue
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
73
eedbf87 @drogus New way of generating urls for Application from Engine.
drogus authored
74 delegate :default_url_options, :default_url_options=, :to => :routes
75
44f0709 @tenderlove dealing with some -w warnings on startup
tenderlove authored
76 def initialize
77 super
78 @initialized = false
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
79 @reloaders = []
44f0709 @tenderlove dealing with some -w warnings on startup
tenderlove authored
80 end
81
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
82 # This method is called just after an application inherits from Rails::Application,
83 # allowing the developer to load classes in lib and use them during application
84 # configuration.
85 #
86 # class MyApplication < Rails::Application
87 # require "my_backend" # in lib/my_backend
88 # config.i18n.backend = MyBackend
89 # end
90 #
91 # Notice this method takes into consideration the default root path. So if you
92 # are changing config.root inside your application definition or having a custom
93 # Rails application, you will need to add lib to $LOAD_PATH on your own in case
94 # you need to load files in lib/ during the application configuration as well.
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
95 def add_lib_to_load_path! #:nodoc:
5a0d73f @josevalim Add lib to load paths when application is inherited to be able to loa…
josevalim authored
96 path = config.root.join('lib').to_s
97 $LOAD_PATH.unshift(path) if File.exists?(path)
98 end
99
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
100 def require_environment! #:nodoc:
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
101 environment = paths["config/environment"].existent.first
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
102 require environment if environment
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored
103 end
104
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
105 # Reload application routes regardless if they changed or not.
7b0c592 @josevalim reload_routes! is part of the public API and should not be removed.
josevalim authored
106 def reload_routes!
107 routes_reloader.reload!
108 end
109
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
110 def routes_reloader #:nodoc:
3049e64 @drogus Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
111 @routes_reloader ||= RoutesReloader.new
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
112 end
5f8e48c @josh Move route reloading into railties
josh authored
113
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
114 # Returns an array of file paths appended with a hash of directories-extensions
115 # suitable for ActiveSupport::FileUpdateChecker API.
116 def watchable_args
516f102 @ndbroadbent Refactored watchable_args and build_original_fullpath methods in rail…
ndbroadbent authored
117 files, dirs = config.watchable_files.dup, config.watchable_dirs.dup
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
118
119 ActiveSupport::Dependencies.autoload_paths.each do |path|
120 dirs[path.to_s] = [:rb]
121 end
122
80256ab @josevalim FileUpdateChecker should be able to handle deleted files.
josevalim authored
123 [files, dirs]
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
124 end
125
126 # Initialize the application passing the given group. By default, the
127 # group is :default but sprockets precompilation passes group equals
128 # to assets if initialize_on_precompile is false to avoid booting the
129 # whole app.
130 def initialize!(group=:default) #:nodoc:
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
131 raise "Application has been already initialized." if @initialized
eb367af @josevalim `rake assets:precompile` loads the application but does not initializ…
josevalim authored
132 run_initializers(group, self)
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
133 @initialized = true
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
134 self
5f8e48c @josh Move route reloading into railties
josh authored
135 end
136
4882271 @franckverrot Rails.initialized? can be called at any time without raising an excep…
franckverrot authored
137 def initialized?
138 @initialized
139 end
140
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
141 # Load the application and its railties tasks and invoke the registered hooks.
142 # Check <tt>Rails::Railtie.rake_tasks</tt> for more info.
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
143 def load_tasks(app=self)
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
144 initialize_tasks
e461e1b @josevalim Ensure application rake tasks and generators are loaded after the one…
josevalim authored
145 super
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
146 self
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
147 end
148
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
149 # Load the application console and invoke the registered hooks.
150 # Check <tt>Rails::Railtie.console</tt> for more info.
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
151 def load_console(app=self)
594b749 @josevalim Streamline generators initialization flow.
josevalim authored
152 initialize_console
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
153 super
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored
154 self
155 end
156
cd2c31a @vijaydev A few doc changes
vijaydev authored
157 # Stores some of the Rails initial environment parameters which
158 # will be used by middlewares and engines to configure themselves.
a132229 @drogus Added ability to set asset_path for engines
drogus authored
159 def env_config
160 @env_config ||= super.merge({
6690d66 @josevalim Rename config.cookie_secret to config.secret_token and pass it as con…
josevalim authored
161 "action_dispatch.parameter_filter" => config.filter_parameters,
a132229 @drogus Added ability to set asset_path for engines
drogus authored
162 "action_dispatch.secret_token" => config.secret_token,
cd9d28d @lest middlewares should use logger from env
lest authored
163 "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
26e7400 @josevalim Fix diagnostics page for routing errors.
josevalim authored
164 "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
fe7d4f0 @lest put backtrace_cleaner to env
lest authored
165 "action_dispatch.logger" => Rails.logger,
166 "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner
a132229 @drogus Added ability to set asset_path for engines
drogus authored
167 })
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
168 end
169
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
170 # Returns the ordered railties for this application considering railties_order.
171 def ordered_railties #:nodoc:
40b19e0 @drogus Allow to change engine's loading priority with config.railties_order=
drogus authored
172 @ordered_railties ||= begin
173 order = config.railties_order.map do |railtie|
174 if railtie == :main_app
175 self
176 elsif railtie.respond_to?(:instance)
177 railtie.instance
178 else
179 railtie
180 end
181 end
182
183 all = (railties.all - order)
184 all.push(self) unless all.include?(self)
185 order.push(:all) unless order.include?(:all)
186
187 index = order.index(:all)
188 order[index] = all
189 order.reverse.flatten
190 end
191 end
192
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
193 def initializers #:nodoc:
e9793c6 @spastorino Makes this code a bit more clear
spastorino authored
194 Bootstrap.initializers_for(self) +
195 super +
196 Finisher.initializers_for(self)
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
197 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
198
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
199 def config #:nodoc:
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
200 @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
201 end
202
adff4a7 @wycats Initial queue implementation
wycats authored
203 def queue #:nodoc:
cd50b60 @tenderlove add a factory method for queue construction so that apps can have mul…
tenderlove authored
204 @queue ||= build_queue
205 end
206
207 def build_queue # :nodoc:
208 config.queue.new
adff4a7 @wycats Initial queue implementation
wycats authored
209 end
210
8496be7 @guilleiguaran Fixing Rails::Server#app under Rack::URLMap
guilleiguaran authored
211 def to_app
212 self
213 end
214
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
215 def helpers_paths #:nodoc:
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
216 config.helpers_paths
217 end
218
482ec2a @drogus Add ORIGINAL_FULLPATH to env
drogus authored
219 def call(env)
220 env["ORIGINAL_FULLPATH"] = build_original_fullpath(env)
221 super(env)
222 end
223
2fe70c1 @josevalim Booya, sprockets now works from Engines.
josevalim authored
224 protected
225
226 alias :build_middleware_stack :app
612454e @josh Move sprockets initializers back to application
josh authored
227
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
228 def reload_dependencies?
229 config.reload_classes_only_on_change != true || reloaders.map(&:updated?).any?
230 end
231
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
232 def default_middleware_stack
233 ActionDispatch::MiddlewareStack.new.tap do |middleware|
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
234 if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
235 require "action_dispatch/http/rack_cache"
236 middleware.use ::Rack::Cache, rack_cache
237 end
254ab7d @wycats First pass at Rack::Cache
wycats authored
238
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
239 if config.force_ssl
9ec63eb @rafaelfranca Rack::SSL -> ActionDispatch::SSL
rafaelfranca authored
240 middleware.use ::ActionDispatch::SSL, config.ssl_options
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
241 end
eeb3246 Only add Rack::Cache to the middleware stack if config.action_control…
Sparky authored
242
55dd060 @drogus Handle files from ActionDispatch::Static with Rack::Sendfile (fixes #…
drogus authored
243 if config.action_dispatch.x_sendfile_header.present?
244 middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
245 end
246
e6369bc @drogus Application's assets should have higher priority than engine's ones
drogus authored
247 if config.serve_static_assets
1ca8123 @josevalim Add static_cache_control as configuration option and set a default fo…
josevalim authored
248 middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
e6369bc @drogus Application's assets should have higher priority than engine's ones
drogus authored
249 end
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
250
ea14396 @neerajdotname replace if ! with unless
neerajdotname authored
251 middleware.use ::Rack::Lock unless config.allow_concurrency
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
252 middleware.use ::Rack::Runtime
e1824c5 @josevalim Remove remaining http_only? calls.
josevalim authored
253 middleware.use ::Rack::MethodOverride
afde6fd @dhh Added X-Request-Id tracking and TaggedLogging to easily log that and …
dhh authored
254 middleware.use ::ActionDispatch::RequestId
c83d9a1 @josevalim Unify logger and taggedlogging middleware as both address logging con…
josevalim authored
255 middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
02127e6 @josevalim Allow a custom exceptions app to set.
josevalim authored
256 middleware.use ::ActionDispatch::ShowExceptions, config.exceptions_app || ActionDispatch::PublicExceptions.new(Rails.public_path)
750bb5c @josevalim Split ShowExceptions responsibilities in two middlewares.
josevalim authored
257 middleware.use ::ActionDispatch::DebugExceptions
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
258 middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
750bb5c @josevalim Split ShowExceptions responsibilities in two middlewares.
josevalim authored
259
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
260 unless config.cache_classes
261 app = self
262 middleware.use ::ActionDispatch::Reloader, lambda { app.reload_dependencies? }
263 end
264
435bccd Replace AD::Callbacks.to_prepare with AD::Reloader.to_prepare
John Firebaugh authored
265 middleware.use ::ActionDispatch::Callbacks
e1824c5 @josevalim Remove remaining http_only? calls.
josevalim authored
266 middleware.use ::ActionDispatch::Cookies
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
267
e1824c5 @josevalim Remove remaining http_only? calls.
josevalim authored
268 if config.session_store
d6933a1 @josevalim config.force_ssl should mark the session as secure.
josevalim authored
269 if config.force_ssl && !config.session_options.key?(:secure)
270 config.session_options[:secure] = true
271 end
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
272 middleware.use config.session_store, config.session_options
273 middleware.use ::ActionDispatch::Flash
274 end
275
276 middleware.use ::ActionDispatch::ParamsParser
277 middleware.use ::ActionDispatch::Head
74dd8a3 @josevalim Move ETag and ConditionalGet logic from AD::Response to the middlewar…
josevalim authored
278 middleware.use ::Rack::ConditionalGet
279 middleware.use ::Rack::ETag, "no-cache"
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
280
e1824c5 @josevalim Remove remaining http_only? calls.
josevalim authored
281 if config.action_dispatch.best_standards_support
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
282 middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support
283 end
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
284 end
285 end
286
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
287 def initialize_tasks #:nodoc:
e290835 @bilts Make Rake::DSL methods available within Railtie rake_tasks block
bilts authored
288 self.class.rake_tasks do
289 require "rails/tasks"
290 task :environment do
291 $rails_rake_task = true
292 require_environment!
293 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
294 end
295 end
296
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
297 def initialize_console #:nodoc:
d54c778 @amatsuda always enable `pp` on the Rails console
amatsuda authored
298 require "pp"
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored
299 require "rails/console/app"
300 require "rails/console/helpers"
301 end
482ec2a @drogus Add ORIGINAL_FULLPATH to env
drogus authored
302
303 def build_original_fullpath(env)
2d62334 @ndbroadbent Reverted build_original_fullpath method, as per discussion at https:/…
ndbroadbent authored
304 path_info = env["PATH_INFO"]
305 query_string = env["QUERY_STRING"]
306 script_name = env["SCRIPT_NAME"]
307
308 if query_string.present?
309 "#{script_name}#{path_info}?#{query_string}"
310 else
311 "#{script_name}#{path_info}"
312 end
482ec2a @drogus Add ORIGINAL_FULLPATH to env
drogus authored
313 end
accd9b4 @josh Restore "Start Rails::Application object
josh authored
314 end
17a91a6 @dlee Logs should show overridden method; Issue 426
dlee authored
315 end
Something went wrong with that request. Please try again.