Skip to content
Newer
Older
100644 530 lines (464 sloc) 17.8 KB
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
1 require 'fileutils'
8d901b9 @rafaelfranca Require yaml before trying to rescue its exception class
rafaelfranca authored
2 require 'yaml'
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
3 require 'active_support/core_ext/hash/keys'
274a3aa @trevorturk Allow transparent upgrading of legacy signed cookies to encrypted coo…
trevorturk authored
4 require 'active_support/core_ext/object/blank'
60609bb @spastorino Sign cookies using key deriver
spastorino authored
5 require 'active_support/key_generator'
0a2d004 @rafaelfranca Add missing require
rafaelfranca authored
6 require 'active_support/message_verifier'
2fde9d7 @josevalim Solve some pendencies.
josevalim authored
7 require 'rails/engine'
dc677f7 tests pass with requiring the frameworks in rails.rb
Carlhuda authored
8
accd9b4 @josh Restore "Start Rails::Application object
josh authored
9 module Rails
271a552 @claudiob [ci skip] Remove comments about Rails 3.1
claudiob authored
10 # An Engine with the responsibility of coordinating the whole boot process.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
11 #
12 # == Initialization
13 #
dad7fdc @spastorino Rails::Plugin has gone
spastorino authored
14 # Rails::Application is responsible for executing all railties and engines
eb716f6 @mjankowski grammar/tense correction to rails application doc
mjankowski authored
15 # initializers. It also executes some bootstrap initializers (check
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
16 # Rails::Application::Bootstrap) and finishing initializers, after all the others
17 # are executed (check Rails::Application::Finisher).
18 #
19 # == Configuration
20 #
21 # Besides providing the same configuration as Rails::Engine and Rails::Railtie,
22 # the application object has several specific configurations, for example
11bc348 @josevalim Remove allow_concurrency as a flag
josevalim authored
23 # "cache_classes", "consider_all_requests_local", "filter_parameters",
dad7fdc @spastorino Rails::Plugin has gone
spastorino authored
24 # "logger" and so forth.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
25 #
26 # Check Rails::Application::Configuration to see them all.
27 #
28 # == Routes
29 #
30 # The application object is also responsible for holding the routes and reloading routes
31 # whenever the files change in development.
32 #
45e6028 @wycats Removing Metal from Rails 3.
wycats authored
33 # == Middlewares
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
34 #
45e6028 @wycats Removing Metal from Rails 3.
wycats authored
35 # 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
36 #
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
37 # == Booting process
38 #
39 # The application is also responsible for setting up and executing the booting
40 # process. From the moment you require "config/application.rb" in your app,
41 # the booting process goes like this:
42 #
43 # 1) require "config/boot.rb" to setup load paths
44 # 2) require railties and engines
45 # 3) Define Rails.application as "class MyApp::Application < Rails::Application"
46 # 4) Run config.before_configuration callbacks
47 # 5) Load config/environments/ENV.rb
48 # 6) Run config.before_initialize callbacks
49 # 7) Run Railtie#initializer defined by railties, engines and application.
11aa167 @josevalim Revert "Provide a unique point for running initializers."
josevalim authored
50 # One by one, each engine sets up its load paths, routes and runs its config/initializers/* files.
ff25b0e @wangjohn Renumbering the comments in the application boot process.
wangjohn authored
51 # 8) Custom Railtie#initializers added by railties, engines and applications are executed
52 # 9) Build the middleware stack and run to_prepare callbacks
53 # 10) Run config.before_eager_load and eager_load! if eager_load is true
54 # 11) Run config.after_initialize callbacks
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
55 #
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
56 # == Multiple Applications
57 #
58 # If you decide to define multiple applications, then the first application
59 # that is initialized will be set to +Rails.application+, unless you override
60 # it with a different application.
61 #
62 # To create a new application, you can instantiate a new instance of a class
63 # that has already been created:
64 #
65 # class Application < Rails::Application
66 # end
67 #
68 # first_application = Application.new
69 # second_application = Application.new(config: first_application.config)
70 #
71 # In the above example, the configuration from the first application was used
72 # to initialize the second application. You can also use the +initialize_copy+
73 # on one of the applications to create a copy of the application which shares
74 # the configuration.
75 #
76 # If you decide to define rake tasks, runners, or initializers in an
77 # application other than +Rails.application+, then you must run those
78 # these manually.
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
79 class Application < Engine
685309c @wangjohn Creating a class to build the default middleware stack.
wangjohn authored
80 autoload :Bootstrap, 'rails/application/bootstrap'
81 autoload :Configuration, 'rails/application/configuration'
82 autoload :DefaultMiddlewareStack, 'rails/application/default_middleware_stack'
83 autoload :Finisher, 'rails/application/finisher'
84 autoload :Railties, 'rails/engine/railties'
85 autoload :RoutesReloader, 'rails/application/routes_reloader'
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
86
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
87 class << self
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
88 def inherited(base)
89 super
d25fe31 @tenderlove lazily instantiate application subclasses
tenderlove authored
90 Rails.app_class = base
9dbcac7 @tenderlove add lib to $LOAD_PATH on application inhertence. fixes #17106
tenderlove authored
91 add_lib_to_load_path!(find_root(base.called_from))
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
92 end
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
93
2296989 @tenderlove defer running after_config hooks until after the object is allocated
tenderlove authored
94 def instance
95 super.run_load_hooks!
96 end
97
8121eef @tenderlove add a new constructor that runs load hooks
tenderlove authored
98 def create(initial_variable_values = {}, &block)
99 new(initial_variable_values, &block).run_load_hooks!
100 end
101
9dbcac7 @tenderlove add lib to $LOAD_PATH on application inhertence. fixes #17106
tenderlove authored
102 def find_root(from)
103 find_root_with_flag "config.ru", from, Dir.pwd
104 end
105
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
106 # Makes the +new+ method public.
107 #
108 # Note that Rails::Application inherits from Rails::Engine, which
109 # inherits from Rails::Railtie and the +new+ method on Rails::Railtie is
110 # private
111 public :new
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
112 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
113
f9da785 @jeremy Move background jobs to the 'jobs' branch until fully baked. Not ship…
jeremy authored
114 attr_accessor :assets, :sandbox
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
115 alias_method :sandbox?, :sandbox
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
116 attr_reader :reloaders
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored
117
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
118 delegate :default_url_options, :default_url_options=, to: :routes
eedbf87 @drogus New way of generating urls for Application from Engine.
drogus authored
119
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
120 INITIAL_VARIABLES = [:config, :railties, :routes_reloader, :reloaders,
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
121 :routes, :helpers, :app_env_config, :secrets] # :nodoc:
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
122
123 def initialize(initial_variable_values = {}, &block)
124 super()
2be4916 @rafaelfranca Make possibile to get different message verifiers
rafaelfranca authored
125 @initialized = false
126 @reloaders = []
127 @routes_reloader = nil
128 @app_env_config = nil
129 @ordered_railties = nil
130 @railties = nil
131 @message_verifiers = {}
2296989 @tenderlove defer running after_config hooks until after the object is allocated
tenderlove authored
132 @ran_load_hooks = false
133
134 # are these actually used?
135 @initial_variable_values = initial_variable_values
136 @block = block
137 end
138
139 # Returns true if the application is initialized.
140 def initialized?
141 @initialized
142 end
143
144 def run_load_hooks! # :nodoc:
145 return self if @ran_load_hooks
146 @ran_load_hooks = true
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
147 ActiveSupport.run_load_hooks(:before_configuration, self)
148
2296989 @tenderlove defer running after_config hooks until after the object is allocated
tenderlove authored
149 @initial_variable_values.each do |variable_name, value|
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
150 if INITIAL_VARIABLES.include?(variable_name)
151 instance_variable_set("@#{variable_name}", value)
152 end
153 end
154
2296989 @tenderlove defer running after_config hooks until after the object is allocated
tenderlove authored
155 instance_eval(&@block) if @block
156 self
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
157 end
158
159 # Reload application routes regardless if they changed or not.
160 def reload_routes!
161 routes_reloader.reload!
162 end
163
dd5b083 @ronakjangir47 Renamed ‘Return’ to ‘Returns’ [ci skip]
ronakjangir47 authored
164 # Returns the application's KeyGenerator
0479bff @NZKoz Provide access to the application's KeyGenerator
NZKoz authored
165 def key_generator
166 # number of iterations selected based on consultation with the google security
167 # team. Details at https://github.com/rails/rails/pull/6952#issuecomment-7661220
70c83f4 @carlosantoniodasilva Remove useless begin..end
carlosantoniodasilva authored
168 @caching_key_generator ||=
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
169 if secrets.secret_key_base
4810f40 @arunagw raise `ArgumentError` when `SECRET_KEY_BASE` is an integer
arunagw authored
170 unless secrets.secret_key_base.kind_of?(String)
171 raise ArgumentError, "`secret_key_base` for #{Rails.env} environment must be a type of String, change this value in `config/secrets.yml`"
172 end
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
173 key_generator = ActiveSupport::KeyGenerator.new(secrets.secret_key_base, iterations: 1000)
851e8fe @spastorino Cache generated keys per KeyGenerator instance using salt + key_size
spastorino authored
174 ActiveSupport::CachingKeyGenerator.new(key_generator)
175 else
db5f1a4 @bf4 `secret_token` is now saved in `Rails.application.secrets.secret_token`
bf4 authored
176 ActiveSupport::LegacyKeyGenerator.new(secrets.secret_token)
851e8fe @spastorino Cache generated keys per KeyGenerator instance using salt + key_size
spastorino authored
177 end
0479bff @NZKoz Provide access to the application's KeyGenerator
NZKoz authored
178 end
179
d3d8498 @rafaelfranca Fix typos
rafaelfranca authored
180 # Returns a message verifier object.
69ac53c @rafaelfranca Add documentation and CHANGELOG entry to Application#verifier
rafaelfranca authored
181 #
d3d8498 @rafaelfranca Fix typos
rafaelfranca authored
182 # This verifier can be used to generate and verify signed messages in the application.
69ac53c @rafaelfranca Add documentation and CHANGELOG entry to Application#verifier
rafaelfranca authored
183 #
f98bd42 @rafaelfranca No need to configure salts
rafaelfranca authored
184 # It is recommended not to use the same verifier for different things, so you can get different
185 # verifiers passing the +verifier_name+ argument.
2be4916 @rafaelfranca Make possibile to get different message verifiers
rafaelfranca authored
186 #
187 # ==== Parameters
188 #
61f6b47 @rafaelfranca Change the message verifier argument to verifier_name
rafaelfranca authored
189 # * +verifier_name+ - the name of the message verifier.
2be4916 @rafaelfranca Make possibile to get different message verifiers
rafaelfranca authored
190 #
191 # ==== Examples
192 #
61f6b47 @rafaelfranca Change the message verifier argument to verifier_name
rafaelfranca authored
193 # message = Rails.application.message_verifier('sensitive_data').generate('my sensible data')
194 # Rails.application.message_verifier('sensitive_data').verify(message)
69ac53c @rafaelfranca Add documentation and CHANGELOG entry to Application#verifier
rafaelfranca authored
195 # # => 'my sensible data'
196 #
f98bd42 @rafaelfranca No need to configure salts
rafaelfranca authored
197 # See the +ActiveSupport::MessageVerifier+ documentation for more information.
61f6b47 @rafaelfranca Change the message verifier argument to verifier_name
rafaelfranca authored
198 def message_verifier(verifier_name)
199 @message_verifiers[verifier_name] ||= begin
bfdae17 @rafaelfranca Make possible to use symbol as the verifier name
rafaelfranca authored
200 secret = key_generator.generate_key(verifier_name.to_s)
2330017 @rafaelfranca Add application verifier
rafaelfranca authored
201 ActiveSupport::MessageVerifier.new(secret)
202 end
203 end
204
9629dea @rafaelfranca Add Rails::Application#config_for
rafaelfranca authored
205 # Convenience for loading config/foo.yml for the current Rails env.
206 #
207 # Example:
208 #
209 # # config/exception_notification.yml:
210 # production:
211 # url: http://127.0.0.1:8080
212 # namespace: my_app_production
213 # development:
214 # url: http://localhost:3001
215 # namespace: my_app_development
216 #
217 # # config/production.rb
994d3eb @tamird Standardize on `Rails.application` [ci skip]
tamird authored
218 # Rails.application.configure do
9629dea @rafaelfranca Add Rails::Application#config_for
rafaelfranca authored
219 # config.middleware.use ExceptionNotifier, config_for(:exception_notification)
220 # end
e58c967 @Sirupsen rails/application: allow passing an env to config_for
Sirupsen authored
221 def config_for(name, env: Rails.env)
fc635b5 @rafaelfranca Accept a Pathname in Application#config_for
rafaelfranca authored
222 if name.is_a?(Pathname)
223 yaml = name
224 else
225 yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml")
226 end
9629dea @rafaelfranca Add Rails::Application#config_for
rafaelfranca authored
227
228 if yaml.exist?
229 require "erb"
e58c967 @Sirupsen rails/application: allow passing an env to config_for
Sirupsen authored
230 (YAML.load(ERB.new(yaml.read).result) || {})[env] || {}
9629dea @rafaelfranca Add Rails::Application#config_for
rafaelfranca authored
231 else
232 raise "Could not load configuration. No such file - #{yaml}"
233 end
234 rescue Psych::SyntaxError => e
235 raise "YAML syntax error occurred while parsing #{yaml}. " \
236 "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
237 "Error: #{e.message}"
238 end
239
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
240 # Stores some of the Rails initial environment parameters which
241 # will be used by middlewares and engines to configure themselves.
242 def env_config
862389c @josevalim Use @app_env_config instead of @env_config
josevalim authored
243 @app_env_config ||= begin
484ff7d @wangjohn Removing a repetitive comment and removing a deprecation warning.
wangjohn authored
244 validate_secret_key_config!
e272000 @spastorino Warn config.derive_keys will be true by default in 4.1
spastorino authored
245
246 super.merge({
247 "action_dispatch.parameter_filter" => config.filter_parameters,
86e3aaa @freegenie Adding filter capability to ActionController logs
freegenie authored
248 "action_dispatch.redirect_filter" => config.filter_redirect,
db5f1a4 @bf4 `secret_token` is now saved in `Rails.application.secrets.secret_token`
bf4 authored
249 "action_dispatch.secret_token" => secrets.secret_token,
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
250 "action_dispatch.secret_key_base" => secrets.secret_key_base,
e272000 @spastorino Warn config.derive_keys will be true by default in 4.1
spastorino authored
251 "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
252 "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
253 "action_dispatch.logger" => Rails.logger,
254 "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner,
47da574 @spastorino Allow users to change the default salt if they want, shouldn't be nec…
spastorino authored
255 "action_dispatch.key_generator" => key_generator,
256 "action_dispatch.http_auth_salt" => config.action_dispatch.http_auth_salt,
257 "action_dispatch.signed_cookie_salt" => config.action_dispatch.signed_cookie_salt,
258 "action_dispatch.encrypted_cookie_salt" => config.action_dispatch.encrypted_cookie_salt,
b23ffd0 @lukesarnacki Allow session serializer key in config.session_store
lukesarnacki authored
259 "action_dispatch.encrypted_signed_cookie_salt" => config.action_dispatch.encrypted_signed_cookie_salt,
29be3f5 @lukaszx0 Add config option for cookies digest
lukaszx0 authored
260 "action_dispatch.cookies_serializer" => config.action_dispatch.cookies_serializer,
261 "action_dispatch.cookies_digest" => config.action_dispatch.cookies_digest
e272000 @spastorino Warn config.derive_keys will be true by default in 4.1
spastorino authored
262 })
263 end
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
264 end
265
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
266 # If you try to define a set of rake tasks on the instance, these will get
267 # passed up to the rake tasks defined on the application's class.
268 def rake_tasks(&block)
269 self.class.rake_tasks(&block)
270 end
271
272 # Sends the initializers to the +initializer+ method defined in the
273 # Rails::Initializable module. Each Rails::Application class has its own
274 # set of initializers, as defined by the Initializable module.
275 def initializer(name, opts={}, &block)
276 self.class.initializer(name, opts, &block)
277 end
278
279 # Sends any runner called in the instance of a new application up
280 # to the +runner+ method defined in Rails::Railtie.
281 def runner(&blk)
282 self.class.runner(&blk)
283 end
284
43f5250 @rafaelfranca Make console and generators blocks works at Application instance level
rafaelfranca authored
285 # Sends any console called in the instance of a new application up
286 # to the +console+ method defined in Rails::Railtie.
287 def console(&blk)
288 self.class.console(&blk)
289 end
290
291 # Sends any generators called in the instance of a new application up
292 # to the +generators+ method defined in Rails::Railtie.
293 def generators(&blk)
294 self.class.generators(&blk)
295 end
296
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
297 # Sends the +isolate_namespace+ method up to the class method.
298 def isolate_namespace(mod)
299 self.class.isolate_namespace(mod)
300 end
301
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
302 ## Rails internal API
303
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
304 # This method is called just after an application inherits from Rails::Application,
305 # allowing the developer to load classes in lib and use them during application
306 # configuration.
307 #
308 # class MyApplication < Rails::Application
309 # require "my_backend" # in lib/my_backend
310 # config.i18n.backend = MyBackend
311 # end
312 #
313 # Notice this method takes into consideration the default root path. So if you
314 # are changing config.root inside your application definition or having a custom
315 # Rails application, you will need to add lib to $LOAD_PATH on your own in case
316 # you need to load files in lib/ during the application configuration as well.
9dbcac7 @tenderlove add lib to $LOAD_PATH on application inhertence. fixes #17106
tenderlove authored
317 def self.add_lib_to_load_path!(root) #:nodoc:
318 path = File.join root, 'lib'
4d4ff53 @tenderlove clean up some warnings on trunk ruby
tenderlove authored
319 if File.exist?(path) && !$LOAD_PATH.include?(path)
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
320 $LOAD_PATH.unshift(path)
321 end
5a0d73f @josevalim Add lib to load paths when application is inherited to be able to loa…
josevalim authored
322 end
323
9e081ca @josevalim Improve documentation for add_lib_to_load_paths!
josevalim authored
324 def require_environment! #:nodoc:
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
325 environment = paths["config/environment"].existent.first
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
326 require environment if environment
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored
327 end
328
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
329 def routes_reloader #:nodoc:
3049e64 @drogus Moved Rails::RoutesReloader to Rails::Application::RoutesReloader
drogus authored
330 @routes_reloader ||= RoutesReloader.new
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
331 end
5f8e48c @josh Move route reloading into railties
josh authored
332
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
333 # Returns an array of file paths appended with a hash of
334 # directories-extensions suitable for ActiveSupport::FileUpdateChecker
335 # API.
336 def watchable_args #:nodoc:
516f102 @ndbroadbent Refactored watchable_args and build_original_fullpath methods in rail…
ndbroadbent authored
337 files, dirs = config.watchable_files.dup, config.watchable_dirs.dup
fa1d9a8 @josevalim Speed up development by only reloading classes if dependencies files …
josevalim authored
338
339 ActiveSupport::Dependencies.autoload_paths.each do |path|
340 dirs[path.to_s] = [:rb]
341 end
342
80256ab @josevalim FileUpdateChecker should be able to handle deleted files.
josevalim authored
343 [files, dirs]
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
344 end
345
346 # Initialize the application passing the given group. By default, the
2d5a6de @hone `initialize_on_precompile` is not used anymore.
hone authored
347 # group is :default
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
348 def initialize!(group=:default) #:nodoc:
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
349 raise "Application has been already initialized." if @initialized
eb367af @josevalim `rake assets:precompile` loads the application but does not initializ…
josevalim authored
350 run_initializers(group, self)
abeb0ff @drogus Ensure that Rails.application.initialize! is called only once
drogus authored
351 @initialized = true
80130d1 @josevalim Extract routes reloading responsibilities from application and load t…
josevalim authored
352 self
5f8e48c @josh Move route reloading into railties
josh authored
353 end
354
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
355 def initializers #:nodoc:
e9793c6 @spastorino Makes this code a bit more clear
spastorino authored
356 Bootstrap.initializers_for(self) +
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
357 railties_initializers(super) +
e9793c6 @spastorino Makes this code a bit more clear
spastorino authored
358 Finisher.initializers_for(self)
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
359 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
360
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
361 def config #:nodoc:
9dbcac7 @tenderlove add lib to $LOAD_PATH on application inhertence. fixes #17106
tenderlove authored
362 @config ||= Application::Configuration.new(self.class.find_root(self.class.called_from))
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
363 end
364
08dc924 @wangjohn Allowing multiple rails applications in the same ruby instance.
wangjohn authored
365 def config=(configuration) #:nodoc:
366 @config = configuration
367 end
368
2f7b72b @tgxworld Rails::Application#secrets should be documented.
tgxworld authored
369 # Returns secrets added to config/secrets.yml.
370 #
371 # Example:
372 #
373 # development:
374 # secret_key_base: 836fa3665997a860728bcb9e9a1e704d427cfc920e79d847d79c8a9a907b9e965defa4154b2b86bdec6930adbe33f21364523a6f6ce363865724549fdfc08553
375 # test:
376 # secret_key_base: 5a37811464e7d378488b0f073e2193b093682e4e21f5d6f3ae0a4e1781e61a351fdc878a843424e81c73fb484a40d23f92c8dafac4870e74ede6e5e174423010
377 # production:
378 # secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
379 # namespace: my_app_production
380 #
381 # +Rails.application.secrets.namespace+ returns +my_app_production+ in the
382 # production environment.
383 def secrets
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
384 @secrets ||= begin
385 secrets = ActiveSupport::OrderedOptions.new
bb7537b @guilleiguaran Rename tokens.yml to secrets.yml
guilleiguaran authored
386 yaml = config.paths["config/secrets"].first
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
387 if File.exist?(yaml)
388 require "erb"
37e30d2 @senny do not crash when `config/secrets.yml` is blank.
senny authored
389 all_secrets = YAML.load(ERB.new(IO.read(yaml)).result) || {}
390 env_secrets = all_secrets[Rails.env]
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
391 secrets.merge!(env_secrets.symbolize_keys) if env_secrets
392 end
393
394 # Fallback to config.secret_key_base if secrets.secret_key_base isn't set
395 secrets.secret_key_base ||= config.secret_key_base
1d1239d No need to sync config.secret_token and secrets.secret_token
Rafael Mendonça França authored
396 # Fallback to config.secret_token if secrets.secret_token isn't set
397 secrets.secret_token ||= config.secret_token
3eaa298 @guilleiguaran Load secret_key_base from tokens.yml, fallback to config.secret_key_base
guilleiguaran authored
398
399 secrets
400 end
401 end
402
403 def secrets=(secrets) #:nodoc:
404 @secrets = secrets
405 end
406
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
407 def to_app #:nodoc:
8496be7 @guilleiguaran Fixing Rails::Server#app under Rack::URLMap
guilleiguaran authored
408 self
409 end
410
27f0add @josevalim Improve docs for Rails::Application and add routes_reloader_hook and …
josevalim authored
411 def helpers_paths #:nodoc:
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
412 config.helpers_paths
413 end
414
b058897 @matthewd Ensure we supply Kernel#y for 1.9 too
matthewd authored
415 console do
416 require "pp"
417 end
418
419 console do
420 unless ::Kernel.private_method_defined?(:y)
185d7cd @claudiob Remove Psych hack for Ruby 1.9
claudiob authored
421 require "psych/y"
b058897 @matthewd Ensure we supply Kernel#y for 1.9 too
matthewd authored
422 end
423 end
424
435e8d7 @huoxito Dont mess with default order engines load
huoxito authored
425 # Return an array of railties respecting the order they're loaded
426 # and the order specified by the +railties_order+ config.
427 #
d969405 @robin850 Some documentation edits [ci skip]
robin850 authored
428 # While running initializers we need engines in reverse order here when
429 # copying migrations from railties ; we need them in the order given by
430 # +railties_order+.
50de394 @arunagw rake railties:install:migrations respects the order of railties
arunagw authored
431 def migration_railties # :nodoc:
435e8d7 @huoxito Dont mess with default order engines load
huoxito authored
432 ordered_railties.flatten - [self]
50de394 @arunagw rake railties:install:migrations respects the order of railties
arunagw authored
433 end
434
2fe70c1 @josevalim Booya, sprockets now works from Engines.
josevalim authored
435 protected
436
437 alias :build_middleware_stack :app
612454e @josh Move sprockets initializers back to application
josh authored
438
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
439 def run_tasks_blocks(app) #:nodoc:
440 railties.each { |r| r.run_tasks_blocks(app) }
441 super
442 require "rails/tasks"
443 task :environment do
9cac69c @pftg #11381: Ignore config.eager_load=true for rake
pftg authored
444 ActiveSupport.on_load(:before_initialize) { config.eager_load = false }
445
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
446 require_environment!
447 end
448 end
449
450 def run_generators_blocks(app) #:nodoc:
451 railties.each { |r| r.run_generators_blocks(app) }
452 super
453 end
454
455 def run_runner_blocks(app) #:nodoc:
456 railties.each { |r| r.run_runner_blocks(app) }
457 super
458 end
459
460 def run_console_blocks(app) #:nodoc:
461 railties.each { |r| r.run_console_blocks(app) }
462 super
463 end
464
465 # Returns the ordered railties for this application considering railties_order.
466 def ordered_railties #:nodoc:
467 @ordered_railties ||= begin
468 order = config.railties_order.map do |railtie|
469 if railtie == :main_app
470 self
471 elsif railtie.respond_to?(:instance)
472 railtie.instance
473 else
474 railtie
475 end
476 end
477
478 all = (railties - order)
479 all.push(self) unless (all + order).include?(self)
480 order.push(:all) unless order.include?(:all)
481
482 index = order.index(:all)
483 order[index] = all
435e8d7 @huoxito Dont mess with default order engines load
huoxito authored
484 order
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
485 end
486 end
487
488 def railties_initializers(current) #:nodoc:
489 initializers = []
435e8d7 @huoxito Dont mess with default order engines load
huoxito authored
490 ordered_railties.reverse.flatten.each do |r|
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
491 if r == self
492 initializers += current
493 else
494 initializers += r.initializers
495 end
496 end
497 initializers
498 end
499
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
500 def default_middleware_stack #:nodoc:
685309c @wangjohn Creating a class to build the default middleware stack.
wangjohn authored
501 default_stack = DefaultMiddlewareStack.new(self, config, paths)
502 default_stack.build_stack
9ee6f3c @josevalim Bring config.allow_concurrency back
josevalim authored
503 end
504
484ff7d @wangjohn Removing a repetitive comment and removing a deprecation warning.
wangjohn authored
505 def validate_secret_key_config! #:nodoc:
db5f1a4 @bf4 `secret_token` is now saved in `Rails.application.secrets.secret_token`
bf4 authored
506 if secrets.secret_key_base.blank?
507 ActiveSupport::Deprecation.warn "You didn't set `secret_key_base`. " +
508 "Read the upgrade documentation to learn more about this new config option."
509
510 if secrets.secret_token.blank?
b93ae0c @Dan-Burnette Error message no longer tells you to set the (deprecated) secret_token.
Dan-Burnette authored
511 raise "Missing `secret_key_base` for '#{Rails.env}' environment, set this value in `config/secrets.yml`"
db5f1a4 @bf4 `secret_token` is now saved in `Rails.application.secrets.secret_token`
bf4 authored
512 end
484ff7d @wangjohn Removing a repetitive comment and removing a deprecation warning.
wangjohn authored
513 end
514 end
42070f8 @tenderlove Engines get different middleware than apps
tenderlove authored
515
516 private
517
8f8b7ae @tenderlove only construct one request in an engine
tenderlove authored
518 def build_request(env)
519 req = super
520 env["ORIGINAL_FULLPATH"] = req.fullpath
521 env["ORIGINAL_SCRIPT_NAME"] = req.script_name
522 req
523 end
524
42070f8 @tenderlove Engines get different middleware than apps
tenderlove authored
525 def build_middleware
526 config.app_middleware + super
527 end
accd9b4 @josh Restore "Start Rails::Application object
josh authored
528 end
17a91a6 @dlee Logs should show overridden method; Issue 426
dlee authored
529 end
Something went wrong with that request. Please try again.