public
Description: rails plugin that allows has_many :through to go through other has_many :throughs
Homepage:
Clone URL: git://github.com/ianwhite/nested_has_many_through.git
nested_has_many_through / spec / models / commenter_spec.rb
100644 109 lines (87 sloc) 3.009 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
require File.expand_path(File.join(File.dirname(__FILE__), '../app'))
 
describe 'Commenter use case (a1: p1>c1, a2: p2>c1, p3>c2, a3: p4>c3)' do
  before do
    @c1 = Category.create!
    @c2 = Category.create!
    @c3 = Category.create!
    @a1 = Author.create!
    @a2 = Author.create!
    @a3 = Author.create!
    @p1 = @a1.posts.create! :category => @c1
    @p2 = @a2.posts.create! :category => @c1
    @p3 = @a2.posts.create! :category => @c2
    @p4 = @a3.posts.create! :category => @c3
    @a1.reload
    @a2.reload
  end
 
  it "a1.posts should == [p1]" do
    @a1.posts.should == [@p1]
  end
 
  it "a1.categories should == [c1]" do
    @a1.categories.should == [@c1]
  end
  
  it "a2.posts should == [p2, p3]" do
    @a2.posts.should == [@p2, @p3]
  end
 
  it "a2.categories should == [c1, c2]" do
    @a2.categories.should == [@c1, @c2]
  end
  
  describe "u1 comments on p2" do
    before do
      @u1 = User.create!
      @comment = @p2.comments.create! :user => @u1
    end
    
    it "u1.comments should == [comment]" do
      @u1.comments.should == [@comment]
    end
    
    it "a1.commenters should be empty" do
      @a1.commenters.should be_empty
    end
    
    it "a2.commenters should == [u1]" do
      @a2.commenters.should == [@u1]
    end
    
    it "u1.commented_posts should == [p2]" do
      @u1.commented_posts.should == [@p2]
    end
    
    it "u1.commented_posts.find_inflamatory(:all) should be empty" do
      @u1.commented_posts.find_inflamatory(:all).should be_empty
    end
    
    if ActiveRecord::Base.respond_to?(:named_scope)
      it "u1.commented_posts.inflamatory should be empty" do
        @u1.commented_posts.inflamatory.should be_empty
      end
    end
    
    it "u1.commented_authors should == [a2]" do
      @u1.commented_authors.should == [@a2]
    end
    
    it "u1.posts_of_interest should == [p1, p2, p3]" do
      @u1.posts_of_interest.should == [@p1, @p2, @p3]
    end
    
    it "u1.categories_of_interest should == [c1, c2]" do
      @u1.categories_of_interest.should == [@c1, @c2]
    end
    
    describe "when p2 is inflamatory" do
      before do
        @p2.toggle!(:inflamatory)
      end
      
      it "p2 should be inflamatory" do
        @p2.should be_inflamatory
      end
      
      it "u1.commented_posts.find_inflamatory(:all) should == [p2]" do
        @u1.commented_posts.find_inflamatory(:all).should == [@p2]
      end
        
      it "u1.posts_of_interest.find_inflamatory(:all) should == [p2]" do
        @u1.posts_of_interest.find_inflamatory(:all).should == [@p2]
      end
      
      if ActiveRecord::Base.respond_to?(:named_scope)
        it "u1.commented_posts.inflamatory should == [p2]" do
          @u1.commented_posts.inflamatory.should == [@p2]
        end
 
        it "u1.posts_of_interest.inflamatory should == [p2]" do
          @u1.posts_of_interest.inflamatory.should == [@p2]
        end
      end
    end
  end
end