diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae8d58..2be1911 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +- [PR #6](https://github.com/DmitryTsepelev/store_model/pull/6) Rewrite MergeErrorStrategy to work with Rails 6.1 ([@DmitryTsepelev][]) + ## 0.2.0 (2019-04-30) - [PR #5](https://github.com/DmitryTsepelev/store_model/pull/5) Raise error when `#cast` cannot handle the passed instance ([@DmitryTsepelev][]) diff --git a/README.md b/README.md index 5d8463d..6c2c379 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,8 @@ You can change the global behavior using `StoreModel.config`: StoreModel.config.merge_errors = true ``` +> Heads up! Due to the [changes](https://github.com/rails/rails/pull/32313) of error internals in Rails >= 6.1 it's impossible to add an error with a key that does not have a corresponding attribute with the same name. Because of that, behavior of `merge_error` strategy will be different - all errors are going to be placed under the attribute name (`{ configuration: ["Color can't be blank"] }` instead of `{ color: ["can't be blank"] }`). + You can also add your own custom strategies to handle errors. All you need to do is to provide a callable object to `StoreModel.config.merge_errors` or as value of `:merge_errors`. It should accept three arguments - _attribute_, _base_errors_ and _store_model_errors_: ```ruby diff --git a/lib/store_model/combine_errors_strategies/merge_error_strategy.rb b/lib/store_model/combine_errors_strategies/merge_error_strategy.rb index 767e73d..5219814 100644 --- a/lib/store_model/combine_errors_strategies/merge_error_strategy.rb +++ b/lib/store_model/combine_errors_strategies/merge_error_strategy.rb @@ -4,7 +4,13 @@ module StoreModel module CombineErrorsStrategies class MergeErrorStrategy def call(_attribute, base_errors, store_model_errors) - base_errors.copy!(store_model_errors) + if Rails::VERSION::MAJOR < 6 || Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR.zero? + base_errors.copy!(store_model_errors) + else + store_model_errors.errors.each do |error| + base_errors.add(:configuration, :invalid, message: error.full_message) + end + end end end end diff --git a/spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb b/spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb index 6a9e6b8..7207d82 100644 --- a/spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb +++ b/spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb @@ -19,8 +19,13 @@ it "adds message that associated object is invalid" do described_class.new.call(:configuration, record.errors, record.configuration.errors) - expect(record.errors.messages).to eq(color: ["can't be blank"]) - expect(record.errors.full_messages).to eq(["Color can't be blank"]) + if Rails::VERSION::MAJOR < 6 || Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 0 + expect(record.errors.messages).to eq(color: ["can't be blank"]) + expect(record.errors.full_messages).to eq(["Color can't be blank"]) + else + expect(record.errors.messages).to eq(configuration: ["Color can't be blank"]) + expect(record.errors.full_messages).to eq(["Configuration Color can't be blank"]) + end expect(record.configuration.errors.messages).to eq(color: ["can't be blank"]) expect(record.configuration.errors.full_messages).to eq(["Color can't be blank"])