aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-10-09 15:37:10 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-10-09 15:47:10 +0100
commite94caf0788df87b139e575f33cdeea12b06f2609 (patch)
treea5ea5d14a5265bf263772921513abf793ae9ded5 /activerecord
parent987d501182971c1e6cdf8acb748635d14cf0c341 (diff)
downloadrails-e94caf0788df87b139e575f33cdeea12b06f2609.tar.gz
rails-e94caf0788df87b139e575f33cdeea12b06f2609.tar.bz2
rails-e94caf0788df87b139e575f33cdeea12b06f2609.zip
Store entire options hash in the class var rather than just the reject_if proc for the nested attributes
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb27
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb8
2 files changed, 19 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index ec6c02db38..05c7e9713f 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -6,8 +6,8 @@ module ActiveRecord
extend ActiveSupport::Concern
included do
- class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false
- self.reject_new_nested_attributes_procs = {}
+ class_inheritable_accessor :nested_attributes_options, :instance_writer => false
+ self.nested_attributes_options = {}
end
# == Nested Attributes
@@ -227,10 +227,10 @@ module ActiveRecord
reflection.options[:autosave] = true
- self.reject_new_nested_attributes_procs[association_name.to_sym] = if options[:reject_if] == :all_blank
- proc { |attributes| attributes.all? {|k,v| v.blank?} }
- else
- options[:reject_if]
+ self.nested_attributes_options[association_name.to_sym] = options
+
+ if options[:reject_if] == :all_blank
+ self.nested_attributes_options[association_name.to_sym][:reject_if] = proc { |attributes| attributes.all? {|k,v| v.blank?} }
end
# def pirate_attributes=(attributes)
@@ -238,7 +238,7 @@ module ActiveRecord
# end
class_eval %{
def #{association_name}_attributes=(attributes)
- assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, #{options[:allow_destroy]})
+ assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
end
}, __FILE__, __LINE__
else
@@ -282,7 +282,8 @@ module ActiveRecord
# If the given attributes include a matching <tt>:id</tt> attribute _and_ a
# <tt>:_destroy</tt> key set to a truthy value, then the existing record
# will be marked for destruction.
- def assign_nested_attributes_for_one_to_one_association(association_name, attributes, allow_destroy)
+ def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
+ options = self.nested_attributes_options[association_name]
attributes = attributes.with_indifferent_access
if attributes['id'].blank?
@@ -295,7 +296,7 @@ module ActiveRecord
end
end
elsif (existing_record = send(association_name)) && existing_record.id.to_s == attributes['id'].to_s
- assign_to_or_mark_for_destruction(existing_record, attributes, allow_destroy)
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
end
end
@@ -326,7 +327,9 @@ module ActiveRecord
# { :name => 'John' },
# { :id => '2', :_destroy => true }
# ])
- def assign_nested_attributes_for_collection_association(association_name, attributes_collection, allow_destroy)
+ def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
+ options = self.nested_attributes_options[association_name]
+
unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
end
@@ -343,7 +346,7 @@ module ActiveRecord
send(association_name).build(attributes.except(*UNASSIGNABLE_KEYS))
end
elsif existing_record = send(association_name).detect { |record| record.id.to_s == attributes['id'].to_s }
- assign_to_or_mark_for_destruction(existing_record, attributes, allow_destroy)
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
end
end
end
@@ -372,7 +375,7 @@ module ActiveRecord
end
def call_reject_if(association_name, attributes)
- callback = self.class.reject_new_nested_attributes_procs[association_name]
+ callback = self.nested_attributes_options[association_name][:reject_if]
case callback
when Symbol
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index e57e361520..2a2a19fd4c 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -29,13 +29,13 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase
Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end
- def test_base_should_have_an_empty_reject_new_nested_attributes_procs
- assert_equal Hash.new, ActiveRecord::Base.reject_new_nested_attributes_procs
+ def test_base_should_have_an_empty_nested_attributes_options
+ assert_equal Hash.new, ActiveRecord::Base.nested_attributes_options
end
- def test_should_add_a_proc_to_reject_new_nested_attributes_procs
+ def test_should_add_a_proc_to_nested_attributes_options
[:parrots, :birds, :birds_with_reject_all_blank].each do |name|
- assert_instance_of Proc, Pirate.reject_new_nested_attributes_procs[name]
+ assert_instance_of Proc, Pirate.nested_attributes_options[name][:reject_if]
end
end