1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

View file

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

View file

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

View file

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