From 9f566aba3278e6e64beb38a88c02a3528760c730 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Mon, 15 Feb 2016 01:41:59 +0900 Subject: 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`. --- activemodel/lib/active_model/errors.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activemodel/lib/active_model') 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 -- cgit v1.2.3