In Rails 3.0, a Rails::Application object was introduced which is nothing more than an Engine but with the responsibility of coordinating the whole boot process.

Initialization

Rails::Application is responsible for executing all railties, engines and plugin initializers. It also executes some bootstrap initializers (check Rails::Application::Bootstrap) and finishing initializers, after all the others are executed (check Rails::Application::Finisher).

Configuration

Besides providing the same configuration as Rails::Engine and Rails::Railtie, the application object has several specific configurations, for example “allow_concurrency“, “cache_classes“, “consider_all_requests_local“, “filter_parameters“, “logger”, “reload_plugins“ and so forth.

Check Rails::Application::Configuration to see them all.

Routes

The application object is also responsible for holding the routes and reloading routes whenever the files change in development.

Middlewares

The Application is also responsible for building the middleware stack.

Booting process

The application is also responsible for setting up and executing the booting process. From the moment you require “config/application.rb“ in your app, the booting process goes like this:

  1)  require "config/boot.rb" to setup load paths
  2)  require railties and engines
  3)  Define Rails.application as "class MyApp::Application < Rails::Application"
  4)  Run config.before_configuration callbacks
  5)  Load config/environments/ENV.rb
  6)  Run config.before_initialize callbacks
  7)  Run Railtie#initializer defined by railties, engines and application.
      One by one, each engine sets up its load paths, routes and runs its config/initializers/* files.
  9)  Custom Railtie#initializers added by railties, engines and applications are executed
  10) Build the middleware stack and run to_prepare callbacks
  11) Run config.before_eager_load and eager_load if cache classes is true
  12) Run config.after_initialize callbacks
Methods
B
C
D
E
I
L
N
R
T
W
Classes and Modules
Attributes
[RW] assets
[RW] sandbox
[R] reloaders
Class Public methods
inherited(base)
    # File railties/lib/rails/application.rb, line 62
62:       def inherited(base)
63:         raise "You cannot have more than one Rails::Application" if Rails.application
64:         super
65:         Rails.application = base.instance
66:         Rails.application.add_lib_to_load_path!
67:         ActiveSupport.run_load_hooks(:before_configuration, base.instance)
68:       end
new()
    # File railties/lib/rails/application.rb, line 77
77:     def initialize
78:       super
79:       @initialized = false
80:       @reloaders   = []
81:     end
Instance Public methods
call(env)
     # File railties/lib/rails/application.rb, line 218
218:     def call(env)
219:       env["ORIGINAL_FULLPATH"] = build_original_fullpath(env)
220:       super(env)
221:     end
env_config()

Rails.application.env_config stores some of the Rails initial environment parameters. Currently stores:

  * action_dispatch.parameter_filter" => config.filter_parameters,
  * action_dispatch.secret_token"     => config.secret_token,
  * action_dispatch.show_exceptions"  => config.action_dispatch.show_exceptions

These parameters will be used by middlewares and engines to configure themselves.

     # File railties/lib/rails/application.rb, line 166
166:     def env_config
167:       @env_config ||= super.merge({
168:         "action_dispatch.parameter_filter" => config.filter_parameters,
169:         "action_dispatch.secret_token" => config.secret_token,
170:         "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
171:         "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
172:         "action_dispatch.logger" => Rails.logger,
173:         "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner
174:       })
175:     end
load_console(app=self)

Load the application console and invoke the registered hooks. Check Rails::Railtie.console for more info.

     # File railties/lib/rails/application.rb, line 151
151:     def load_console(app=self)
152:       initialize_console
153:       super
154:       self
155:     end
load_tasks(app=self)

Load the application and its railties tasks and invoke the registered hooks. Check Rails::Railtie.rake_tasks for more info.

     # File railties/lib/rails/application.rb, line 143
143:     def load_tasks(app=self)
144:       initialize_tasks
145:       super
146:       self
147:     end
reload_routes!()

Reload application routes regardless if they changed or not.

     # File railties/lib/rails/application.rb, line 107
107:     def reload_routes!
108:       routes_reloader.reload!
109:     end
to_app()
     # File railties/lib/rails/application.rb, line 210
210:     def to_app
211:       self
212:     end
watchable_args()

Returns an array of file paths appended with a hash of directories-extensions suitable for ActiveSupport::FileUpdateChecker API.

     # File railties/lib/rails/application.rb, line 117
117:     def watchable_args
118:       files = []
119:       files.concat config.watchable_files
120: 
121:       dirs = {}
122:       dirs.merge! config.watchable_dirs
123:       ActiveSupport::Dependencies.autoload_paths.each do |path|
124:         dirs[path.to_s] = [:rb]
125:       end
126: 
127:       [files, dirs]
128:     end
Instance Protected methods
build_original_fullpath(env)
     # File railties/lib/rails/application.rb, line 303
303:     def build_original_fullpath(env)
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
313:     end
default_middleware_stack()
     # File railties/lib/rails/application.rb, line 231
231:     def default_middleware_stack
232:       ActionDispatch::MiddlewareStack.new.tap do |middleware|
233:         if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
234:           require "action_dispatch/http/rack_cache"
235:           middleware.use ::Rack::Cache, rack_cache
236:         end
237: 
238:         if config.force_ssl
239:           require "rack/ssl"
240:           middleware.use ::Rack::SSL, config.ssl_options
241:         end
242: 
243:         if config.serve_static_assets
244:           middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
245:         end
246: 
247:         middleware.use ::Rack::Lock unless config.allow_concurrency
248:         middleware.use ::Rack::Runtime
249:         middleware.use ::Rack::MethodOverride
250:         middleware.use ::ActionDispatch::RequestId
251:         middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
252:         middleware.use ::ActionDispatch::ShowExceptions, config.exceptions_app || ActionDispatch::PublicExceptions.new(Rails.public_path)
253:         middleware.use ::ActionDispatch::DebugExceptions
254:         middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
255: 
256:         if config.action_dispatch.x_sendfile_header.present?
257:           middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
258:         end
259: 
260:         unless config.cache_classes
261:           app = self
262:           middleware.use ::ActionDispatch::Reloader, lambda { app.reload_dependencies? }
263:         end
264: 
265:         middleware.use ::ActionDispatch::Callbacks
266:         middleware.use ::ActionDispatch::Cookies
267: 
268:         if config.session_store
269:           if config.force_ssl && !config.session_options.key?(:secure)
270:             config.session_options[:secure] = true
271:           end
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
278:         middleware.use ::Rack::ConditionalGet
279:         middleware.use ::Rack::ETag, "no-cache"
280: 
281:         if config.action_dispatch.best_standards_support
282:           middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support
283:         end
284:       end
285:     end
reload_dependencies?()
     # File railties/lib/rails/application.rb, line 227
227:     def reload_dependencies?
228:       config.reload_classes_only_on_change != true || reloaders.map(&:updated?).any?
229:     end