From dd72cf289653a216921faa2d158bc3b712d39920 Mon Sep 17 00:00:00 2001 From: Bob Showalter Date: Fri, 11 Jul 2008 11:04:48 -0400 Subject: [PATCH] Add support for :source option on has_many :through assocations --- lib/shoulda/active_record_helpers.rb | 2 +- test/rails_root/app/models/post.rb | 1 + test/unit/post_test.rb | 1 + 3 files changed, 3 insertions(+), 1 deletions(-) diff --git a/lib/shoulda/active_record_helpers.rb b/lib/shoulda/active_record_helpers.rb index 381a33f..1503dfc 100644 --- a/lib/shoulda/active_record_helpers.rb +++ b/lib/shoulda/active_record_helpers.rb @@ -444,7 +444,7 @@ module ThoughtBot # :nodoc: assert reflection, "#{klass.name} does not have any relationship to #{association}" assert_equal :has_many, reflection.macro - associated_klass = (reflection.options[:class_name] || association.to_s.classify).constantize + associated_klass = (reflection.options[:source] || reflection.options[:class_name] || association).to_s.classify.constantize if through through_reflection = klass.reflect_on_association(through) diff --git a/test/rails_root/app/models/post.rb b/test/rails_root/app/models/post.rb index 5b840b9..76c094e 100644 --- a/test/rails_root/app/models/post.rb +++ b/test/rails_root/app/models/post.rb @@ -3,6 +3,7 @@ class Post < ActiveRecord::Base belongs_to :owner, :foreign_key => :user_id, :class_name => 'User' has_many :taggings has_many :tags, :through => :taggings + has_many :through_tags, :through => :taggings, :source => :tags validates_uniqueness_of :title validates_presence_of :title diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 9905147..bee2507 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -6,6 +6,7 @@ class PostTest < Test::Unit::TestCase should_belong_to :user should_belong_to :owner should_have_many :tags, :through => :taggings + should_have_many :through_tags, :through => :taggings should_require_unique_attributes :title should_require_attributes :body, :message => /wtf/ -- 1.5.6