aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-11-21 00:49:01 -0500
committerGitHub <noreply@github.com>2018-11-21 00:49:01 -0500
commit32695069b10a79c13312b189d6508f8ed4c6186a (patch)
tree4d52961dadfbfdb1a137da25880ead554edfc69f /activemodel/lib/active_model
parent9fb53e7b198a0b497566f06ba938d66502fa4326 (diff)
parentee2b84f3cbd97d8116467ea0d44b8182824c83ab (diff)
downloadrails-32695069b10a79c13312b189d6508f8ed4c6186a.tar.gz
rails-32695069b10a79c13312b189d6508f8ed4c6186a.tar.bz2
rails-32695069b10a79c13312b189d6508f8ed4c6186a.zip
Merge pull request #34489 from 6temes/add_slice_method_to_errors
Add slice! method to ActiveModel::Errors
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/errors.rb11
1 files changed, 11 insertions, 0 deletions
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"]