Copy argument in AttributeAssignment#attributes=
Before df186bd16f
, `assign_attributes` and
`attributes=` were both defined in Active Model and both made a copy of
their argument. Now `assign_attributes` is overridden in Active Record
and the copy happens there instead, but `attributes=` isn't overridden.
This meant that assigning nested or multi-parameters via `attributes=`
would mutate the argument, which the copying was meant to prevent.
This commit is contained in:
parent
0cb6c27344
commit
4f92aa6741
|
@ -10,6 +10,8 @@ module ActiveRecord
|
||||||
super(attributes.dup)
|
super(attributes.dup)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias attributes= assign_attributes
|
||||||
|
|
||||||
private
|
private
|
||||||
def _assign_attributes(attributes)
|
def _assign_attributes(attributes)
|
||||||
multi_parameter_attributes = {}
|
multi_parameter_attributes = {}
|
||||||
|
|
|
@ -279,14 +279,16 @@ class AttributeMethodsTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "hashes are not mangled" do
|
test "hashes are not mangled" do
|
||||||
new_topic = { title: "New Topic" }
|
new_topic = { title: "New Topic", content: { key: "First value" } }
|
||||||
new_topic_values = { title: "AnotherTopic" }
|
new_topic_values = { title: "AnotherTopic", content: { key: "Second value" } }
|
||||||
|
|
||||||
topic = Topic.new(new_topic)
|
topic = Topic.new(new_topic)
|
||||||
assert_equal new_topic[:title], topic.title
|
assert_equal new_topic[:title], topic.title
|
||||||
|
assert_equal new_topic[:content], topic.content
|
||||||
|
|
||||||
topic.attributes = new_topic_values
|
topic.attributes = new_topic_values
|
||||||
assert_equal new_topic_values[:title], topic.title
|
assert_equal new_topic_values[:title], topic.title
|
||||||
|
assert_equal new_topic_values[:content], topic.content
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create through factory" do
|
test "create through factory" do
|
||||||
|
|
Loading…
Reference in New Issue