diff options
-rw-r--r-- | railties/lib/rails/application.rb | 8 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 80 |
2 files changed, 59 insertions, 29 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 656786246d..acd97b64bf 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -232,10 +232,12 @@ module Rails 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 diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index b8a0434432..3635de7162 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1736,21 +1736,6 @@ module ApplicationTests 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,10 +1751,12 @@ module ApplicationTests assert_equal "bar", Rails.application.config.my_custom_config[:foo] end - test "config_for loads custom configuration from yaml accessible as method" do + test "config_for loads nested custom configuration from yaml as symbol keys" do app_file "config/custom.yml", <<-RUBY development: - foo: 'bar' + foo: + bar: + baz: 1 RUBY add_to_config <<-RUBY @@ -1778,15 +1765,15 @@ module ApplicationTests app "development" - assert_equal "bar", Rails.application.config.my_custom_config.foo + assert_equal 1, Rails.application.config.my_custom_config[:foo][:bar][:baz] end - test "config_for loads nested custom configuration from yaml as symbol keys" do + test "config_for makes all hash methods available" do app_file "config/custom.yml", <<-RUBY development: - foo: - bar: - baz: 1 + foo: 0 + bar: + baz: 1 RUBY add_to_config <<-RUBY @@ -1795,7 +1782,14 @@ module ApplicationTests app "development" - assert_equal 1, Rails.application.config.my_custom_config.foo[:bar][:baz] + 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 @@ module ApplicationTests 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 @@ module ApplicationTests 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 @@ module ApplicationTests 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 @@ module ApplicationTests 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 |