Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 206 lines (175 sloc) 6.083 kb
6690d66 José Valim Rename config.cookie_secret to config.secret_token and pass it as config...
josevalim authored
1 require 'active_support/core_ext/hash/reverse_merge'
02c5137 José Valim Add view paths to Engine setup.
josevalim authored
2 require 'fileutils'
b17e358 José Valim Move configuration to subfolders.
josevalim authored
3 require 'rails/plugin'
2fde9d7 José Valim Solve some pendencies.
josevalim authored
4 require 'rails/engine'
dc677f7 tests pass with requiring the frameworks in rails.rb
Carlhuda authored
5
accd9b4 Joshua Peek Restore "Start Rails::Application object
josh authored
6 module Rails
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
7 # In Rails 3.0, a Rails::Application object was introduced which is nothing more than
8 # an Engine but with the responsibility of coordinating the whole boot process.
9 #
10 # Opposite to Rails::Engine, you can only have one Rails::Application instance
11 # in your process and both Rails::Application and YourApplication::Application
12 # points to it.
13 #
14 # In other words, Rails::Application is Singleton and whenever you are accessing
9cfeefb Yehuda Katz Reorganized initializers a bit to enable better hooks for common cases w...
wycats authored
15 # Rails::Application.config or YourApplication::Application.config, you are actually
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
16 # accessing YourApplication::Application.instance.config.
17 #
18 # == Initialization
19 #
20 # Rails::Application is responsible for executing all railties, engines and plugin
21 # initializers. Besides, it also executed some bootstrap initializers (check
22 # Rails::Application::Bootstrap) and finishing initializers, after all the others
23 # are executed (check Rails::Application::Finisher).
24 #
25 # == Configuration
26 #
27 # Besides providing the same configuration as Rails::Engine and Rails::Railtie,
28 # the application object has several specific configurations, for example
29 # "allow_concurrency", "cache_classes", "consider_all_requests_local", "filter_parameters",
45e6028 Yehuda Katz Removing Metal from Rails 3.
wycats authored
30 # "logger", "reload_engines", "reload_plugins" and so forth.
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
31 #
32 # Check Rails::Application::Configuration to see them all.
33 #
34 # == Routes
35 #
36 # The application object is also responsible for holding the routes and reloading routes
37 # whenever the files change in development.
38 #
45e6028 Yehuda Katz Removing Metal from Rails 3.
wycats authored
39 # == Middlewares
781d0a9 José Valim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
40 #
45e6028 Yehuda Katz Removing Metal from Rails 3.
wycats authored
41 # The Application is also responsible for building the middleware stack.
9cfeefb Yehuda Katz Reorganized initializers a bit to enable better hooks for common cases w...
wycats authored
42 #
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
43 class Application < Engine
4f03603 José Valim Break application.rb file in smaller chunks.
josevalim authored
44 autoload :Bootstrap, 'rails/application/bootstrap'
f8bf198 José Valim Add tests for explicit engines.
josevalim authored
45 autoload :Configurable, 'rails/application/configurable'
b17e358 José Valim Move configuration to subfolders.
josevalim authored
46 autoload :Configuration, 'rails/application/configuration'
4f03603 José Valim Break application.rb file in smaller chunks.
josevalim authored
47 autoload :Finisher, 'rails/application/finisher'
13d66cd José Valim Extract Railtie load from application.
josevalim authored
48 autoload :Railties, 'rails/application/railties'
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
49 autoload :RoutesReloader, 'rails/application/routes_reloader'
50
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
51 class << self
94bb331 Jeremy Kemper Shift more responsibility from application class to its singleton instan...
jeremy authored
52 private :new
79b0b50 Yehuda Katz Aliasing class_eval makes JRuby ornery.
wycats authored
53
54 def configure(&block)
55 class_eval(&block)
56 end
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
57
94bb331 Jeremy Kemper Shift more responsibility from application class to its singleton instan...
jeremy authored
58 def instance
db99324 José Valim Ensure calling a method in Rails::Application does not instantiate a voi...
josevalim authored
59 if self == Rails::Application
60 Rails.application
9212652 Joshua Peek Add Rails::Application pointer to the default app to add symmetry to Foo...
josh authored
61 else
db99324 José Valim Ensure calling a method in Rails::Application does not instantiate a voi...
josevalim authored
62 @@instance ||= new
9212652 Joshua Peek Add Rails::Application pointer to the default app to add symmetry to Foo...
josh authored
63 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
64 end
65
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
66 def inherited(base)
2fde9d7 José Valim Solve some pendencies.
josevalim authored
67 raise "You cannot have more than one Rails::Application" if Rails.application
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
68 super
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
69 Rails.application = base.instance
5a0d73f José Valim Add lib to load paths when application is inherited to be able to load l...
josevalim authored
70 Rails.application.add_lib_to_load_paths!
9cfeefb Yehuda Katz Reorganized initializers a bit to enable better hooks for common cases w...
wycats authored
71 ActiveSupport.run_load_hooks(:before_configuration, base.instance)
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
72 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
73
e7418ab José Valim Add more tests to some key points in Railties.
josevalim authored
74 def respond_to?(*args)
75 super || instance.respond_to?(*args)
76 end
77
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
78 protected
79
80 def method_missing(*args, &block)
81 instance.send(*args, &block)
426348b Joshua Peek Update routes.rb template to use App name
josh authored
82 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
83 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
84
45e6028 Yehuda Katz Removing Metal from Rails 3.
wycats authored
85 delegate :middleware, :to => :config
395d664 José Valim Move application configuration to the application configuration object, ...
josevalim authored
86
5a0d73f José Valim Add lib to load paths when application is inherited to be able to load l...
josevalim authored
87 def add_lib_to_load_paths!
88 path = config.root.join('lib').to_s
89 $LOAD_PATH.unshift(path) if File.exists?(path)
90 end
91
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
92 def require_environment!
1b816d5 José Valim The rake task :environment now loads config/environment.rb instead of in...
josevalim authored
93 environment = paths.config.environment.to_a.first
13d66cd José Valim Extract Railtie load from application.
josevalim authored
94 require environment if environment
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored
95 end
96
5a0d73f José Valim Add lib to load paths when application is inherited to be able to load l...
josevalim authored
97 def eager_load!
98 railties.all(&:eager_load!)
99 super
100 end
101
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
102 def routes
9a5be2e Get Railties tests passing
Carlhuda authored
103 @routes ||= ActionDispatch::Routing::RouteSet.new
13d66cd José Valim Extract Railtie load from application.
josevalim authored
104 end
105
106 def railties
107 @railties ||= Railties.new(config)
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
108 end
5f8e48c Joshua Peek Move route reloading into railties
josh authored
109
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
110 def routes_reloader
226d8e7 José Valim Refactor MetalLoader and RoutesReloader to rely less on class configurat...
josevalim authored
111 @routes_reloader ||= RoutesReloader.new
5f8e48c Joshua Peek Move route reloading into railties
josh authored
112 end
113
114 def reload_routes!
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
115 routes_reloader.reload!
116 end
5f8e48c Joshua Peek Move route reloading into railties
josh authored
117
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
118 def initialize!
119 run_initializers(self)
120 self
5f8e48c Joshua Peek Move route reloading into railties
josh authored
121 end
122
94bb331 Jeremy Kemper Shift more responsibility from application class to its singleton instan...
jeremy authored
123 def load_tasks
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
124 initialize_tasks
13d66cd José Valim Extract Railtie load from application.
josevalim authored
125 railties.all { |r| r.load_tasks }
e461e1b José Valim Ensure application rake tasks and generators are loaded after the ones s...
josevalim authored
126 super
13d66cd José Valim Extract Railtie load from application.
josevalim authored
127 self
94bb331 Jeremy Kemper Shift more responsibility from application class to its singleton instan...
jeremy authored
128 end
129
4ca9765 José Valim Allow railties to specify generators paths.
josevalim authored
130 def load_generators
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
131 initialize_generators
13d66cd José Valim Extract Railtie load from application.
josevalim authored
132 railties.all { |r| r.load_generators }
e461e1b José Valim Ensure application rake tasks and generators are loaded after the ones s...
josevalim authored
133 super
13d66cd José Valim Extract Railtie load from application.
josevalim authored
134 self
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored
135 end
136
603d4fb Joshua Peek Build middleware stack and reload routes after app initializers run
josh authored
137 def app
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
138 @app ||= begin
139 config.middleware = config.middleware.merge_into(default_middleware_stack)
140 config.middleware.build(routes)
141 end
603d4fb Joshua Peek Build middleware stack and reload routes after app initializers run
josh authored
142 end
143
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
144 def call(env)
6690d66 José Valim Rename config.cookie_secret to config.secret_token and pass it as config...
josevalim authored
145 app.call(env.reverse_merge!(env_defaults))
146 end
147
148 def env_defaults
149 @env_defaults ||= {
150 "action_dispatch.parameter_filter" => config.filter_parameters,
151 "action_dispatch.secret_token" => config.secret_token
152 }
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
153 end
154
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
155 def initializers
5d07869 José Valim Ensure all initializers are collections.
josevalim authored
156 initializers = Bootstrap.initializers_for(self)
13d66cd José Valim Extract Railtie load from application.
josevalim authored
157 railties.all { |r| initializers += r.initializers }
2d1f9fb Joshua Peek Plugins need to load before app initializers
josh authored
158 initializers += super
5d07869 José Valim Ensure all initializers are collections.
josevalim authored
159 initializers += Finisher.initializers_for(self)
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
160 initializers
161 end
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
162
163 protected
164
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
165 def default_middleware_stack
166 ActionDispatch::MiddlewareStack.new.tap do |middleware|
167 middleware.use ::ActionDispatch::Static, paths.public.to_a.first if config.serve_static_assets
168 middleware.use ::Rack::Lock if !config.allow_concurrency
169 middleware.use ::Rack::Runtime
170 middleware.use ::Rails::Rack::Logger
171 middleware.use ::ActionDispatch::ShowExceptions, config.consider_all_requests_local if config.action_dispatch.show_exceptions
172 middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
173 middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
174 middleware.use ::ActionDispatch::Callbacks, !config.cache_classes
175 middleware.use ::ActionDispatch::Cookies
176
177 if config.session_store
178 middleware.use config.session_store, config.session_options
179 middleware.use ::ActionDispatch::Flash
180 end
181
182 middleware.use ::ActionDispatch::ParamsParser
183 middleware.use ::Rack::MethodOverride
184 middleware.use ::ActionDispatch::Head
185 end
186 end
187
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
188 def initialize_tasks
189 require "rails/tasks"
190 task :environment do
191 $rails_rake_task = true
1b816d5 José Valim The rake task :environment now loads config/environment.rb instead of in...
josevalim authored
192 require_environment!
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
193 end
194 end
195
196 def initialize_generators
197 require "rails/generators"
198 end
199
200 # Application is always reloadable when config.cache_classes is false.
201 def reloadable?(app)
202 true
203 end
accd9b4 Joshua Peek Restore "Start Rails::Application object
josh authored
204 end
205 end
Something went wrong with that request. Please try again.