aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorlulalala <mark@goodlife.tw>2019-01-19 09:53:25 +0800
committerlulalala <mark@goodlife.tw>2019-03-31 22:59:13 +0800
commit514c4b4d5319f10a275813f1af5a21ed3f41263d (patch)
tree792707388a321b607d4b1cf3ac297555fb503baf /activemodel
parente7834214a668cde0a4f7757f7f4a3d78f73f2fd8 (diff)
downloadrails-514c4b4d5319f10a275813f1af5a21ed3f41263d.tar.gz
rails-514c4b4d5319f10a275813f1af5a21ed3f41263d.tar.bz2
rails-514c4b4d5319f10a275813f1af5a21ed3f41263d.zip
Freeze DeprecationHandling array and hash
Diffstat (limited to 'activemodel')
-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)