From 379c02267b3cbbf6d1ac48bc79ec6ea01af7b53a Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 16 Dec 2010 22:06:19 +0000 Subject: Specify insert_record with NotImplementedError in AssociationCollection, to indicate that subclasses should implement it. Also add save_record to reduce duplication. --- .../lib/active_record/associations/association_collection.rb | 11 +++++++++++ .../associations/has_and_belongs_to_many_association.rb | 6 +----- .../lib/active_record/associations/has_many_association.rb | 2 +- .../associations/has_many_through_association.rb | 6 +----- 4 files changed, 14 insertions(+), 11 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 11a7a725e5..51d8952eca 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -476,6 +476,17 @@ module ActiveRecord end private + # Do the relevant stuff to insert the given record into the association collection. The + # force param specifies whether or not an exception should be raised on failure. The + # validate param specifies whether validation should be performed (if force is false). + def insert_record(record, force = true, validate = true) + raise NotImplementedError + end + + def save_record(record, force, validate) + force ? record.save! : record.save(:validate => validate) + end + def create_record(attrs) attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) ensure_owner_is_persisted! diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index e2ce9aefcf..259bf1f591 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -35,11 +35,7 @@ module ActiveRecord def insert_record(record, force = true, validate = true) if record.new_record? - if force - record.save! - else - return false unless record.save(:validate => validate) - end + return false unless save_record(record, force, validate) end if @reflection.options[:insert_sql] diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 4ff61fff45..cfbb49b6a3 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -55,7 +55,7 @@ module ActiveRecord def insert_record(record, force = false, validate = true) set_belongs_to_association_for(record) - force ? record.save! : record.save(:validate => validate) + save_record(record, force, validate) end # Deletes the records according to the :dependent option. diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 781aa7ef62..179a27725b 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -60,11 +60,7 @@ module ActiveRecord def insert_record(record, force = true, validate = true) if record.new_record? - if force - record.save! - else - return false unless record.save(:validate => validate) - end + return false unless save_record(record, force, validate) end through_association = @owner.send(@reflection.through_reflection.name) -- cgit v1.2.3