aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJosh Susser <josh@hasmanythrough.com>2011-11-27 11:22:12 -0800
committerJosh Susser <josh@hasmanythrough.com>2011-11-27 11:22:12 -0800
commit61bcc318c865289d215e8e19618b9414bd07d1e8 (patch)
tree1bc644f4152dc4315d8490b099c24abc2815088c /activerecord/lib/active_record
parent9cdf33af0bc46fde1ad50346b8271251c2b4aa69 (diff)
downloadrails-61bcc318c865289d215e8e19618b9414bd07d1e8.tar.gz
rails-61bcc318c865289d215e8e19618b9414bd07d1e8.tar.bz2
rails-61bcc318c865289d215e8e19618b9414bd07d1e8.zip
use GeneratedFeatureMethods module for associations
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/builder/association.rb8
-rw-r--r--activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb12
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb6
-rw-r--r--activerecord/lib/active_record/base.rb14
4 files changed, 27 insertions, 13 deletions
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
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 2d720fe700..d7bfaa5655 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -8,12 +8,6 @@ module ActiveRecord
include ActiveModel::AttributeMethods
module ClassMethods
- def inherited(child_class)
- # force creation + include before accessor method modules
- child_class.generated_attribute_methods
- super
- end
-
# Generates all the attribute related methods for columns in the database
# accessors, mutators and query methods.
def define_attribute_methods
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 3558ae3545..a9c8ed1396 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -450,6 +450,20 @@ module ActiveRecord #:nodoc:
:having, :create_with, :uniq, :to => :scoped
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :to => :scoped
+ def inherited(child_class) #:nodoc:
+ # force attribute methods to be higher in inheritance hierarchy than other generated methods
+ child_class.generated_attribute_methods
+ child_class.generated_feature_methods
+ super
+ end
+
+ def generated_feature_methods
+ unless const_defined?(:GeneratedFeatureMethods, false)
+ include const_set(:GeneratedFeatureMethods, Module.new)
+ end
+ const_get(:GeneratedFeatureMethods)
+ end
+
# Executes a custom SQL query against your database and returns all the results. The results will
# be returned as an array with columns requested encapsulated as attributes of the model you call
# this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in