Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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