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:
commit
df186bd16f
4 changed files with 16 additions and 11 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue