Permalink
Browse files

Use an instance variable to store the current masss assignment options

(cherry picked from commit 9f7442a)
  • Loading branch information...
1 parent d764378 commit cee27beb0c2d967f5a6a7442d0a505db714dbdaa @pixeltrix pixeltrix committed Jul 4, 2011
Showing with 18 additions and 13 deletions.
  1. +14 −9 activerecord/lib/active_record/base.rb
  2. +4 −4 activerecord/lib/active_record/nested_attributes.rb
@@ -1723,27 +1723,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
@@ -1904,6 +1901,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

0 comments on commit cee27be

Please sign in to comment.