Skip to content

Commit

Permalink
apply [6408] to stable
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6410 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Mar 13, 2007
1 parent 736cca8 commit 3054971
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 47 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*1.15.3* (March 12th, 2007)

* Allow a polymorphic :source for has_many :through associations. Closes #7143 [protocool]

* Consistently quote primary key column names. #7763 [toolmantim]

* Fixtures: fix YAML ordered map support. #2665 [Manuel Holtgrewe, nfbuckley]
Expand Down
105 changes: 61 additions & 44 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -20,7 +20,13 @@ def initialize(owner_class_name, reflection, source_reflection)
super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' on the polymorphic object '#{source_reflection.class_name}##{source_reflection.name}'.")
end
end


class HasManyThroughAssociationPointlessSourceTypeError < ActiveRecordError #:nodoc:
def initialize(owner_class_name, reflection, source_reflection)
super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' with a :source_type option if the '#{reflection.through_reflection.class_name}##{source_reflection.name}' is not polymorphic. Try removing :source_type on your association.")
end
end

class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError #:nodoc:
def initialize(reflection)
through_reflection = reflection.through_reflection
Expand Down Expand Up @@ -529,6 +535,8 @@ module ClassMethods
# * <tt>:source</tt>: Specifies the source association name used by <tt>has_many :through</tt> queries. Only use it if the name cannot be
# inferred from the association. <tt>has_many :subscribers, :through => :subscriptions</tt> will look for either +:subscribers+ or
# +:subscriber+ on +Subscription+, unless a +:source+ is given.
# * <tt>:source_type</tt>: Specifies type of the source association used by <tt>has_many :through</tt> queries where the source association
# is a polymorphic belongs_to.
# * <tt>:uniq</tt> - if set to true, duplicates will be omitted from the collection. Useful in conjunction with :through.
#
# Option examples:
Expand Down Expand Up @@ -1087,7 +1095,7 @@ def create_has_many_reflection(association_id, options, &extension)
:class_name, :table_name, :foreign_key,
:exclusively_dependent, :dependent,
:select, :conditions, :include, :order, :group, :limit, :offset,
:as, :through, :source,
:as, :through, :source, :source_type,
:uniq,
:finder_sql, :counter_sql,
:before_add, :after_add, :before_remove, :after_remove,
Expand Down Expand Up @@ -1491,57 +1499,65 @@ def association_join
case
when reflection.macro == :has_many && reflection.options[:through]
through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''

jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil
first_key = second_key = as_extra = nil

if through_reflection.options[:as] # has_many :through against a polymorphic join
polymorphic_foreign_key = through_reflection.options[:as].to_s + '_id'
polymorphic_foreign_type = through_reflection.options[:as].to_s + '_type'

" LEFT OUTER JOIN %s ON (%s.%s = %s.%s AND %s.%s = %s) " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
aliased_join_table_name, polymorphic_foreign_key,
parent.aliased_table_name, parent.primary_key,
aliased_join_table_name, polymorphic_foreign_type, klass.quote_value(parent.active_record.base_class.name)] +
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias,
aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key
jt_foreign_key = through_reflection.options[:as].to_s + '_id'
jt_as_extra = " AND %s.%s = %s" % [
aliased_join_table_name, reflection.active_record.connection.quote_column_name(through_reflection.options[:as].to_s + '_type'),
klass.quote_value(parent.active_record.base_class.name)
]
else
if source_reflection.macro == :has_many && source_reflection.options[:as]
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
through_reflection.primary_key_name,
parent.aliased_table_name, parent.primary_key] +
" LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [
table_name_and_alias,
aliased_table_name, "#{source_reflection.options[:as]}_id",
aliased_join_table_name, options[:foreign_key] || primary_key,
aliased_table_name, "#{source_reflection.options[:as]}_type",
jt_foreign_key = through_reflection.primary_key_name
end

