diff options
author | Kenichi Kamiya <kachick1@gmail.com> | 2016-02-15 01:41:59 +0900 |
---|---|---|
committer | Kenichi Kamiya <kachick1@gmail.com> | 2016-11-22 04:10:41 +0900 |
commit | 9f566aba3278e6e64beb38a88c02a3528760c730 (patch) | |
tree | 3b579474d01c159fcd422e40c7f79b992d7bf301 /activemodel/lib/active_model | |
parent | 2b4a9735d816d3a05c7fca207d97bebcb09c95a0 (diff) | |
download | rails-9f566aba3278e6e64beb38a88c02a3528760c730.tar.gz rails-9f566aba3278e6e64beb38a88c02a3528760c730.tar.bz2 rails-9f566aba3278e6e64beb38a88c02a3528760c730.zip |
Allow indifferent access in ActiveModel::Errors
`#[]` has already applied indifferent access, but some methods does not.
`#include?`, `#has_key?`, `#key?`, `#delete` and `#full_messages_for`.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 14adfa081f..5ee9413cff 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -110,6 +110,7 @@ module ActiveModel # person.errors.include?(:name) # => true # person.errors.include?(:age) # => false def include?(attribute) + attribute = attribute.to_sym messages.key?(attribute) && messages[attribute].present? end alias :has_key? :include? @@ -121,8 +122,9 @@ module ActiveModel # person.errors.delete(:name) # => ["cannot be nil"] # person.errors[:name] # => [] def delete(key) - details.delete(key) - messages.delete(key) + attribute = key.to_sym + details.delete(attribute) + messages.delete(attribute) end # When passed a symbol or a name of a method, returns an array of errors @@ -342,6 +344,7 @@ module ActiveModel # person.errors.full_messages_for(:name) # # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"] def full_messages_for(attribute) + attribute = attribute.to_sym messages[attribute].map { |message| full_message(attribute, message) } end |