Skip to content
Newer
Older
100644 194 lines (169 sloc) 6.5 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'
f8bf198 @josevalim Add tests for explicit engines.
josevalim authored
38 autoload :Configurable, 'rails/application/configurable'
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
39 autoload :Configuration, 'rails/application/configuration'
4f03603 @josevalim Break application.rb file in smaller chunks.
josevalim authored
40 autoload :Finisher, 'rails/application/finisher'
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
41 autoload :Railties, 'rails/application/railties'
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
eedbf87 @drogus New way of generating urls for Application from Engine.
drogus authored
53 delegate :default_url_options, :default_url_options=, :to => :routes
54
9e081ca @josevalim 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 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
68 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
69 path = config.root.join('lib').to_s
70 $LOAD_PATH.unshift(path) if File.exists?(path)
71 end
72
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
73 def require_environment! #:nodoc:
1b816d5 @josevalim The rake task :environment now loads config/environment.rb instead of…
josevalim authored
74 environment = paths.config.environment.to_a.first
13d66cd @josevalim 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 @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
78 def eager_load! #:nodoc:
5a0d73f @josevalim Add lib to load paths when application is inherited to be able to loa…
josevalim authored
79 railties.all(&:eager_load!)
80 super
81 end
82
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
83 def routes_reloader
772c2b0 @josevalim Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
josevalim authored
84 @routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! }
5f8e48c @josh Move route reloading into railties
josh authored
85 end
86
87 def reload_routes!
0189fb7 @josevalim reload_routes! was still referencing old Rails::Application.
josevalim authored
88 _routes = self.routes
89 _routes.disable_clear_and_finalize = true
90 _routes.clear!
772c2b0 @josevalim Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
josevalim authored
91 routes_reloader.paths.each { |path| load(path) }
0189fb7 @josevalim reload_routes! was still referencing old Rails::Application.
josevalim authored
92 ActiveSupport.on_load(:action_controller) { _routes.finalize! }
772c2b0 @josevalim Use the new ActiveSupport::FileUpdateChecker instead of RoutesReloader.
josevalim authored
93 ensure
0189fb7 @josevalim reload_routes! was still referencing old Rails::Application.
josevalim authored
94 _routes.disable_clear_and_finalize = false
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
95 end
5f8e48c @josh Move route reloading into railties
josh authored
96
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
97 def initialize!
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
98 raise "Application has been already initialized." if @initialized
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
99 run_initializers(self)
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
100 @initialized = true
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
101 self
5f8e48c @josh Move route reloading into railties
josh authored
102 end
103
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
104 def load_tasks
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
105 initialize_tasks
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
106 railties.all { |r| r.load_tasks }
e461e1b @josevalim Ensure application rake tasks and generators are loaded after the one…
josevalim authored
107 super
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
108 self
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
109 end
110
4ca9765 @josevalim Allow railties to specify generators paths.
josevalim authored
111 def load_generators
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
112 initialize_generators
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
113 railties.all { |r| r.load_generators }
e461e1b @josevalim Ensure application rake tasks and generators are loaded after the one…
josevalim authored
114 super
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
115 self
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored
116 end
117
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored
118 def load_console(sandbox=false)
119 initialize_console(sandbox)
120 railties.all { |r| r.load_console }
121 super()
122 self
123 end
124
f816666 @josevalim Alias app to build_middleware_stack for clarity.
josevalim authored
125 alias :build_middleware_stack :app
603d4fb @josh Build middleware stack and reload routes after app initializers run
josh authored
126
a132229 @drogus Added ability to set asset_path for engines
drogus authored
127 def env_config
128 @env_config ||= super.merge({
6690d66 @josevalim Rename config.cookie_secret to config.secret_token and pass it as con…
josevalim authored
129 "action_dispatch.parameter_filter" => config.filter_parameters,
a132229 @drogus Added ability to set asset_path for engines
drogus authored
130 "action_dispatch.secret_token" => config.secret_token,
131 "action_dispatch.asset_path" => nil
132 })
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
133 end
134
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
135 def initializers
5d07869 @josevalim Ensure all initializers are collections.
josevalim authored
136 initializers = Bootstrap.initializers_for(self)
2d1f9fb @josh Plugins need to load before app initializers
josh authored
137 initializers += super
5d07869 @josevalim Ensure all initializers are collections.
josevalim authored
138 initializers += Finisher.initializers_for(self)
9f7874a @jeremy Move Collection responsibility from application to initializable
jeremy authored
139 initializers
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
140 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
141
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
142 def config
143 @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
144 end
145
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
146 protected
147
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
148 def default_middleware_stack
149 ActionDispatch::MiddlewareStack.new.tap do |middleware|
254ab7d @wycats First pass at Rack::Cache
wycats authored
150 require "action_dispatch/http/rack_cache" if config.action_dispatch.rack_cache
151
152 middleware.use ::Rack::Cache, config.action_dispatch.rack_cache if config.action_dispatch.rack_cache
8284fd3 @drogus Get rid of static_paths method and instead configure paths for Action…
drogus authored
153 middleware.use ::ActionDispatch::Static, config.static_asset_paths if config.serve_static_assets
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
154 middleware.use ::Rack::Lock if !config.allow_concurrency
155 middleware.use ::Rack::Runtime
156 middleware.use ::Rails::Rack::Logger
157 middleware.use ::ActionDispatch::ShowExceptions, config.consider_all_requests_local if config.action_dispatch.show_exceptions
158 middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
159 middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
160 middleware.use ::ActionDispatch::Callbacks, !config.cache_classes
161 middleware.use ::ActionDispatch::Cookies
162
163 if config.session_store
164 middleware.use config.session_store, config.session_options
165 middleware.use ::ActionDispatch::Flash
166 end
167
168 middleware.use ::ActionDispatch::ParamsParser
169 middleware.use ::Rack::MethodOverride
170 middleware.use ::ActionDispatch::Head
6767946 @wycats Improve best_standards_support to use only IE=Edge in development mode
wycats authored
171 middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support if config.action_dispatch.best_standards_support
19d8c8c @josevalim Remove the laziness from the middleware stack.
josevalim authored
172 end
173 end
174
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
175 def initialize_tasks
176 require "rails/tasks"
177 task :environment do
178 $rails_rake_task = true
1b816d5 @josevalim The rake task :environment now loads config/environment.rb instead of…
josevalim authored
179 require_environment!
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
180 end
181 end
182
183 def initialize_generators
184 require "rails/generators"
185 end
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored
186
187 def initialize_console(sandbox=false)
188 require "rails/console/app"
189 require "rails/console/sandbox" if sandbox
190 require "rails/console/helpers"
191 end
accd9b4 @josh Restore "Start Rails::Application object
josh authored
192 end
193 end
Something went wrong with that request. Please try again.