aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorDaniel Lopez Prat <daniel@6temes.cat>2018-11-21 08:54:22 +0900
committerDaniel Lopez Prat <daniel@6temes.cat>2018-11-21 08:56:19 +0900
commitee2b84f3cbd97d8116467ea0d44b8182824c83ab (patch)
treeb5a4f4cb32c73ae7515a0e65bba9ea89aacac352 /activemodel/lib
parent218e50ce59c3abfa5ffdfac253cdeb2f74602da0 (diff)
downloadrails-ee2b84f3cbd97d8116467ea0d44b8182824c83ab.tar.gz
rails-ee2b84f3cbd97d8116467ea0d44b8182824c83ab.tar.bz2
rails-ee2b84f3cbd97d8116467ea0d44b8182824c83ab.zip
Add slice! method to ActiveModel::Errors
Diffstat (limited to 'activemodel/lib')
-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"]