Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / test / models / post.rb
1 class Post < ActiveRecord::Base
2 named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'"
3 named_scope :ranked_by_comments, :order => "comments_count DESC"
4 named_scope :limit, lambda {|limit| {:limit => limit} }
5 named_scope :with_authors_at_address, lambda { |address| {
6 :conditions => [ 'authors.author_address_id = ?', address.id ],
7 :joins => 'JOIN authors ON authors.id = posts.author_id'
8 }
9 }
10
11 belongs_to :author do
12 def greeting
13 "hello"
14 end
15 end
16
17 belongs_to :author_with_posts, :class_name => "Author", :foreign_key => :author_id, :include => :posts
18 belongs_to :author_with_address, :class_name => "Author", :foreign_key => :author_id, :include => :author_address
19
20 has_one :last_comment, :class_name => 'Comment', :order => 'id desc'
21
22 named_scope :with_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'SpecialComment'} }
23 named_scope :with_very_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'VerySpecialComment'} }
24 named_scope :with_post, lambda {|post_id|
25 { :joins => :comments, :conditions => {:comments => {:post_id => post_id} } }
26 }
27
28 has_many :comments, :order => "body" do
29 def find_most_recent
30 find(:first, :order => "id DESC")
31 end
32 end
33
34 has_many :author_favorites, :through => :author
35
36 has_many :comments_with_interpolated_conditions, :class_name => 'Comment',
37 :conditions => ['#{"#{aliased_table_name}." rescue ""}body = ?', 'Thank you for the welcome']
38
39 has_one :very_special_comment
40 has_one :very_special_comment_with_post, :class_name => "VerySpecialComment", :include => :post
41 has_many :special_comments
42 has_many :nonexistant_comments, :class_name => 'Comment', :conditions => 'comments.id < 0'
43
44 has_and_belongs_to_many :categories
45 has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id'
46
47 has_many :taggings, :as => :taggable
48 has_many :tags, :through => :taggings do
49 def add_joins_and_select
50 find :all, :select => 'tags.*, authors.id as author_id', :include => false,
51 :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'
52 end
53 end
54
55 has_many :funky_tags, :through => :taggings, :source => :tag
56 has_many :super_tags, :through => :taggings
57 has_one :tagging, :as => :taggable
58
59 has_many :invalid_taggings, :as => :taggable, :class_name => "Tagging", :conditions => 'taggings.id < 0'
60 has_many :invalid_tags, :through => :invalid_taggings, :source => :tag
61
62 has_many :categorizations, :foreign_key => :category_id
63 has_many :authors, :through => :categorizations
64
65 has_many :readers
66 has_many :people, :through => :readers
67 has_many :people_with_callbacks, :source=>:person, :through => :readers,
68 :before_add => lambda {|owner, reader| log(:added, :before, reader.first_name) },
69 :after_add => lambda {|owner, reader| log(:added, :after, reader.first_name) },
70 :before_remove => lambda {|owner, reader| log(:removed, :before, reader.first_name) },
71 :after_remove => lambda {|owner, reader| log(:removed, :after, reader.first_name) }
72
73 def self.top(limit)
74 ranked_by_comments.limit(limit)
75 end
76
77 def self.reset_log
78 @log = []
79 end
80
81 def self.log(message=nil, side=nil, new_record=nil)
82 return @log if message.nil?
83 @log << [message, side, new_record]
84 end
85
86 def self.what_are_you
87 'a post...'
88 end
89 end
90
91 class SpecialPost < Post; end
92
93 class StiPost < Post
94 self.abstract_class = true
95 has_one :special_comment, :class_name => "SpecialComment"
96 end
97
98 class SubStiPost < StiPost
99 self.table_name = Post.table_name
100 end