Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite MergeErrorStrategy to work with Rails 6.1 #6

Merged
merged 1 commit into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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