aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/errors.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model/errors.rb')
-rw-r--r--activemodel/lib/active_model/errors.rb33
1 files changed, 20 insertions, 13 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 4b8b601b4b..81a65a60a7 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -151,7 +151,7 @@ module ActiveModel
keys = keys.map(&:to_sym)
- results = messages.slice!(*keys)
+ results = messages.dup.slice!(*keys)
@errors.keep_if do |error|
keys.include?(error.attribute)
@@ -625,16 +625,7 @@ module ActiveModel
class DeprecationHandlingMessageHash < SimpleDelegator
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)
+ super(prepare_content)
end
def []=(attribute, value)
@@ -644,15 +635,31 @@ module ActiveModel
@errors.add(attribute, message)
end
- super(attribute, DeprecationHandlingMessageArray.new(@errors.messages_for(attribute), @errors, attribute))
+ __setobj__ prepare_content
end
+
+ private
+
+ def prepare_content
+ 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 = hash.dup
+ hash[attribute] = DeprecationHandlingMessageArray.new([], @errors, attribute)
+ __setobj__ hash.freeze
+ hash[attribute]
+ end
+ content.freeze
+ end
end
class DeprecationHandlingMessageArray < SimpleDelegator
def initialize(content, errors, attribute)
@errors = errors
@attribute = attribute
- super(content)
+ super(content.freeze)
end
def <<(message)