From 7cba6a37848ba96b4decec885779fb309d71c339 Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Mon, 14 Nov 2011 22:57:15 -0800 Subject: association methods are now generated in modules Instead of generating association methods directly in the model class, they are generated in an anonymous module which is then included in the model class. There is one such module for each association. The only subtlety is that the generated_attributes_methods module (from ActiveModel) must be forced to be included before association methods are created so that attribute methods will not shadow association methods. --- .../lib/active_record/associations/builder/association.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record/associations/builder/association.rb') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 96fca97440..686db0725d 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 + attr_reader :model, :name, :options, :reflection, :mixin def self.build(model, name, options) new(model, name, options).build @@ -14,6 +14,8 @@ module ActiveRecord::Associations::Builder def initialize(model, name, options) @model, @name, @options = model, name, options + @mixin = Module.new + @model.__send__(:include, @mixin) end def build @@ -36,16 +38,14 @@ module ActiveRecord::Associations::Builder def define_readers name = self.name - - model.redefine_method(name) do |*params| + mixin.send(:define_method, name) do |*params| association(name).reader(*params) end end def define_writers name = self.name - - model.redefine_method("#{name}=") do |value| + mixin.send(:define_method, "#{name}=") do |value| association(name).writer(value) end end -- cgit v1.2.3