diff options
author | Daniel Lopez Prat <daniel@6temes.cat> | 2018-11-21 08:54:22 +0900 |
---|---|---|
committer | Daniel Lopez Prat <daniel@6temes.cat> | 2018-11-21 08:56:19 +0900 |
commit | ee2b84f3cbd97d8116467ea0d44b8182824c83ab (patch) | |
tree | b5a4f4cb32c73ae7515a0e65bba9ea89aacac352 /activemodel | |
parent | 218e50ce59c3abfa5ffdfac253cdeb2f74602da0 (diff) | |
download | rails-ee2b84f3cbd97d8116467ea0d44b8182824c83ab.tar.gz rails-ee2b84f3cbd97d8116467ea0d44b8182824c83ab.tar.bz2 rails-ee2b84f3cbd97d8116467ea0d44b8182824c83ab.zip |
Add slice! method to ActiveModel::Errors
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 11 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 24 |
3 files changed, 39 insertions, 0 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 6048911217..0bc8728e36 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add `#slice!` method to `ActiveModel::Errors`. + + *Daniel López Prat* + * Fix numericality validator to still use value before type cast except Active Record. Fixes #33651, #33686. diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 9de6b609a3..969effdc20 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -112,6 +112,17 @@ module ActiveModel @details.merge!(other.details) { |_, ary1, ary2| ary1 + ary2 } end + # Removes all errors except the given keys. Returns a hash containing the removed errors. + # + # person.errors.keys # => [:name, :age, :gender, :city] + # person.errors.slice!(:age, :gender) # => { :name=>["cannot be nil"], :city=>["cannot be nil"] } + # person.errors.keys # => [:age, :gender] + def slice!(*keys) + keys = keys.map(&:to_sym) + @details.slice!(*keys) + @messages.slice!(*keys) + end + # Clear the error messages. # # person.errors.full_messages # => ["name cannot be nil"] diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 185b5a24ae..f9015b869d 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -411,6 +411,30 @@ class ErrorsTest < ActiveModel::TestCase assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details) end + test "slice! removes all errors except the given keys" do + person = Person.new + person.errors.add(:name, "cannot be nil") + person.errors.add(:age, "cannot be nil") + person.errors.add(:gender, "cannot be nil") + person.errors.add(:city, "cannot be nil") + + person.errors.slice!(:age, "gender") + + assert_equal [:age, :gender], person.errors.keys + end + + test "slice! returns the deleted errors" do + person = Person.new + person.errors.add(:name, "cannot be nil") + person.errors.add(:age, "cannot be nil") + person.errors.add(:gender, "cannot be nil") + person.errors.add(:city, "cannot be nil") + + removed_errors = person.errors.slice!(:age, "gender") + + assert_equal({ name: ["cannot be nil"], city: ["cannot be nil"] }, removed_errors) + end + test "errors are marshalable" do errors = ActiveModel::Errors.new(Person.new) errors.add(:name, :invalid) |