From 9f7442a3abf1242a2348bfdd0c700f707ee04c52 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 4 Jul 2011 06:47:42 +0100 Subject: [PATCH] Use an instance variable to store the current masss assignment options --- activerecord/lib/active_record/base.rb | 23 +++++++++++-------- .../lib/active_record/nested_attributes.rb | 8 +++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c2383630e0..3b8df4f2f1 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -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 diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index f51fd21077..53617059d0 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -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 -- GitLab