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
- B
- C
- D
- E
- I
- L
- N
- R
- T
- W
- MODULE Rails::Application::Bootstrap
- MODULE Rails::Application::Finisher
- CLASS Rails::Application::Configuration
- CLASS Rails::Application::Railties
- CLASS Rails::Application::RouteInspector
- CLASS Rails::Application::RoutesReloader
[RW] | assets | |
[RW] | sandbox | |
[R] | reloaders |
# 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
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 the application console and invoke the registered hooks. Check Rails::Railtie.console for more info.
Load the application and its railties tasks and invoke the registered hooks. Check Rails::Railtie.rake_tasks for more info.
Reload application routes regardless if they changed or not.
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
# 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
# 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