From 9da52a5e55cc665a539afb45783f84d9f3607282 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 1 Aug 2013 10:16:34 -0700 Subject: push more mutations out of the builder `configure_dependency` actually defined callbacks, so rename the method and move it to the appropriate method. --- .../active_record/associations/builder/association.rb | 17 +++++++++-------- .../lib/active_record/associations/builder/has_one.rb | 10 ++++++---- 2 files changed, 15 insertions(+), 12 deletions(-) (limited to 'activerecord/lib/active_record/associations/builder') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index b20347a52a..ef397fdb52 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -53,7 +53,6 @@ module ActiveRecord::Associations::Builder end def build - configure_dependency if options[:dependent] ActiveRecord::Reflection.create(macro, name, scope, options, model) end @@ -70,6 +69,7 @@ module ActiveRecord::Associations::Builder end def define_callbacks(model, reflection) + add_before_destroy_callbacks(model, name) if options[:dependent] Association.extensions.each do |extension| extension.build model, reflection end @@ -103,17 +103,18 @@ module ActiveRecord::Associations::Builder CODE end - def configure_dependency + def valid_dependent_options + raise NotImplementedError + end + + private + + def add_before_destroy_callbacks(model, name) unless valid_dependent_options.include? options[:dependent] raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{options[:dependent]}" end - n = name - model.before_destroy lambda { |o| o.association(n).handle_dependency } - end - - def valid_dependent_options - raise NotImplementedError + model.before_destroy lambda { |o| o.association(name).handle_dependency } end end end diff --git a/activerecord/lib/active_record/associations/builder/has_one.rb b/activerecord/lib/active_record/associations/builder/has_one.rb index 2406437a07..62d454ce55 100644 --- a/activerecord/lib/active_record/associations/builder/has_one.rb +++ b/activerecord/lib/active_record/associations/builder/has_one.rb @@ -14,12 +14,14 @@ module ActiveRecord::Associations::Builder !options[:through] end - def configure_dependency - super unless options[:through] - end - def valid_dependent_options [:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception] end + + private + + def add_before_destroy_callbacks(model, name) + super unless options[:through] + end end end -- cgit v1.2.3