forked from ianwhite/nested_has_many_through
/
app.rb
82 lines (68 loc) · 2.81 KB
/
app.rb
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
# Testing app setup
#########
# Models
#
# Domain model is this:
#
# - authors (type of user) can create posts in categories
# - users can comment on posts
# - authors have similar_posts: posts in the same categories as ther posts
# - authors have similar_authors: authors of the recommended_posts
# - authors have posts_of_similar_authors: all posts by similar authors (not just the similar posts,
# similar_posts is be a subset of this collection)
# - authors have commenters: users who have commented on their posts
#
# Following model is added to verify the has_many/belongs_to :through relationship still works (as it is currently broken)
# - authors have many assistants: they can access the posts of their author
class User < ActiveRecord::Base
has_many :comments
# Possible cases added by this plugin
has_many :commented_posts, :through => :comments, :source => :post, :uniq => true
has_many :commented_authors, :through => :commented_posts, :source => :author, :uniq => true
has_many :posts_of_interest, :through => :commented_authors, :source => :posts_of_similar_authors, :uniq => true
has_many :categories_of_interest, :through => :posts_of_interest, :source => :category, :uniq => true
end
class Author < User
# Normal cases
has_many :posts
has_many :categories, :through => :posts
has_many :commenters, :through => :posts, :uniq => true
has_many :assistants
# Possible cases added by this plugin
has_many :similar_posts, :through => :categories, :source => :posts
has_many :similar_authors, :through => :similar_posts, :source => :author, :uniq => true
has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts, :uniq => true
end
class Post < ActiveRecord::Base
# testing with_scope
def self.find_inflamatory(*args)
with_scope :find => {:conditions => {:inflamatory => true}} do
find(*args)
end
end
# only test named_scope in edge
named_scope(:inflamatory, :conditions => {:inflamatory => true}) if respond_to?(:named_scope)
belongs_to :author
belongs_to :category
has_many :comments
has_many :commenters, :through => :comments, :source => :user, :uniq => true
end
class Category < ActiveRecord::Base
has_many :posts
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
class Assistant < ActiveRecord::Base
# Normal cases
belongs_to :author
has_many :posts, :through => :author
# Possible cases added by this plugin
with_options :uniq => true do |w|
w.has_many :categories, :through => :posts
w.has_many :similar_posts, :through => :categories, :source => :posts
w.has_many :similar_authors, :through => :similar_posts, :source => :author
w.has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts
end
end