Class: Rails::Application
- Defined in:
- railties/lib/rails/application.rb,
railties/lib/rails/application/finisher.rb,
railties/lib/rails/application/railties.rb,
railties/lib/rails/application/bootstrap.rb,
railties/lib/rails/application/configuration.rb,
railties/lib/rails/application/route_inspector.rb,
railties/lib/rails/application/routes_reloader.rb
Overview
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
Defined Under Namespace
Modules: Bootstrap, Finisher Classes: Configuration, Railties, RouteInspector, RoutesReloader
Constant Summary
Constants inherited from Railtie
Instance Attribute Summary collapse
-
#assets ⇒ Object
Returns the value of attribute assets.
-
#reloaders ⇒ Object
readonly
Returns the value of attribute reloaders.
-
#sandbox ⇒ Object
(also: #sandbox?)
Returns the value of attribute sandbox.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_lib_to_load_path! ⇒ Object
This method is called just after an application inherits from Rails::Application, allowing the developer to load classes in lib and use them during application configuration.
- #call(env) ⇒ Object
-
#config ⇒ Object
:nodoc:.
-
#env_config ⇒ Object
Rails.application.env_config stores some of the Rails initial environment parameters.
-
#helpers_paths ⇒ Object
:nodoc:.
-
#initialize ⇒ Application
constructor
A new instance of Application.
-
#initialize!(group = :default) ⇒ Object
Initialize the application passing the given group.
-
#initializers ⇒ Object
:nodoc:.
-
#load_console(app = self) ⇒ Object
Load the application console and invoke the registered hooks.
-
#load_tasks(app = self) ⇒ Object
Load the application and its railties tasks and invoke the registered hooks.
-
#ordered_railties ⇒ Object
Returns the ordered railties for this application considering railties_order.
-
#reload_routes! ⇒ Object
Reload application routes regardless if they changed or not.
-
#require_environment! ⇒ Object
:nodoc:.
-
#routes_reloader ⇒ Object
:nodoc:.
- #to_app ⇒ Object
-
#watchable_args ⇒ Object
Returns an array of file paths appended with a hash of directories-extensions suitable for ActiveSupport::FileUpdateChecker API.
Methods inherited from Engine
#app, #eager_load!, endpoint, #endpoint, find, #helpers, isolate_namespace, #load_generators, #load_seed, #railties, #routes, #routes_url_helpers
Methods inherited from Railtie
abstract_railtie?, console, #eager_load!, generators, #load_generators, railtie_name, #railtie_namespace, rake_tasks, subclasses
Methods included from Initializable
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
77 78 79 80 81 |
# File 'railties/lib/rails/application.rb', line 77 def initialize super @initialized = false @reloaders = [] end |
Instance Attribute Details
#assets ⇒ Object
Returns the value of attribute assets
71 72 73 |
# File 'railties/lib/rails/application.rb', line 71 def assets @assets end |
#reloaders ⇒ Object (readonly)
Returns the value of attribute reloaders
73 74 75 |
# File 'railties/lib/rails/application.rb', line 73 def reloaders @reloaders end |
#sandbox ⇒ Object Also known as: sandbox?
Returns the value of attribute sandbox
71 72 73 |
# File 'railties/lib/rails/application.rb', line 71 def sandbox @sandbox end |
Class Method Details
.inherited(base) ⇒ Object
62 63 64 65 66 67 68 |
# File 'railties/lib/rails/application.rb', line 62 def inherited(base) raise "You cannot have more than one Rails::Application" if Rails.application super Rails.application = base.instance Rails.application.add_lib_to_load_path! ActiveSupport.run_load_hooks(:before_configuration, base.instance) end |
Instance Method Details
#add_lib_to_load_path! ⇒ Object
This method is called just after an application inherits from Rails::Application, allowing the developer to load classes in lib and use them during application configuration.
class MyApplication < Rails::Application
require "my_backend" # in lib/my_backend
config.i18n.backend = MyBackend
end
Notice this method takes into consideration the default root path. So if you are changing config.root inside your application definition or having a custom Rails application, you will need to add lib to $LOAD_PATH on your own in case you need to load files in lib/ during the application configuration as well.
96 97 98 99 |
# File 'railties/lib/rails/application.rb', line 96 def add_lib_to_load_path! #:nodoc: path = config.root.join('lib').to_s $LOAD_PATH.unshift(path) if File.exists?(path) end |
#call(env) ⇒ Object
221 222 223 224 |
# File 'railties/lib/rails/application.rb', line 221 def call(env) env["ORIGINAL_FULLPATH"] = build_original_fullpath(env) super(env) end |
#config ⇒ Object
:nodoc:
209 210 211 |
# File 'railties/lib/rails/application.rb', line 209 def config #:nodoc: @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd)) end |
#env_config ⇒ Object
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,
* "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
* "action_dispatch.logger" => Rails.logger,
* "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner
These parameters will be used by middlewares and engines to configure themselves.
169 170 171 172 173 174 175 176 177 178 |
# File 'railties/lib/rails/application.rb', line 169 def env_config @env_config ||= super.merge({ "action_dispatch.parameter_filter" => config.filter_parameters, "action_dispatch.secret_token" => config.secret_token, "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions, "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local, "action_dispatch.logger" => Rails.logger, "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner }) end |
#helpers_paths ⇒ Object
:nodoc:
217 218 219 |
# File 'railties/lib/rails/application.rb', line 217 def helpers_paths #:nodoc: config.helpers_paths end |
#initialize!(group = :default) ⇒ Object
Initialize the application passing the given group. By default, the group is :default but sprockets precompilation passes group equals to assets if initialize_on_precompile is false to avoid booting the whole app.
134 135 136 137 138 139 |
# File 'railties/lib/rails/application.rb', line 134 def initialize!(group=:default) #:nodoc: raise "Application has been already initialized." if @initialized run_initializers(group, self) @initialized = true self end |
#initializers ⇒ Object
:nodoc:
203 204 205 206 207 |
# File 'railties/lib/rails/application.rb', line 203 def initializers #:nodoc: Bootstrap.initializers_for(self) + super + Finisher.initializers_for(self) end |
#load_console(app = self) ⇒ Object
Load the application console and invoke the registered hooks. Check Rails::Railtie.console
for more info.
151 152 153 154 155 |
# File 'railties/lib/rails/application.rb', line 151 def load_console(app=self) initialize_console super self end |
#load_tasks(app = self) ⇒ Object
Load the application and its railties tasks and invoke the registered hooks. Check Rails::Railtie.rake_tasks
for more info.
143 144 145 146 147 |
# File 'railties/lib/rails/application.rb', line 143 def load_tasks(app=self) initialize_tasks super self end |
#ordered_railties ⇒ Object
Returns the ordered railties for this application considering railties_order.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'railties/lib/rails/application.rb', line 181 def ordered_railties #:nodoc: @ordered_railties ||= begin order = config.railties_order.map do |railtie| if railtie == :main_app self elsif railtie.respond_to?(:instance) railtie.instance else railtie end end all = (railties.all - order) all.push(self) unless (all + order).include?(self) order.push(:all) unless order.include?(:all) index = order.index(:all) order[index] = all order.reverse.flatten end end |
#reload_routes! ⇒ Object
Reload application routes regardless if they changed or not.
107 108 109 |
# File 'railties/lib/rails/application.rb', line 107 def reload_routes! routes_reloader.reload! end |
#require_environment! ⇒ Object
:nodoc:
101 102 103 104 |
# File 'railties/lib/rails/application.rb', line 101 def require_environment! #:nodoc: environment = paths["config/environment"].existent.first require environment if environment end |
#routes_reloader ⇒ Object
:nodoc:
111 112 113 |
# File 'railties/lib/rails/application.rb', line 111 def routes_reloader #:nodoc: @routes_reloader ||= RoutesReloader.new end |
#to_app ⇒ Object
213 214 215 |
# File 'railties/lib/rails/application.rb', line 213 def to_app self end |
#watchable_args ⇒ Object
Returns an array of file paths appended with a hash of directories-extensions suitable for ActiveSupport::FileUpdateChecker API.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'railties/lib/rails/application.rb', line 117 def watchable_args files = [] files.concat config.watchable_files dirs = {} dirs.merge! config.watchable_dirs ActiveSupport::Dependencies.autoload_paths.each do |path| dirs[path.to_s] = [:rb] end [files, dirs] end |