From ba38b40e83302cb9da17c95086d93e5071426668 Mon Sep 17 00:00:00 2001 From: lulalala Date: Sun, 23 Dec 2018 10:33:57 +0800 Subject: Split messages and to_hash Fix double wrapping issue Revert messages_for wrapping. It's a new method so no need to put deprecation warnings. --- activemodel/lib/active_model/errors.rb | 39 ++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 5440988d27..4b8b601b4b 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -208,7 +208,7 @@ module ActiveModel # person.errors[:name] # => ["cannot be nil"] # person.errors['name'] # => ["cannot be nil"] def [](attribute) - messages_for(attribute) + DeprecationHandlingMessageArray.new(messages_for(attribute), self, attribute) end # Iterates through each error key, value pair in the error messages hash. @@ -302,13 +302,16 @@ module ActiveModel # person.errors.to_hash(true) # => {:name=>["name cannot be nil"]} def to_hash(full_messages = false) hash = {} - message_method = full_messages ? :full_messages_for : :messages_for + message_method = full_messages ? :full_message : :message group_by_attribute.each do |attribute, errors| - hash[attribute] = public_send(message_method, attribute) + hash[attribute] = errors.map(&message_method) end - DeprecationHandlingMessageHash.new(hash, self) + hash + end + + def messages + DeprecationHandlingMessageHash.new(self) end - alias :messages :to_hash def details hash = {} @@ -458,7 +461,7 @@ module ActiveModel end def messages_for(attribute) - DeprecationHandlingMessageArray.new(where(attribute).map(&:message).freeze, self, attribute) + where(attribute).map(&:message) end # Returns a full message for a given attribute. @@ -620,8 +623,17 @@ module ActiveModel end class DeprecationHandlingMessageHash < SimpleDelegator - def initialize(content, errors) + def initialize(errors) @errors = errors + + content = @errors.to_hash + content.each do |attribute, value| + content[attribute] = DeprecationHandlingMessageArray.new(value, @errors, attribute) + end + content.default_proc = proc do |hash, attribute| + hash[attribute] = DeprecationHandlingMessageArray.new([], @errors, attribute) + end + super(content) end @@ -631,16 +643,8 @@ module ActiveModel Array(value).each do |message| @errors.add(attribute, message) end - __setobj__ @errors.messages - value - end - - def [](attribute) - messages = super(attribute) - - return messages if messages - __getobj__[attribute]= DeprecationHandlingMessageArray.new([], @errors, attribute) + super(attribute, DeprecationHandlingMessageArray.new(@errors.messages_for(attribute), @errors, attribute)) end end @@ -655,12 +659,11 @@ module ActiveModel ActiveSupport::Deprecation.warn("Calling `<<` to an ActiveModel::Errors message array in order to add an error is deprecated. Please call `ActiveModel::Errors#add` instead.") @errors.add(@attribute, message) - __setobj__ @errors[@attribute] + __setobj__ @errors.messages_for(@attribute) self end end - # Raised when a validation cannot be corrected by end users and are considered # exceptional. # -- cgit v1.2.3