diff options
author | Eileen M. Uchitelle <eileencodes@users.noreply.github.com> | 2019-02-14 12:58:54 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-14 12:58:54 -0500 |
commit | 5f7180676bd60b44566a24b293376d54ff5d0d94 (patch) | |
tree | 1a78f0cd5d52715a406ed897f57accc683f29b82 /activerecord/lib | |
parent | e53430fa9af239e21e11548499d814f540d421e5 (diff) | |
parent | 06f9434342d636b18229953a8f50ee5d989f4c75 (diff) | |
download | rails-5f7180676bd60b44566a24b293376d54ff5d0d94.tar.gz rails-5f7180676bd60b44566a24b293376d54ff5d0d94.tar.bz2 rails-5f7180676bd60b44566a24b293376d54ff5d0d94.zip |
Merge pull request #35242 from eileencodes/add-setter-and-deprecation-for-configurations-hashes
Add setter and deprecation for configurations hashes
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/database_configurations.rb | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/database_configurations.rb b/activerecord/lib/active_record/database_configurations.rb index 654a01f23b..9879b01d3a 100644 --- a/activerecord/lib/active_record/database_configurations.rb +++ b/activerecord/lib/active_record/database_configurations.rb @@ -102,6 +102,7 @@ module ActiveRecord def build_configs(configs) return configs.configurations if configs.is_a?(DatabaseConfigurations) + return configs if configs.is_a?(Array) build_db_config = configs.each_pair.flat_map do |env_name, config| walk_configs(env_name.to_s, "primary", config) @@ -166,21 +167,38 @@ module ActiveRecord end def method_missing(method, *args, &blk) - if Hash.method_defined?(method) - ActiveSupport::Deprecation.warn \ - "Returning a hash from ActiveRecord::Base.configurations is deprecated. Therefore calling `#{method}` on the hash is also deprecated. Please switch to using the `configs_for` method instead to collect and iterate over database configurations." - end - case method when :each, :first + throw_getter_deprecation(method) configurations.send(method, *args, &blk) when :fetch + throw_getter_deprecation(method) configs_for(env_name: args.first) when :values + throw_getter_deprecation(method) configurations.map(&:config) + when :[]= + throw_setter_deprecation(method) + + env_name = args[0] + config = args[1] + + remaining_configs = configurations.reject { |db_config| db_config.env_name == env_name } + new_config = build_configs(env_name => config) + new_configs = remaining_configs + new_config + + ActiveRecord::Base.configurations = new_configs else - super + raise NotImplementedError, "`ActiveRecord::Base.configurations` in Rails 6 now returns an object instead of a hash. The #{method} method is not supported. Please use `configs_for` or consult the documentation for supported methods." end end + + def throw_setter_deprecation(method) + ActiveSupport::Deprecation.warn("Setting `ActiveRecord::Base.configurations` with `#{method}` is deprecated. Use `ActiveRecord::Base.configurations=` directly to set the configurations instead.") + end + + def throw_getter_deprecation(method) + ActiveSupport::Deprecation.warn("`ActiveRecord::Base.configurations` no longer returns a hash. Methods that act on the hash like `#{method}` are deprecated and will be removed in Rails 6.1. Use the `configs_for` method to collect and iterate over the database configurations.") + end end end |