Skip to content
Newer
Older
100644 227 lines (195 sloc) 6.96 KB
6690d66 @josevalim Rename config.cookie_secret to config.secret_token and pass it as con…
josevalim authored Apr 5, 2010
1 require 'active_support/core_ext/hash/reverse_merge'
772c2b0 @josevalim Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
josevalim authored Jun 20, 2010
2 require 'active_support/file_update_checker'
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored Jan 22, 2010
3 require 'fileutils'
b17e358 @josevalim Move configuration to subfolders.
josevalim authored Jan 23, 2010
4 require 'rails/plugin'
2fde9d7 @josevalim Solve some pendencies.
josevalim authored Jan 24, 2010
5 require 'rails/engine'
dc677f7 tests pass with requiring the frameworks in rails.rb
Carlhuda authored Dec 22, 2009
6
accd9b4 @josh Restore "Start Rails::Application object
josh authored Sep 25, 2009
7 module Rails
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
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 case…
wycats authored May 15, 2010
16 # Rails::Application.config or YourApplication::Application.config, you are actually
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
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 May 29, 2010
31 # "logger", "reload_engines", "reload_plugins" and so forth.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
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 May 29, 2010
40 # == Middlewares
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
41 #
45e6028 @wycats Removing Metal from Rails 3.
wycats authored May 29, 2010
42 # The Application is also responsible for building the middleware stack.
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored May 15, 2010
43 #
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
44 class Application < Engine
4f03603 @josevalim Break application.rb file in smaller chunks.
josevalim authored Jan 23, 2010
45 autoload :Bootstrap, 'rails/application/bootstrap'
f8bf198 @josevalim Add tests for explicit engines.
josevalim authored Jan 26, 2010
46 autoload :Configurable, 'rails/application/configurable'
b17e358 @josevalim Move configuration to subfolders.
josevalim authored Jan 23, 2010
47 autoload :Configuration, 'rails/application/configuration'
4f03603 @josevalim Break application.rb file in smaller chunks.
josevalim authored Jan 23, 2010
48 autoload :Finisher, 'rails/application/finisher'
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
49 autoload :Railties, 'rails/application/railties'
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored Jan 23, 2010
50
e4d7e50 Moving more initializers into the application object
Carl Lerche authored Oct 8, 2009
51 class << self
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 23, 2009
52 private :new
79b0b50 @wycats Aliasing class_eval makes JRuby ornery.
wycats authored Mar 7, 2010
53
54 def configure(&block)
55 class_eval(&block)
56 end
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored Jan 23, 2010
57
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
58 def instance
db99324 @josevalim Ensure calling a method in Rails::Application does not instantiate a …
josevalim authored Jan 26, 2010
59 if self == Rails::Application
60 Rails.application
9212652 @josh Add Rails::Application pointer to the default app to add symmetry to …
josh authored Jan 24, 2010
61 else
db99324 @josevalim Ensure calling a method in Rails::Application does not instantiate a …
josevalim authored Jan 26, 2010
62 @@instance ||= new
9212652 @josh Add Rails::Application pointer to the default app to add symmetry to …
josh authored Jan 24, 2010
63 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 2, 2009
64 end
65
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
66 def inherited(base)
2fde9d7 @josevalim Solve some pendencies.
josevalim authored Jan 24, 2010
67 raise "You cannot have more than one Rails::Application" if Rails.application
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
68 super
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
69 Rails.application = base.instance
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored Jun 22, 2010
70 Rails.application.add_lib_to_load_path!
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored May 15, 2010
71 ActiveSupport.run_load_hooks(:before_configuration, base.instance)
e4d7e50 Moving more initializers into the application object
Carl Lerche authored Oct 8, 2009
72 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored Sep 28, 2009
73
e7418ab @josevalim Add more tests to some key points in Railties.
josevalim authored Jan 27, 2010
74 def respond_to?(*args)
75 super || instance.respond_to?(*args)
76 end
77
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored Jan 23, 2010
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 Dec 21, 2009
82 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
83 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored Sep 29, 2009
84
45e6028 @wycats Removing Metal from Rails 3.
wycats authored May 29, 2010
85 delegate :middleware, :to => :config
395d664 @josevalim Move application configuration to the application configuration objec…
josevalim authored Mar 26, 2010
86
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored Jun 20, 2010
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 an…
fxn authored Jun 22, 2010
100 def add_lib_to_load_path! #:nodoc:
5a0d73f @josevalim Add lib to load paths when application is inherited to be able to loa…
josevalim authored Jun 2, 2010
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 Jun 20, 2010
105 def require_environment! #:nodoc:
1b816d5 @josevalim The rake task :environment now loads config/environment.rb instead of…
josevalim authored Apr 29, 2010
106 environment = paths.config.environment.to_a.first
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
107 require environment if environment
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored Dec 21, 2009
108 end
109
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored Jun 20, 2010
110 def eager_load! #:nodoc:
5a0d73f @josevalim Add lib to load paths when application is inherited to be able to loa…
josevalim authored Jun 1, 2010
111 railties.all(&:eager_load!)
112 super
113 end
114
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored Jan 23, 2010
115 def routes
9a5be2e Get Railties tests passing
Carlhuda authored Feb 24, 2010
116 @routes ||= ActionDispatch::Routing::RouteSet.new
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
117 end
118
119 def railties
120 @railties ||= Railties.new(config)
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
121 end
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
122
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored Jan 23, 2010
123 def routes_reloader
772c2b0 @josevalim Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
josevalim authored Jun 20, 2010
124 @routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! }
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
125 end
126
127 def reload_routes!
772c2b0 @josevalim Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
josevalim authored Jun 20, 2010
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 t…
josevalim authored Jan 23, 2010
136 end
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
137
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored Jan 23, 2010
138 def initialize!
139 run_initializers(self)
140 self
5f8e48c @josh Move route reloading into railties
josh authored Dec 14, 2009
141 end
142
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
143 def load_tasks
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
144 initialize_tasks
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
145 railties.all { |r| r.load_tasks }
e461e1b @josevalim Ensure application rake tasks and generators are loaded after the one…
josevalim authored Apr 26, 2010
146 super
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
147 self
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored Dec 24, 2009
148 end
149
4ca9765 @josevalim Allow railties to specify generators paths.
josevalim authored Jan 19, 2010
150 def load_generators
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
151 initialize_generators
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
152 railties.all { |r| r.load_generators }
e461e1b @josevalim Ensure application rake tasks and generators are loaded after the one…
josevalim authored Apr 26, 2010
153 super
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
154 self
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored Nov 6, 2009
155 end
156
603d4fb @josh Build middleware stack and reload routes after app initializers run
josh authored Jan 16, 2010
157 def app
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored May 29, 2010
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 Jan 16, 2010
162 end
f816666 @josevalim Alias app to build_middleware_stack for clarity.
josevalim authored Jun 21, 2010
163 alias :build_middleware_stack :app
603d4fb @josh Build middleware stack and reload routes after app initializers run
josh authored Jan 16, 2010
164
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored Nov 3, 2009
165 def call(env)
6690d66 @josevalim Rename config.cookie_secret to config.secret_token and pass it as con…
josevalim authored Apr 5, 2010
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 Oct 7, 2009
174 end
175
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored Jan 23, 2010
176 def initializers
5d07869 @josevalim Ensure all initializers are collections.
josevalim authored Jan 26, 2010
177 initializers = Bootstrap.initializers_for(self)
13d66cd @josevalim Extract Railtie load from application.
josevalim authored Jan 23, 2010
178 railties.all { |r| initializers += r.initializers }
2d1f9fb @josh Plugins need to load before app initializers
josh authored Jan 25, 2010
179 initializers += super
5d07869 @josevalim Ensure all initializers are collections.
josevalim authored Jan 25, 2010
180 initializers += Finisher.initializers_for(self)
9f7874a @jeremy Move Collection responsibility from application to initializable
jeremy authored Jun 24, 2010
181 initializers
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored Jan 23, 2010
182 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
183
184 protected
185
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored May 29, 2010
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 di…
josevalim authored Jan 23, 2010
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…
josevalim authored Apr 29, 2010
213 require_environment!
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
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 Sep 26, 2009
225 end
226 end
Something went wrong with that request. Please try again.