Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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