Skip to content

Commit

Permalink
Rewrite MergeErrorStrategy to work with Rails 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTsepelev committed May 6, 2019
1 parent 54558ca commit 38b47a3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 38b47a3

Please sign in to comment.