diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2011-07-04 06:47:42 +0100 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2011-07-04 06:47:42 +0100 |
commit | 9f7442a3abf1242a2348bfdd0c700f707ee04c52 (patch) | |
tree | 531f7ce17ceb350dd64888873a41555b3441cc93 | |
parent | 78ff00d1964bdc911175ed29559d1382ace33d64 (diff) | |
download | rails-9f7442a3abf1242a2348bfdd0c700f707ee04c52.tar.gz rails-9f7442a3abf1242a2348bfdd0c700f707ee04c52.tar.bz2 rails-9f7442a3abf1242a2348bfdd0c700f707ee04c52.zip |
Use an instance variable to store the current masss assignment options
-rw-r--r-- | activerecord/lib/active_record/base.rb | 23 | ||||
-rw-r--r-- | activerecord/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 @@ MSG 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 @@ MSG 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 @@ module ActiveRecord 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 |