Skip to content

Commit

Permalink
Add test_has_one_through_has_one_through_with_belongs_to_source_refle…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
jonleighton committed Oct 14, 2010
1 parent 25acd19 commit 002985f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 4 deletions.
Expand Up @@ -26,6 +26,7 @@
require 'models/organization'
require 'models/category'
require 'models/categorization'
require 'models/membership'

# NOTE: Some of these tests might not really test "nested" HMT associations, as opposed to ones which
# are just one level deep. But it's all the same thing really, as the "nested" code is being
Expand All @@ -36,7 +37,7 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :authors, :books, :posts, :subscriptions, :subscribers, :tags, :taggings,
:people, :readers, :references, :jobs, :ratings, :comments, :members, :member_details,
:member_types, :sponsors, :clubs, :organizations, :categories, :categories_posts,
:categorizations
:categorizations, :memberships

# Through associations can either use the has_many or has_one macros.
#
Expand Down Expand Up @@ -241,9 +242,19 @@ def test_has_one_through_has_one_with_has_one_through_source_reflection
assert_equal member_types(:founding), members.first.nested_member_type
end

# TODO: has_one through
# has_one through
# Source: belongs_to
# Through: has_one through
def test_has_one_through_has_one_through_with_belongs_to_source_reflection
assert_equal categories(:general), members(:groucho).club_category

members = Member.joins(:club_category).where('categories.id' => categories(:technology).id)
assert_equal [members(:blarpy_winkup)], members

# TODO: Make this work
# members = Member.includes(:club_category)
# assert_equal categories(:general), members.first.club_category
end

def test_distinct_has_many_through_a_has_many_through_association_on_source_reflection
author = authors(:david)
Expand Down
4 changes: 3 additions & 1 deletion activerecord/test/fixtures/clubs.yml
@@ -1,6 +1,8 @@
boring_club:
name: Banana appreciation society
category_id: 1
moustache_club:
name: Moustache and Eyebrow Fancier Club
crazy_club:
name: Skull and bones
name: Skull and bones
category_id: 2
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/members.yml
Expand Up @@ -6,3 +6,6 @@ some_other_guy:
id: 2
name: Englebert Humperdink
member_type_id: 2
blarpy_winkup:
id: 3
name: Blarpy Winkup
7 changes: 7 additions & 0 deletions activerecord/test/fixtures/memberships.yml
Expand Up @@ -18,3 +18,10 @@ other_guys_membership:
member_id: 2
favourite: false
type: CurrentMembership

blarpy_winkup_crazy_club:
joined_on: <%= 4.weeks.ago.to_s(:db) %>
club: crazy_club
member_id: 3
favourite: false
type: CurrentMembership
3 changes: 2 additions & 1 deletion activerecord/test/models/club.rb
Expand Up @@ -4,10 +4,11 @@ class Club < ActiveRecord::Base
has_many :current_memberships
has_one :sponsor
has_one :sponsored_member, :through => :sponsor, :source => :sponsorable, :source_type => "Member"
belongs_to :category

private

def private_method
"I'm sorry sir, this is a *private* club, not a *pirate* club"
end
end
end
2 changes: 2 additions & 0 deletions activerecord/test/models/member.rb
Expand Up @@ -18,4 +18,6 @@ class Member < ActiveRecord::Base

has_many :organization_member_details, :through => :member_detail
has_many :organization_member_details_2, :through => :organization, :source => :member_details

has_one :club_category, :through => :club, :source => :category
end
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -113,6 +113,7 @@ def create_table(*args, &block)

create_table :clubs, :force => true do |t|
t.string :name
t.integer :category_id
end

create_table :collections, :force => true do |t|
Expand Down

0 comments on commit 002985f

Please sign in to comment.