aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorbogdanvlviv <bogdanvlviv@gmail.com>2017-03-28 12:05:14 +0300
committerbogdanvlviv <bogdanvlviv@gmail.com>2017-03-28 18:48:56 +0300
commit01269aede398f63e5ef68ebe0f0dafbc472c686e (patch)
treeeeb2a2fc509cb384d012e7ef5147aa09329ecbcd /activemodel/lib
parentd28c48243521a978ba8d432092e11a4aafb2b154 (diff)
downloadrails-01269aede398f63e5ef68ebe0f0dafbc472c686e.tar.gz
rails-01269aede398f63e5ef68ebe0f0dafbc472c686e.tar.bz2
rails-01269aede398f63e5ef68ebe0f0dafbc472c686e.zip
Fix ActiveModel::Errors #keys, #values
Before: person.errors.keys # => [] person.errors.values # => [] person.errors[:name] # => [] person.errors.keys # => [:name] person.errors.values # => [[]] After: person.errors.keys # => [] person.errors.values # => [] person.errors[:name] # => [] person.errors.keys # => [] person.errors.values # => [] Related to #23468
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/errors.rb17
1 files changed, 6 insertions, 11 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 9df4ca51fe..5d3472802b 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -132,15 +132,6 @@ module ActiveModel
#
# person.errors[:name] # => ["cannot be nil"]
# person.errors['name'] # => ["cannot be nil"]
- #
- # Note that, if you try to get errors of an attribute which has
- # no errors associated with it, this method will instantiate
- # an empty error list for it and +keys+ will return an array
- # of error keys which includes this attribute.
- #
- # person.errors.keys # => []
- # person.errors[:name] # => []
- # person.errors.keys # => [:name]
def [](attribute)
messages[attribute.to_sym]
end
@@ -181,7 +172,9 @@ module ActiveModel
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
# person.errors.values # => [["cannot be nil", "must be specified"]]
def values
- messages.values
+ messages.reject do |key, value|
+ value.empty?
+ end.values
end
# Returns all message keys.
@@ -189,7 +182,9 @@ module ActiveModel
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
# person.errors.keys # => [:name]
def keys
- messages.keys
+ messages.reject do |key, value|
+ value.empty?
+ end.keys
end
# Returns +true+ if no errors are found, +false+ otherwise.