From 7f5fcc0785e28175b1a54971e5adc34ecd50787d Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 17 Dec 2010 09:56:47 +0000 Subject: Refactor create_record and build_record in AssociationCollection --- .../associations/association_collection.rb | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 3f80133299..18d05aa133 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -487,32 +487,20 @@ module ActiveRecord force ? record.save! : record.save(:validate => validate) end - def create_record(attrs) + def create_record(attrs, &block) ensure_owner_is_persisted! - attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) - create_scope = @scope[:create].merge(scoped.where_values_hash || {}) - transaction do - record = with_scope(:create => create_scope) do - @reflection.build_association(attrs) - end - if block_given? - add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } - else - add_record_to_target_with_callbacks(record) + with_scope(:create => @scope[:create].merge(scoped.where_values_hash || {})) do + build_record(attrs, &block) end end end - def build_record(attrs) + def build_record(attrs, &block) attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) record = @reflection.build_association(attrs) - if block_given? - add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } - else - add_record_to_target_with_callbacks(record) - end + add_record_to_target_with_callbacks(record, &block) end def remove_records(*records) -- cgit v1.2.3