Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 209 lines (180 sloc) 6.888 kB
6690d66 @josevalim Rename config.cookie_secret to config.secret_token and pass it as con…
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 # == 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 @josevalim A few changes were done in this commit:
josevalim authored
23 # "logger", "reload_plugins" and so forth.
781d0a9 @josevalim 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 @wycats Removing Metal from Rails 3.
wycats authored
32 # == Middlewares
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
33 #
45e6028 @wycats Removing Metal from Rails 3.
wycats authored
34 # 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
35 #
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
36 class Application < Engine
4f03603 @josevalim Break application.rb file in smaller chunks.
josevalim authored
37 autoload :Bootstrap, 'rails/application/bootstrap'
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
38 autoload :Configuration, 'rails/application/configuration'
4f03603 @josevalim Break application.rb file in smaller chunks.
josevalim authored
39 autoload :Finisher, 'rails/application/finisher'
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
40 autoload :Railties, 'rails/application/railties'
3049e64 @drogus Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
41 autoload :RoutesReloader, 'rails/application/routes_reloader'
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
42
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
43 class << self
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
44 def inherited(base)
2fde9d7 @josevalim Solve some pendencies.
josevalim authored
45 raise "You cannot have more than one Rails::Application" if Rails.application
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
46 super
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
47 Rails.application = base.instance
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
48 Rails.application.add_lib_to_load_path!
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
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
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
53 attr_accessor :assets, :sandbox
54 alias_method :sandbox?, :sandbox
55
eedbf87 @drogus New way of generating urls for Application from Engine.
drogus authored
56 delegate :default_url_options, :default_url_options=, :to => :routes
57
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
58 # This method is called just after an application inherits from Rails::Application,
59 # allowing the developer to load classes in lib and use them during application
60 # configuration.
61 #
62 # class MyApplication < Rails::Application
63 # require "my_backend" # in lib/my_backend
64 # config.i18n.backend = MyBackend
65 # end
66 #
67 # Notice this method takes into consideration the default root path. So if you
68 # are changing config.root inside your application definition or having a custom
69 # Rails application, you will need to add lib to $LOAD_PATH on your own in case
70 # 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
71 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
72 path = config.root.join('lib').to_s
73 $LOAD_PATH.unshift(path) if File.exists?(path)
74 end
75
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
76 def require_environment! #:nodoc:
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
77 environment = paths["config/environment"].existent.first
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
78 require environment if environment
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored
79 end
80
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
81 def eager_load! #:nodoc:
5a0d73f @josevalim Add lib to load paths when application is inherited to be able to loa…
josevalim authored
82 railties.all(&:eager_load!)
83 super
84 end
85
7b0c592 @josevalim reload_routes! is part of the public API and should not be removed.
josevalim authored
86 def reload_routes!
87 routes_reloader.reload!
88 end
89
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
90 def routes_reloader
3049e64 @drogus Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
91 @routes_reloader ||= RoutesReloader.new
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
92 end
5f8e48c @josh Move route reloading into railties
josh authored
93
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
94 def initialize!
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
95 raise "Application has been already initialized." if @initialized
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
96 run_initializers(self)
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
97 @initialized = true
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
98 self
5f8e48c @josh Move route reloading into railties
josh authored
99 end
100
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
101 def load_tasks(app=self)
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
102 initialize_tasks
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
103 railties.all { |r| r.load_tasks(app) }
e461e1b @josevalim Ensure application rake tasks and generators are loaded after the one…
josevalim authored
104 super
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
105 self
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
106 end
107
c1df442 @wycats Update generator config to support hiding namespaces and pass the app…
wycats authored
108 def load_generators(app=self)
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
109 initialize_generators
c1df442 @wycats Update generator config to support hiding namespaces and pass the app…
wycats authored
110 railties.all { |r| r.load_generators(app) }
594b749 @josevalim Streamline generators initialization flow.
josevalim authored
111 Rails::Generators.configure!(app.config.generators)
e461e1b @josevalim Ensure application rake tasks and generators are loaded after the one…
josevalim authored
112 super
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
113 self
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored
114 end
115
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
116 def load_console(app=self)
594b749 @josevalim Streamline generators initialization flow.
josevalim authored
117 initialize_console
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
118 railties.all { |r| r.load_console(app) }
119 super
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored
120 self
121 end
122
a132229 @drogus Added ability to set asset_path for engines
drogus authored
123 def env_config
124 @env_config ||= super.merge({
6690d66 @josevalim Rename config.cookie_secret to config.secret_token and pass it as con…
josevalim authored
125 "action_dispatch.parameter_filter" => config.filter_parameters,
a132229 @drogus Added ability to set asset_path for engines
drogus authored
126 "action_dispatch.secret_token" => config.secret_token,
439a745 @sikachu Make sure that we set `env["action_dispatch.show_exceptions"]`
sikachu authored
127 "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions
a132229 @drogus Added ability to set asset_path for engines
drogus authored
128 })
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
129 end
130
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
131 def initializers
e9793c6 @spastorino Makes this code a bit more clear
spastorino authored
132 Bootstrap.initializers_for(self) +
133 super +
134 Finisher.initializers_for(self)
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
135 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
136
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
137 def config
138 @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
139 end
140
2fe70c1 @josevalim Booya, sprockets now works from Engines.
josevalim authored
141 protected
142
143 alias :build_middleware_stack :app
612454e @josh Move sprockets initializers back to application
josh authored
144
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
145 def default_middleware_stack
146 ActionDispatch::MiddlewareStack.new.tap do |middleware|
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
147 if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
148 require "action_dispatch/http/rack_cache"
149 middleware.use ::Rack::Cache, rack_cache
150 end
254ab7d @wycats First pass at Rack::Cache
wycats authored
151
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
152 if config.force_ssl
153 require "rack/ssl"
154 middleware.use ::Rack::SSL
155 end
eeb3246 Only add Rack::Cache to the middleware stack if config.action_control…
Sparky authored
156
e6369bc @drogus Application's assets should have higher priority than engine's ones
drogus authored
157 if config.serve_static_assets
1ca8123 @josevalim Add static_cache_control as configuration option and set a default fo…
josevalim authored
158 middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
e6369bc @drogus Application's assets should have higher priority than engine's ones
drogus authored
159 end
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
160
ea14396 @neerajdotname replace if ! with unless
neerajdotname authored
161 middleware.use ::Rack::Lock unless config.allow_concurrency
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
162 middleware.use ::Rack::Runtime
17a91a6 @dlee Logs should show overridden method; Issue 426
dlee authored
163 middleware.use ::Rack::MethodOverride
164 middleware.use ::Rails::Rack::Logger # must come after Rack::MethodOverride to properly log overridden methods
515ea95 @sikachu Always use ActionDispatch::ShowExceptions middleware [#6462 state:res…
sikachu authored
165 middleware.use ::ActionDispatch::ShowExceptions, config.consider_all_requests_local
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
166 middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
167 middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
0f7c970 Introduce ActionDispatch::Reloader
John Firebaugh authored
168 middleware.use ::ActionDispatch::Reloader unless config.cache_classes
435bccd Replace AD::Callbacks.to_prepare with AD::Reloader.to_prepare
John Firebaugh authored
169 middleware.use ::ActionDispatch::Callbacks
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
170 middleware.use ::ActionDispatch::Cookies
171
172 if config.session_store
173 middleware.use config.session_store, config.session_options
174 middleware.use ::ActionDispatch::Flash
175 end
176
177 middleware.use ::ActionDispatch::ParamsParser
178 middleware.use ::ActionDispatch::Head
74dd8a3 @josevalim Move ETag and ConditionalGet logic from AD::Response to the middlewar…
josevalim authored
179 middleware.use ::Rack::ConditionalGet
180 middleware.use ::Rack::ETag, "no-cache"
2c0c4d7 @sikachu Add `config.force_ssl` configuration which will load `Rack::SSL` midd…
sikachu authored
181
182 if config.action_dispatch.best_standards_support
183 middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support
184 end
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
185 end
186 end
187
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
188 def initialize_tasks
e290835 @bilts Make Rake::DSL methods available within Railtie rake_tasks block
bilts authored
189 self.class.rake_tasks do
190 require "rails/tasks"
191 task :environment do
192 $rails_rake_task = true
193 require_environment!
194 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
195 end
196 end
197
198 def initialize_generators
199 require "rails/generators"
200 end
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored
201
594b749 @josevalim Streamline generators initialization flow.
josevalim authored
202 def initialize_console
d54c778 @amatsuda always enable `pp` on the Rails console
amatsuda authored
203 require "pp"
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored
204 require "rails/console/app"
205 require "rails/console/helpers"
206 end
accd9b4 @josh Restore "Start Rails::Application object
josh authored
207 end
17a91a6 @dlee Logs should show overridden method; Issue 426
dlee authored
208 end
Something went wrong with that request. Please try again.