提交 9f7442a3 编写于 作者: A Andrew White

Use an instance variable to store the current masss assignment options

上级 78ff00d1
......@@ -1709,27 +1709,24 @@ def assign_attributes(new_attributes, options = {})
return unless new_attributes
attributes = new_attributes.stringify_keys
role = options[:as] || :default
multi_parameter_attributes = []
@mass_assignment_options = options
unless options[:without_protection]
attributes = sanitize_for_mass_assignment(attributes, role)
attributes = sanitize_for_mass_assignment(attributes, mass_assignment_role)
end
attributes.each do |k, v|
if k.include?("(")
multi_parameter_attributes << [ k, v ]
elsif respond_to?("#{k}=")
send("#{k}=", v)
else
method_name = "#{k}="
if respond_to?(method_name)
method(method_name).arity == -2 ? send(method_name, v, options) : send(method_name, v)
else
raise(UnknownAttributeError, "unknown attribute: #{k}")
end
raise(UnknownAttributeError, "unknown attribute: #{k}")
end
end
@mass_assignment_options = nil
assign_multiparameter_attributes(multi_parameter_attributes)
end
......@@ -1894,6 +1891,14 @@ def clone_attribute_value(reader_method, attribute_name)
value
end
def mass_assignment_options
@mass_assignment_options ||= {}
end
def mass_assignment_role
mass_assignment_options[:as] || :default
end
private
# Under Ruby 1.9, Array#flatten will call #to_ary (recursively) on each of the elements
......
......@@ -276,15 +276,15 @@ def accepts_nested_attributes_for(*attr_names)
type = (reflection.collection? ? :collection : :one_to_one)
# def pirate_attributes=(attributes, assignment_opts = {})
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes, assignment_opts)
# def pirate_attributes=(attributes)
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes, mass_assignment_options)
# end
class_eval <<-eoruby, __FILE__, __LINE__ + 1
if method_defined?(:#{association_name}_attributes=)
remove_method(:#{association_name}_attributes=)
end
def #{association_name}_attributes=(attributes, assignment_opts = {})
assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, assignment_opts)
def #{association_name}_attributes=(attributes)
assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, mass_assignment_options)
end
eoruby
else
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册