From 57b7532b910f9258cad4111db79349d2d63be6d4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 2 Dec 2005 06:03:43 +0000 Subject: Work-in progress for providing better join model support and polymorphic associations git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3209 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations/association_proxy.rb | 1 - .../associations/belongs_to_association.rb | 4 -- .../belongs_to_polymorphic_association.rb | 70 ++++++++++++++++++++++ .../associations/has_many_association.rb | 21 ++++--- 4 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 17b8cc6446..8a7d925d0d 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -76,7 +76,6 @@ module ActiveRecord end private - def method_missing(method, *args, &block) load_target @target.send(method, *args, &block) diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 528c42cea7..39d128aef1 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -1,7 +1,6 @@ module ActiveRecord module Associations class BelongsToAssociation < AssociationProxy #:nodoc: - def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options) super construct_sql @@ -43,9 +42,6 @@ module ActiveRecord @updated end - protected - - private def find_target if @options[:conditions] diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb new file mode 100644 index 0000000000..e2a7f1a58e --- /dev/null +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -0,0 +1,70 @@ +module ActiveRecord + module Associations + class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc: + def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options) + @owner = owner + @options = options + @association_name = association_name + @association_class_primary_key_name = association_class_primary_key_name + + proxy_extend(options[:extend]) if options[:extend] + + reset + end + + def create(attributes = {}) + raise ActiveRecord::ActiveRecordError, "Can't create an abstract polymorphic object" + end + + def build(attributes = {}) + raise ActiveRecord::ActiveRecordError, "Can't build an abstract polymorphic object" + end + + def replace(obj, dont_save = false) + if obj.nil? + @target = @owner[@association_class_primary_key_name] = @owner[@options[:foreign_type]] = nil + else + @target = (AssociationProxy === obj ? obj.target : obj) + + unless obj.new_record? + @owner[@association_class_primary_key_name] = obj.id + @owner[@options[:foreign_type]] = ActiveRecord::Base.send(:class_name_of_active_record_descendant, obj.class).to_s + end + + @updated = true + end + + @loaded = true + + return (@target.nil? ? nil : self) + end + + private + def find_target + return nil if association_class.nil? + + if @options[:conditions] + association_class.find( + @owner[@association_class_primary_key_name], + :conditions => interpolate_sql(@options[:conditions]), + :include => @options[:include] + ) + else + association_class.find(@owner[@association_class_primary_key_name], :include => @options[:include]) + end + end + + def foreign_key_present + !@owner[@association_class_primary_key_name].nil? + end + + def target_obsolete? + @owner[@association_class_primary_key_name] != @target.id + end + + def association_class + @owner[@options[:foreign_type]] ? @owner[@options[:foreign_type]].constantize : nil + end + end + end +end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 288742d965..465e1f8c72 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -163,11 +163,19 @@ module ActiveRecord end def construct_sql - if @options[:finder_sql] - @finder_sql = interpolate_sql(@options[:finder_sql]) - else - @finder_sql = "#{@association_class.table_name}.#{@association_class_primary_key_name} = #{@owner.quoted_id}" - @finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions + case + when @options[:as] + @finder_sql = + "#{@association_class.table_name}.#{@options[:as]}_id = #{@owner.quoted_id} AND " + + "#{@association_class.table_name}.#{@options[:as]}_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s}'" + @finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions + + when @options[:finder_sql] + @finder_sql = interpolate_sql(@options[:finder_sql]) + + else + @finder_sql = "#{@association_class.table_name}.#{@association_class_primary_key_name} = #{@owner.quoted_id}" + @finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions end if @options[:counter_sql] @@ -176,8 +184,7 @@ module ActiveRecord @options[:counter_sql] = @options[:finder_sql].gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM") @counter_sql = interpolate_sql(@options[:counter_sql]) else - @counter_sql = "#{@association_class.table_name}.#{@association_class_primary_key_name} = #{@owner.quoted_id}" - @counter_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions + @counter_sql = @finder_sql end end end -- cgit v1.2.3