Class: Rails::Application

Inherits:
Engine show all
Defined in:
lib/rails/application.rb,
lib/rails/application/finisher.rb,
lib/rails/application/railties.rb,
lib/rails/application/bootstrap.rb,
lib/rails/application/configurable.rb,
lib/rails/application/configuration.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. Besides, it also executed 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, Configurable, Finisher Classes: Configuration, Railties

Constant Summary

Constants inherited from Railtie

Railtie::ABSTRACT_RAILTIES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Engine

find_root_with_flag

Methods inherited from Railtie

abstract_railtie?, console, generators, log_subscriber, railtie_name, rake_tasks, subclasses

Methods included from Initializable

included, #run_initializers

Class Method Details

.configure(&block) ⇒ Object



46
47
48
# File 'lib/rails/application.rb', line 46

def configure(&block)
  class_eval(&block)
end

.inherited(base) ⇒ Object



62
63
64
65
66
67
68
# File '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

.instanceObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails/application.rb', line 50

def instance
  if self == Rails::Application
    if Rails.application
      ActiveSupport::Deprecation.warn "Calling a method in Rails::Application is deprecated, " <<
        "please call it directly in your application constant #{Rails.application.class.name}.", caller
    end
    Rails.application
  else
    @@instance ||= new
  end
end

.respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rails/application.rb', line 70

def respond_to?(*args)
  super || instance.respond_to?(*args)
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 '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

#appObject Also known as: build_middleware_stack



159
160
161
162
163
164
# File 'lib/rails/application.rb', line 159

def app
  @app ||= begin
    config.middleware = config.middleware.merge_into(default_middleware_stack)
    config.middleware.build(routes)
  end
end

#call(env) ⇒ Object



167
168
169
# File 'lib/rails/application.rb', line 167

def call(env)
  app.call(env.reverse_merge!(env_defaults))
end

#eager_load!Object

:nodoc:



106
107
108
109
# File 'lib/rails/application.rb', line 106

def eager_load! #:nodoc:
  railties.all(&:eager_load!)
  super
end

#env_defaultsObject



171
172
173
174
175
176
# File 'lib/rails/application.rb', line 171

def env_defaults
  @env_defaults ||= {
    "action_dispatch.parameter_filter" => config.filter_parameters,
    "action_dispatch.secret_token" => config.secret_token
  }
end

#initialize!Object



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

def initialize!
  run_initializers(self)
  self
end

#initializersObject



178
179
180
181
182
183
184
# File 'lib/rails/application.rb', line 178

def initializers
  initializers = Bootstrap.initializers_for(self)
  railties.all { |r| initializers += r.initializers }
  initializers += super
  initializers += Finisher.initializers_for(self)
  initializers
end

#load_console(sandbox = false) ⇒ Object



152
153
154
155
156
157
# File 'lib/rails/application.rb', line 152

def load_console(sandbox=false)
  initialize_console(sandbox)
  railties.all { |r| r.load_console }
  super()
  self
end

#load_generatorsObject



145
146
147
148
149
150
# File 'lib/rails/application.rb', line 145

def load_generators
  initialize_generators
  railties.all { |r| r.load_generators }
  super
  self
end

#load_tasksObject



138
139
140
141
142
143
# File 'lib/rails/application.rb', line 138

def load_tasks
  initialize_tasks
  railties.all { |r| r.load_tasks }
  super
  self
end

#railtiesObject



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

def railties
  @railties ||= Railties.new(config)
end

#reload_routes!Object



123
124
125
126
127
128
129
130
131
# File 'lib/rails/application.rb', line 123

def reload_routes!
  _routes = self.routes
  _routes.disable_clear_and_finalize = true
  _routes.clear!
  routes_reloader.paths.each { |path| load(path) }
  ActiveSupport.on_load(:action_controller) { _routes.finalize! }
ensure
  _routes.disable_clear_and_finalize = false
end

#require_environment!Object

:nodoc:



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

def require_environment! #:nodoc:
  environment = paths.config.environment.to_a.first
  require environment if environment
end

#routesObject



111
112
113
# File 'lib/rails/application.rb', line 111

def routes
  @routes ||= ActionDispatch::Routing::RouteSet.new
end

#routes_reloaderObject



119
120
121
# File 'lib/rails/application.rb', line 119

def routes_reloader
  @routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! }
end