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.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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue