Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 227 lines (197 sloc) 7.376 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'
b17e358 José Valim Move configuration to subfolders.
josevalim authored
38 autoload :Configuration, 'rails/application/configuration'
4f03603 José Valim Break application.rb file in smaller chunks.
josevalim authored
39 autoload :Finisher, 'rails/application/finisher'
13d66cd José Valim Extract Railtie load from application.
josevalim authored
40 autoload :Railties, 'rails/application/railties'
3049e64 Piotr Sarnacki Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
41 autoload :RoutesReloader, 'rails/application/routes_reloader'
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
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
44 def inherited(base)
2fde9d7 José Valim Solve some pendencies.
josevalim authored
45 raise "You cannot have more than one Rails::Application" if Rails.application
7fcf859 José Valim Massive cleanup in Railties and load stack.
josevalim authored
46 super
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
47 Rails.application = base.instance
6f83a50 Xavier Noria renames load_(once_)paths to autoload_(once_)paths in dependencies and c...
fxn authored
48 Rails.application.add_lib_to_load_path!
9cfeefb Yehuda Katz Reorganized initializers a bit to enable better hooks for common cases w...
wycats authored
49 ActiveSupport.run_load_hooks(:before_configuration, base.instance)
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
50 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
51 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
52
eedbf87 Piotr Sarnacki New way of generating urls for Application from Engine.
drogus authored
53 delegate :default_url_options, :default_url_options=, :to => :routes
54
9e081ca José Valim Improve documentation for add_lib_to_load_paths!
josevalim authored
55 # This method is called just after an application inherits from Rails::Application,
56 # allowing the developer to load classes in lib and use them during application
57 # configuration.
58 #
59 # class MyApplication < Rails::Application
60 # require "my_backend" # in lib/my_backend
61 # config.i18n.backend = MyBackend
62 # end
63 #
64 # Notice this method takes into consideration the default root path. So if you
65 # are changing config.root inside your application definition or having a custom
66 # Rails application, you will need to add lib to $LOAD_PATH on your own in case
67 # 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
68 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
69 path = config.root.join('lib').to_s
70 $LOAD_PATH.unshift(path) if File.exists?(path)
71 end
72
9e081ca José Valim Improve documentation for add_lib_to_load_paths!
josevalim authored
73 def require_environment! #:nodoc:
d649bf1 José Valim Provide a cleaner syntax for paths configuration that does not rely on m...
josevalim authored
74 environment = paths["config/environment"].existent.first
13d66cd José Valim Extract Railtie load from application.
josevalim authored
75 require environment if environment
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored
76 end
77
9e081ca José Valim Improve documentation for add_lib_to_load_paths!
josevalim authored
78 def eager_load! #:nodoc:
5a0d73f José Valim Add lib to load paths when application is inherited to be able to load l...
josevalim authored
79 railties.all(&:eager_load!)
80 super
81 end
82
7b0c592 José Valim reload_routes! is part of the public API and should not be removed.
josevalim authored
83 def reload_routes!
84 routes_reloader.reload!
85 end
86
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
87 def routes_reloader
3049e64 Piotr Sarnacki Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
88 @routes_reloader ||= RoutesReloader.new
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
89 end
5f8e48c Joshua Peek Move route reloading into railties
josh authored
90
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
91 def initialize!
abeb0ff Piotr Sarnacki Ensure that Rails.application.initialize! is called only once
drogus authored
92 raise "Application has been already initialized." if @initialized
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
93 run_initializers(self)
abeb0ff Piotr Sarnacki Ensure that Rails.application.initialize! is called only once
drogus authored
94 @initialized = true
80130d1 José Valim Extract routes reloading responsibilities from application and load them...
josevalim authored
95 self
5f8e48c Joshua Peek Move route reloading into railties
josh authored
96 end
97
94bb331 Jeremy Kemper Shift more responsibility from application class to its singleton instan...
jeremy authored
98 def load_tasks
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
99 initialize_tasks
13d66cd José Valim Extract Railtie load from application.
josevalim authored
100 railties.all { |r| r.load_tasks }
e461e1b José Valim Ensure application rake tasks and generators are loaded after the ones s...
josevalim authored
101 super
13d66cd José Valim Extract Railtie load from application.
josevalim authored
102 self
94bb331 Jeremy Kemper Shift more responsibility from application class to its singleton instan...
jeremy authored
103 end
104
4ca9765 José Valim Allow railties to specify generators paths.
josevalim authored
105 def load_generators
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
106 initialize_generators
13d66cd José Valim Extract Railtie load from application.
josevalim authored
107 railties.all { |r| r.load_generators }
e461e1b José Valim Ensure application rake tasks and generators are loaded after the ones s...
josevalim authored
108 super
13d66cd José Valim Extract Railtie load from application.
josevalim authored
109 self
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored
110 end
111
fa98eca José Valim Add console hook to force ActiveRecord::Base to be loaded when console s...
josevalim authored
112 def load_console(sandbox=false)
113 initialize_console(sandbox)
114 railties.all { |r| r.load_console }
115 super()
116 self
117 end
118
f816666 José Valim Alias app to build_middleware_stack for clarity.
josevalim authored
119 alias :build_middleware_stack :app
603d4fb Joshua Peek Build middleware stack and reload routes after app initializers run
josh authored
120
a132229 Piotr Sarnacki Added ability to set asset_path for engines
drogus authored
121 def env_config
122 @env_config ||= super.merge({
6690d66 José Valim Rename config.cookie_secret to config.secret_token and pass it as config...
josevalim authored
123 "action_dispatch.parameter_filter" => config.filter_parameters,
a132229 Piotr Sarnacki Added ability to set asset_path for engines
drogus authored
124 "action_dispatch.secret_token" => config.secret_token,
439a745 Prem Sichanugrist Make sure that we set `env["action_dispatch.show_exceptions"]`
sikachu authored
125 "action_dispatch.asset_path" => nil,
126 "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions
a132229 Piotr Sarnacki Added ability to set asset_path for engines
drogus authored
127 })
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
128 end
129
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
130 def initializers
e9793c6 Santiago Pastorino Makes this code a bit more clear
spastorino authored
131 Bootstrap.initializers_for(self) +
132 super +
133 Finisher.initializers_for(self)
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
134 end
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
135
32a5b49 Piotr Sarnacki Move singleton pattern to Railtie and remove Engine::Configurable and Ap...
drogus authored
136 def config
137 @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
138 end
139
612454e Joshua Peek Move sprockets initializers back to application
josh authored
140 def assets
141 @assets ||= build_asset_environment
142 end
143
144 def build_asset_environment
145 return nil if !config.use_sprockets
146 require 'sprockets'
147 env = Sprockets::Environment.new(root.to_s)
148 env.static_root = root.join("public/assets")
149 env
150 end
151
152 initializer :add_sprockets_paths do |app|
153 [
154 "app/javascripts",
155 "app/stylesheets",
156 "vendor/plugins/*/app/javascripts",
157 "vendor/plugins/*/app/stylesheets",
158 "vendor/plugins/*/javascripts",
159 "vendor/plugins/*/stylesheets"
160 ].each do |pattern|
161 Dir[app.root.join(pattern)].each do |dir|
162 app.assets.paths << dir
163 end
164 end
165 end
166
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
167 protected
168
1860d87 José Valim Ensure asset_path defaults to nil for application
josevalim authored
169 def default_asset_path
170 nil
171 end
172
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
173 def default_middleware_stack
174 ActionDispatch::MiddlewareStack.new.tap do |middleware|
eeb3246 Only add Rack::Cache to the middleware stack if config.action_controller...
Sparky authored
175 rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
254ab7d Yehuda Katz First pass at Rack::Cache
wycats authored
176
eeb3246 Only add Rack::Cache to the middleware stack if config.action_controller...
Sparky authored
177 require "action_dispatch/http/rack_cache" if rack_cache
74dd8a3 José Valim Move ETag and ConditionalGet logic from AD::Response to the middleware s...
josevalim authored
178 middleware.use ::Rack::Cache, rack_cache if rack_cache
eeb3246 Only add Rack::Cache to the middleware stack if config.action_controller...
Sparky authored
179
e6369bc Piotr Sarnacki Application's assets should have higher priority than engine's ones
drogus authored
180 if config.serve_static_assets
181 asset_paths = ActiveSupport::OrderedHash[config.static_asset_paths.to_a.reverse]
182 middleware.use ::ActionDispatch::Static, asset_paths
183 end
ea14396 Neeraj Singh replace if ! with unless
neerajdotname authored
184 middleware.use ::Rack::Lock unless config.allow_concurrency
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
185 middleware.use ::Rack::Runtime
186 middleware.use ::Rails::Rack::Logger
515ea95 Prem Sichanugrist Always use ActionDispatch::ShowExceptions middleware [#6462 state:resolv...
sikachu authored
187 middleware.use ::ActionDispatch::ShowExceptions, config.consider_all_requests_local
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
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
0f7c970 Introduce ActionDispatch::Reloader
John Firebaugh authored
190 middleware.use ::ActionDispatch::Reloader unless config.cache_classes
435bccd Replace AD::Callbacks.to_prepare with AD::Reloader.to_prepare
John Firebaugh authored
191 middleware.use ::ActionDispatch::Callbacks
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
192 middleware.use ::ActionDispatch::Cookies
193
194 if config.session_store
195 middleware.use config.session_store, config.session_options
196 middleware.use ::ActionDispatch::Flash
197 end
198
199 middleware.use ::ActionDispatch::ParamsParser
200 middleware.use ::Rack::MethodOverride
201 middleware.use ::ActionDispatch::Head
74dd8a3 José Valim Move ETag and ConditionalGet logic from AD::Response to the middleware s...
josevalim authored
202 middleware.use ::Rack::ConditionalGet
203 middleware.use ::Rack::ETag, "no-cache"
6767946 Yehuda Katz Improve best_standards_support to use only IE=Edge in development mode
wycats authored
204 middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support if config.action_dispatch.best_standards_support
19d8c8c José Valim Remove the laziness from the middleware stack.
josevalim authored
205 end
206 end
207
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
208 def initialize_tasks
209 require "rails/tasks"
210 task :environment do
211 $rails_rake_task = true
1b816d5 José Valim The rake task :environment now loads config/environment.rb instead of in...
josevalim authored
212 require_environment!
788fce2 José Valim Create configurable modules and ensure that they are added only on direc...
josevalim authored
213 end
214 end
215
216 def initialize_generators
217 require "rails/generators"
218 end
fa98eca José Valim Add console hook to force ActiveRecord::Base to be loaded when console s...
josevalim authored
219
220 def initialize_console(sandbox=false)
221 require "rails/console/app"
222 require "rails/console/sandbox" if sandbox
223 require "rails/console/helpers"
224 end
accd9b4 Joshua Peek Restore "Start Rails::Application object
josh authored
225 end
226 end
Something went wrong with that request. Please try again.