aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorlulalala <mark@goodlife.tw>2018-12-23 10:33:57 +0800
committerlulalala <mark@goodlife.tw>2019-03-31 22:59:12 +0800
commitba38b40e83302cb9da17c95086d93e5071426668 (patch)
treeb79de034672a468a013332598a79c666057aeac8 /activemodel
parentabee0343686b476139c476952b1c68f1fba6b3d0 (diff)
downloadrails-ba38b40e83302cb9da17c95086d93e5071426668.tar.gz
rails-ba38b40e83302cb9da17c95086d93e5071426668.tar.bz2
rails-ba38b40e83302cb9da17c95086d93e5071426668.zip
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.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/errors.rb39
1 files changed, 21 insertions, 18 deletions
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.
#