Updated README.rdoc again
[feedcatcher.git] / vendor / rails / activerecord / test / models / author.rb
1 class Author < ActiveRecord::Base
2 has_many :posts
3 has_many :posts_with_comments, :include => :comments, :class_name => "Post"
4 has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type"
5 has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id'
6 has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1
7 has_many :posts_with_categories, :include => :categories, :class_name => "Post"
8 has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
9 has_many :posts_containing_the_letter_a, :class_name => "Post"
10 has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension
11 def testing_proxy_owner
12 proxy_owner
13 end
14 def testing_proxy_reflection
15 proxy_reflection
16 end
17 def testing_proxy_target
18 proxy_target
19 end
20 end
21 has_one :post_about_thinking, :class_name => 'Post', :conditions => "posts.title like '%thinking%'"
22 has_one :post_about_thinking_with_last_comment, :class_name => 'Post', :conditions => "posts.title like '%thinking%'", :include => :last_comment
23 has_many :comments, :through => :posts
24 has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
25 has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'"
26 has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post
27
28
29 has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
30 has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
31 has_many :funky_comments, :through => :posts, :source => :comments
32 has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id'
33 has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC'
34 has_many :readonly_comments, :through => :posts, :source => :comments, :readonly => true
35
36 has_many :special_posts
37 has_many :special_post_comments, :through => :special_posts, :source => :comments
38
39 has_many :sti_posts, :class_name => 'StiPost'
40 has_many :sti_post_comments, :through => :sti_posts, :source => :comments
41
42 has_many :special_nonexistant_posts, :class_name => "SpecialPost", :conditions => "posts.body = 'nonexistant'"
43 has_many :special_nonexistant_post_comments, :through => :special_nonexistant_posts, :source => :comments, :conditions => "comments.post_id = 0"
44 has_many :nonexistant_comments, :through => :posts
45
46 has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'"
47 has_many :hello_post_comments, :through => :hello_posts, :source => :comments
48 has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments
49
50 has_many :hello_posts_with_hash_conditions, :class_name => "Post",
51 :conditions => {:body => 'hello'}
52 has_many :hello_post_comments_with_hash_conditions, :through =>
53 :hello_posts_with_hash_conditions, :source => :comments
54
55 has_many :other_posts, :class_name => "Post"
56 has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
57 :after_add => :log_after_adding,
58 :before_remove => :log_before_removing,
59 :after_remove => :log_after_removing
60 has_many :posts_with_proc_callbacks, :class_name => "Post",
61 :before_add => Proc.new {|o, r| o.post_log << "before_adding#{r.id || '<new>'}"},
62 :after_add => Proc.new {|o, r| o.post_log << "after_adding#{r.id || '<new>'}"},
63 :before_remove => Proc.new {|o, r| o.post_log << "before_removing#{r.id}"},
64 :after_remove => Proc.new {|o, r| o.post_log << "after_removing#{r.id}"}
65 has_many :posts_with_multiple_callbacks, :class_name => "Post",
66 :before_add => [:log_before_adding, Proc.new {|o, r| o.post_log << "before_adding_proc#{r.id || '<new>'}"}],
67 :after_add => [:log_after_adding, Proc.new {|o, r| o.post_log << "after_adding_proc#{r.id || '<new>'}"}]
68 has_many :unchangable_posts, :class_name => "Post", :before_add => :raise_exception, :after_add => :log_after_adding
69
70 has_many :categorizations
71 has_many :categories, :through => :categorizations
72
73 has_many :categories_like_general, :through => :categorizations, :source => :category, :class_name => 'Category', :conditions => { :name => 'General' }
74
75 has_many :categorized_posts, :through => :categorizations, :source => :post
76 has_many :unique_categorized_posts, :through => :categorizations, :source => :post, :uniq => true
77
78 has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
79
80 has_many :author_favorites
81 has_many :favorite_authors, :through => :author_favorites, :order => 'name'
82
83 has_many :tagging, :through => :posts # through polymorphic has_one
84 has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many
85 has_many :tags, :through => :posts # through has_many :through
86 has_many :post_categories, :through => :posts, :source => :categories
87
88 belongs_to :author_address, :dependent => :destroy
89 belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
90
91 attr_accessor :post_log
92
93 def after_initialize
94 @post_log = []
95 end
96
97 def label
98 "#{id}-#{name}"
99 end
100
101 private
102 def log_before_adding(object)
103 @post_log << "before_adding#{object.id || '<new>'}"
104 end
105
106 def log_after_adding(object)
107 @post_log << "after_adding#{object.id}"
108 end
109
110 def log_before_removing(object)
111 @post_log << "before_removing#{object.id}"
112 end
113
114 def log_after_removing(object)
115 @post_log << "after_removing#{object.id}"
116 end
117
118 def raise_exception(object)
119 raise Exception.new("You can't add a post")
120 end
121 end
122
123 class AuthorAddress < ActiveRecord::Base
124 has_one :author
125
126 def self.destroyed_author_address_ids
127 @destroyed_author_address_ids ||= Hash.new { |h,k| h[k] = [] }
128 end
129
130 before_destroy do |author_address|
131 if author_address.author
132 AuthorAddress.destroyed_author_address_ids[author_address.author.id] << author_address.id
133 end
134 true
135 end
136 end
137
138 class AuthorFavorite < ActiveRecord::Base
139 belongs_to :author
140 belongs_to :favorite_author, :class_name => "Author"
141 end