Class: Rails::Application

Inherits:
Engine show all
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/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.

Defined Under Namespace

Modules: Bootstrap, Finisher Classes: Configuration, Railties, RoutesReloader

Constant Summary

Constants inherited from Railtie

Railtie::ABSTRACT_RAILTIES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Engine

#app, #call, #eager_load!, endpoint, #endpoint, find, #helpers, isolate_namespace, #load_seed, #railties, #routes

Methods inherited from Railtie

abstract_railtie?, console, #eager_load!, generators, railtie_name, rake_tasks, subclasses

Methods included from Initializable

included, #run_initializers

Constructor Details

#initializeApplication

Returns a new instance of Application.



58
59
60
61
# File 'railties/lib/rails/application.rb', line 58

def initialize
  super
  @initialized = false
end

Instance Attribute Details

#assetsObject

Returns the value of attribute assets



53
54
55
# File 'railties/lib/rails/application.rb', line 53

def assets
  @assets
end

#sandboxObject Also known as: sandbox?

Returns the value of attribute sandbox



53
54
55
# File 'railties/lib/rails/application.rb', line 53

def sandbox
  @sandbox
end

Class Method Details

.inherited(base) ⇒ Object



44
45
46
47
48
49
50
# File 'railties/lib/rails/application.rb', line 44

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.



76
77
78
79
# File 'railties/lib/rails/application.rb', line 76

def add_lib_to_load_path! #:nodoc:
  path = config.root.join('lib').to_s
  $LOAD_PATH.unshift(path) if File.exists?(path)
end

#configObject



133
134
135
# File 'railties/lib/rails/application.rb', line 133

def config
  @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
end

#env_configObject



119
120
121
122
123
124
125
# File 'railties/lib/rails/application.rb', line 119

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
  })
end

#initialize!(group = :default) ⇒ Object



94
95
96
97
98
99
# File 'railties/lib/rails/application.rb', line 94

def initialize!(group=:default)
  raise "Application has been already initialized." if @initialized
  run_initializers(group, self)
  @initialized = true
  self
end

#initializersObject



127
128
129
130
131
# File 'railties/lib/rails/application.rb', line 127

def initializers
  Bootstrap.initializers_for(self) +
  super +
  Finisher.initializers_for(self)
end

#load_console(app = self) ⇒ Object



113
114
115
116
117
# File 'railties/lib/rails/application.rb', line 113

def load_console(app=self)
  initialize_console
  super
  self
end

#load_generators(app = self) ⇒ Object



107
108
109
110
111
# File 'railties/lib/rails/application.rb', line 107

def load_generators(app=self)
  initialize_generators
  super
  self
end

#load_tasks(app = self) ⇒ Object



101
102
103
104
105
# File 'railties/lib/rails/application.rb', line 101

def load_tasks(app=self)
  initialize_tasks
  super
  self
end

#reload_routes!Object



86
87
88
# File 'railties/lib/rails/application.rb', line 86

def reload_routes!
  routes_reloader.reload!
end

#require_environment!Object

:nodoc:



81
82
83
84
# File 'railties/lib/rails/application.rb', line 81

def require_environment! #:nodoc:
  environment = paths["config/environment"].existent.first
  require environment if environment
end

#routes_reloaderObject



90
91
92
# File 'railties/lib/rails/application.rb', line 90

def routes_reloader
  @routes_reloader ||= RoutesReloader.new
end

#to_appObject



137
138
139
# File 'railties/lib/rails/application.rb', line 137

def to_app
  self
end