Merge pull request #33882 from mberlanda/mberlanda/as-inheritable-options-intialization

[Realties] config_for as ActiveSupport::OrderedOptions
This commit is contained in:
Rafael França 2018-11-30 11:42:44 -05:00 committed by GitHub
commit 25c076117c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 37 deletions

@ -232,10 +232,12 @@ def config_for(name, env: Rails.env)
if yaml.exist?
require "erb"
require "active_support/ordered_options"
config = YAML.load(ERB.new(yaml.read).result) || {}
config = (config["shared"] || {}).merge(config[env] || {})
config = (YAML.load(ERB.new(yaml.read).result) || {})[env] || {}
ActiveSupport::InheritableOptions.new(config.deep_symbolize_keys)
ActiveSupport::OrderedOptions.new.tap do |config_as_ordered_options|
config_as_ordered_options.update(config.deep_symbolize_keys)
end
else
raise "Could not load configuration. No such file - #{yaml}"
end

@ -1736,21 +1736,6 @@ def index
assert_equal true, Rails.application.config.action_mailer.show_previews
end
test "config_for loads custom configuration from yaml files" do
app_file "config/custom.yml", <<-RUBY
development:
foo: 'bar'
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "development"
assert_equal "bar", Rails.application.config.my_custom_config["foo"]
end
test "config_for loads custom configuration from yaml accessible as symbol" do
app_file "config/custom.yml", <<-RUBY
development:
@ -1766,21 +1751,6 @@ def index
assert_equal "bar", Rails.application.config.my_custom_config[:foo]
end
test "config_for loads custom configuration from yaml accessible as method" do
app_file "config/custom.yml", <<-RUBY
development:
foo: 'bar'
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "development"
assert_equal "bar", Rails.application.config.my_custom_config.foo
end
test "config_for loads nested custom configuration from yaml as symbol keys" do
app_file "config/custom.yml", <<-RUBY
development:
@ -1795,7 +1765,31 @@ def index
app "development"
assert_equal 1, Rails.application.config.my_custom_config.foo[:bar][:baz]
assert_equal 1, Rails.application.config.my_custom_config[:foo][:bar][:baz]
end
test "config_for makes all hash methods available" do
app_file "config/custom.yml", <<-RUBY
development:
foo: 0
bar:
baz: 1
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "development"
actual = Rails.application.config.my_custom_config
assert_equal actual, foo: 0, bar: { baz: 1 }
assert_equal actual.keys, [ :foo, :bar ]
assert_equal actual.values, [ 0, baz: 1]
assert_equal actual.to_h, foo: 0, bar: { baz: 1 }
assert_equal actual[:foo], 0
assert_equal actual[:bar], baz: 1
end
test "config_for uses the Pathname object if it is provided" do
@ -1810,7 +1804,7 @@ def index
app "development"
assert_equal "custom key", Rails.application.config.my_custom_config["key"]
assert_equal "custom key", Rails.application.config.my_custom_config[:key]
end
test "config_for raises an exception if the file does not exist" do
@ -1840,6 +1834,40 @@ def index
assert_equal({}, Rails.application.config.my_custom_config)
end
test "config_for implements shared configuration as secrets case found" do
app_file "config/custom.yml", <<-RUBY
shared:
foo: :bar
test:
foo: :baz
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "test"
assert_equal(:baz, Rails.application.config.my_custom_config[:foo])
end
test "config_for implements shared configuration as secrets case not found" do
app_file "config/custom.yml", <<-RUBY
shared:
foo: :bar
test:
foo: :baz
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "development"
assert_equal(:bar, Rails.application.config.my_custom_config[:foo])
end
test "config_for with empty file returns an empty hash" do
app_file "config/custom.yml", <<-RUBY
RUBY
@ -1909,7 +1937,7 @@ class Post < ActiveRecord::Base
app "development"
assert_equal "custom key", Rails.application.config.my_custom_config["key"]
assert_equal "custom key", Rails.application.config.my_custom_config[:key]
end
test "config_for with syntax error show a more descriptive exception" do
@ -1942,7 +1970,7 @@ class Post < ActiveRecord::Base
RUBY
require "#{app_path}/config/environment"
assert_equal "unicorn", Rails.application.config.my_custom_config["key"]
assert_equal "unicorn", Rails.application.config.my_custom_config[:key]
end
test "api_only is false by default" do