Merge pull request #38401 from vinistock/stop_stringifying_during_attribute_assignment

Do not stringify attributes in assign_attributes
This commit is contained in:
Rafael França 2020-02-13 16:54:35 -05:00 committed by GitHub
commit df186bd16f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 11 deletions

View File

@ -26,13 +26,12 @@ module ActiveModel
# cat.name # => 'Gorby'
# cat.status # => 'sleeping'
def assign_attributes(new_attributes)
if !new_attributes.respond_to?(:stringify_keys)
unless new_attributes.respond_to?(:each_pair)
raise ArgumentError, "When assigning attributes, you must pass a hash as an argument, #{new_attributes.class} passed."
end
return if new_attributes.empty?
attributes = new_attributes.stringify_keys
_assign_attributes(sanitize_for_mass_assignment(attributes))
_assign_attributes(sanitize_for_mass_assignment(new_attributes))
end
alias attributes= assign_attributes
@ -49,7 +48,7 @@ module ActiveModel
if respond_to?(setter)
public_send(setter, v)
else
raise UnknownAttributeError.new(self, k)
raise UnknownAttributeError.new(self, k.to_s)
end
end
end

View File

@ -49,8 +49,8 @@ class AttributeAssignmentTest < ActiveModel::TestCase
@parameters
end
def stringify_keys
dup
def each_pair(&block)
@parameters.each_pair(&block)
end
def dup

View File

@ -6,16 +6,22 @@ module ActiveRecord
module AttributeAssignment
include ActiveModel::AttributeAssignment
def assign_attributes(attributes)
super(attributes.dup)
end
private
def _assign_attributes(attributes)
multi_parameter_attributes = {}
nested_parameter_attributes = {}
attributes.each do |k, v|
if k.include?("(")
multi_parameter_attributes[k] = attributes.delete(k)
key = k.to_s
if key.include?("(")
multi_parameter_attributes[key] = attributes.delete(k)
elsif v.is_a?(Hash)
nested_parameter_attributes[k] = attributes.delete(k)
nested_parameter_attributes[key] = attributes.delete(k)
end
end
super(attributes)

View File

@ -28,8 +28,8 @@ class ProtectedParams
end
alias to_unsafe_h to_h
def stringify_keys
dup
def each_pair(&block)
@parameters.each_pair(&block)
end
def dup