1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add mention to shared section in config_for docs [ci skip]

37913 added the possibility to deeply merge configurations by grouping
them within a shared section. This powerful alternative was not reflected
in any documentation, which made my team think it was not possible until
I found out this feature after looking at the source code.

This patch reflects this change in the documentation so that it is
easier for other developers to know about this behavior.
This commit is contained in:
Alberto Almagro 2020-05-01 14:35:38 +02:00
parent 348e142b26
commit 8bae32ba2c
2 changed files with 44 additions and 2 deletions

View file

@ -1592,13 +1592,14 @@ These configuration points are then available through the configuration object:
You can also use `Rails::Application.config_for` to load whole configuration files:
```ruby
```yaml
# config/payment.yml:
production:
environment: production
merchant_id: production_merchant_id
public_key: production_public_key
private_key: production_private_key
development:
environment: sandbox
merchant_id: development_merchant_id
@ -1616,6 +1617,28 @@ You can also use `Rails::Application.config_for` to load whole configuration fil
```ruby
Rails.configuration.payment['merchant_id'] # => production_merchant_id or development_merchant_id
```
`Rails::Application.config_for` supports a `shared` configuration to group common
configurations. The shared configuration will be merged into the environment
configuration.
```yaml
# config/example.yml
shared:
foo:
bar:
baz: 1
development:
foo:
bar:
qux: 2
```
```ruby
# development environment
Rails.application.config_for(:example)[:foo][:bar] #=> { baz: 1, qux: 2 }
```
Search Engines Indexing
-----------------------

View file

@ -206,12 +206,13 @@ module Rails
# Convenience for loading config/foo.yml for the current Rails env.
#
# Example:
# Examples:
#
# # config/exception_notification.yml:
# production:
# url: http://127.0.0.1:8080
# namespace: my_app_production
#
# development:
# url: http://localhost:3001
# namespace: my_app_development
@ -220,6 +221,24 @@ module Rails
# Rails.application.configure do
# config.middleware.use ExceptionNotifier, config_for(:exception_notification)
# end
#
# # You can also store configurations in a shared section which will be
# # merged with the environment configuration
#
# # config/example.yml
# shared:
# foo:
# bar:
# baz: 1
#
# development:
# foo:
# bar:
# qux: 2
#
# # development environment
# Rails.application.config_for(:example)[:foo][:bar]
# # => { baz: 1, qux: 2 }
def config_for(name, env: Rails.env)
yaml = name.is_a?(Pathname) ? name : Pathname.new("#{paths["config"].existent.first}/#{name}.yml")