From 61bcc318c865289d215e8e19618b9414bd07d1e8 Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Sun, 27 Nov 2011 11:22:12 -0800 Subject: use GeneratedFeatureMethods module for associations --- .../lib/active_record/associations/builder/association.rb | 8 +++++--- .../associations/builder/has_and_belongs_to_many.rb | 12 ++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 686db0725d..3534e037b7 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -6,7 +6,7 @@ module ActiveRecord::Associations::Builder # Set by subclasses class_attribute :macro - attr_reader :model, :name, :options, :reflection, :mixin + attr_reader :model, :name, :options, :reflection def self.build(model, name, options) new(model, name, options).build @@ -14,8 +14,10 @@ module ActiveRecord::Associations::Builder def initialize(model, name, options) @model, @name, @options = model, name, options - @mixin = Module.new - @model.__send__(:include, @mixin) + end + + def mixin + @model.generated_feature_methods end def build diff --git a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb index f3391eba13..30fc44b4c2 100644 --- a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb @@ -15,10 +15,14 @@ module ActiveRecord::Associations::Builder def define_destroy_hook name = self.name - mixin.send(:define_method, :destroy_associations) do - association(name).delete_all - super() - end + model.send(:include, Module.new { + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def destroy_associations + association(#{name.to_sym.inspect}).delete_all + super + end + RUBY + }) end # TODO: These checks should probably be moved into the Reflection, and we should not be -- cgit v1.2.3