case source_reflection.macro
when :has_many
if source_reflection.options[:as]
first_key = "#{source_reflection.options[:as]}_id"
second_key = options[:foreign_key] || primary_key
as_extra = " AND %s.%s = %s" % [
aliased_table_name, reflection.active_record.connection.quote_column_name("#{source_reflection.options[:as]}_type"),
klass.quote_value(source_reflection.active_record.base_class.name)
]
else
case source_reflection.macro
when :belongs_to
first_key = primary_key
second_key = source_reflection.options[:foreign_key] || klass.to_s.classify.foreign_key
extra = nil
when :has_many
first_key = through_reflection.klass.base_class.to_s.classify.foreign_key
second_key = options[:foreign_key] || primary_key
extra = through_reflection.klass.descends_from_active_record? ? nil :
" AND %s.%s = %s" % [
aliased_join_table_name,
reflection.active_record.connection.quote_column_name(through_reflection.active_record.inheritance_column),
through_reflection.klass.quote_value(through_reflection.klass.name.demodulize)]
end
" LEFT OUTER JOIN %s ON (%s.%s = %s.%s%s) " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
aliased_join_table_name, through_reflection.primary_key_name,
parent.aliased_table_name, parent.primary_key, extra] +
" LEFT OUTER JOIN %s ON (%s.%s = %s.%s) " % [
table_name_and_alias,
aliased_table_name, first_key,
aliased_join_table_name, second_key
first_key = through_reflection.klass.base_class.to_s.classify.foreign_key
second_key = options[:foreign_key] || primary_key
end

unless through_reflection.klass.descends_from_active_record?
jt_sti_extra = " AND %s.%s = %s" % [
aliased_join_table_name,
reflection.active_record.connection.quote_column_name(through_reflection.active_record.inheritance_column),
through_reflection.klass.quote_value(through_reflection.klass.name.demodulize)]
end
when :belongs_to
first_key = primary_key
if reflection.options[:source_type]
second_key = source_reflection.association_foreign_key
jt_source_extra = " AND %s.%s = %s" % [
aliased_join_table_name, reflection.active_record.connection.quote_column_name(reflection.source_reflection.options[:foreign_type]),
klass.quote_value(reflection.options[:source_type])
]
else
second_key = source_reflection.options[:foreign_key] || klass.to_s.classify.foreign_key
end
end

" LEFT OUTER JOIN %s ON (%s.%s = %s.%s%s%s%s) " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
parent.aliased_table_name, reflection.active_record.connection.quote_column_name(parent.primary_key),
aliased_join_table_name, reflection.active_record.connection.quote_column_name(jt_foreign_key),
jt_as_extra, jt_source_extra, jt_sti_extra
] +
" LEFT OUTER JOIN %s ON (%s.%s = %s.%s%s) " % [
table_name_and_alias,
aliased_table_name, reflection.active_record.connection.quote_column_name(first_key),
aliased_join_table_name, reflection.active_record.connection.quote_column_name(second_key),
as_extra
]

when reflection.macro == :has_many && reflection.options[:as]
" LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s" % [
Expand Down Expand Up @@ -1588,6 +1604,7 @@ def association_join
end

protected

def pluralize(table_name)
ActiveRecord::Base.pluralize_table_names ? table_name.to_s.pluralize : table_name
end
Expand Down
Expand Up @@ -138,7 +138,11 @@ def construct_owner_attributes(reflection)

# Construct attributes for :through pointing to owner and associate.
def construct_join_attributes(associate)
construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id)
returning construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id) do |join_attributes|
if @reflection.options[:source_type]
join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s)
end
end
end

# Associate attributes pointing to owner, quoted.
Expand Down Expand Up @@ -176,6 +180,12 @@ def construct_joins(custom_joins = nil)
if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.klass.primary_key
source_primary_key = @reflection.source_reflection.primary_key_name
if @reflection.options[:source_type]
polymorphic_join = "AND %s.%s = %s" % [
@reflection.through_reflection.table_name, "#{@reflection.source_reflection.options[:foreign_type]}",
@owner.class.quote_value(@reflection.options[:source_type])
]
end
else
reflection_primary_key = @reflection.source_reflection.primary_key_name
source_primary_key = @reflection.klass.primary_key
Expand Down
8 changes: 6 additions & 2 deletions activerecord/lib/active_record/reflection.rb
Expand Up @@ -186,8 +186,12 @@ def check_validity!
if source_reflection.nil?
raise HasManyThroughSourceAssociationNotFoundError.new(self)
end

if options[:source_type] && source_reflection.options[:polymorphic].nil?
raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection)
end

if source_reflection.options[:polymorphic]
if source_reflection.options[:polymorphic] && options[:source_type].nil?
raise HasManyThroughAssociationPolymorphicError.new(active_record.name, self, source_reflection)
end

Expand All @@ -205,7 +209,7 @@ def name_to_class_name(name)
if options[:class_name]
options[:class_name]
elsif through_reflection # get the class_name of the belongs_to association of the through reflection
source_reflection.class_name
options[:source_type] || source_reflection.class_name
else
class_name = name.to_s.camelize
class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro)
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/associations/join_model_test.rb
Expand Up @@ -300,6 +300,18 @@ def test_has_many_polymorphic
assert_equal [posts(:welcome), posts(:thinking)], tags(:general).taggings.find(:all, :include => :taggable)
end
end

def test_has_many_polymorphic_with_source_type
assert_equal [posts(:welcome), posts(:thinking)], tags(:general).tagged_posts
end

def test_eager_has_many_polymorphic_with_source_type
tag_with_include = Tag.find(tags(:general).id, :include => :tagged_posts)
desired = [posts(:welcome), posts(:thinking)]
assert_no_queries do
assert_equal desired, tag_with_include.tagged_posts
end
end

def test_has_many_through_has_many_find_all
assert_equal comments(:greetings), authors(:david).comments.find(:all, :order => 'comments.id').first
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/tag.rb
Expand Up @@ -2,4 +2,6 @@ class Tag < ActiveRecord::Base
has_many :taggings
has_many :taggables, :through => :taggings
has_one :tagging

has_many :tagged_posts, :through => :taggings, :source => :taggable, :source_type => 'Post'
end

0 comments on commit 3054971

Please sign in to comment.