Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion guides/source/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
baz: 1
baz: 1


development:
foo:
bar:
qux: 2
```


```ruby
# development environment
Rails.application.config_for(:example)[:foo][:bar] #=> { baz: 1, qux: 2 }
```

Search Engines Indexing
-----------------------
Expand Down
21 changes: 20 additions & 1 deletion railties/lib/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ def message_verifier(verifier_name)

# 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
Expand All @@ -220,6 +221,24 @@ def message_verifier(verifier_name)
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# baz: 1
# 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")

Expand Down