author.rb 9.4 KB
Newer Older
1
class Author < ActiveRecord::Base
2
  has_many :posts
3
  has_many :very_special_comments, :through => :posts
4
  has_many :posts_with_comments, :include => :comments, :class_name => "Post"
5
  has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type"
6
  has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id'
7
  has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1
8
  has_many :posts_with_categories, :include => :categories, :class_name => "Post"
9
  has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
10
  has_many :posts_containing_the_letter_a, :class_name => "Post"
11 12 13 14 15 16 17 18 19 20 21
  has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension
    def testing_proxy_owner
      proxy_owner
    end
    def testing_proxy_reflection
      proxy_reflection
    end
    def testing_proxy_target
      proxy_target
    end
  end
22 23
  has_one  :post_about_thinking, :class_name => 'Post', :conditions => "posts.title like '%thinking%'"
  has_one  :post_about_thinking_with_last_comment, :class_name => 'Post', :conditions => "posts.title like '%thinking%'", :include => :last_comment
24
  has_many :comments, :through => :posts
25
  has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
26 27 28
  has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'"
  has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post

29 30
  has_many :first_posts
  has_many :comments_on_first_posts, :through => :first_posts, :source => :comments, :order => 'posts.id desc, comments.id asc'
31 32 33

  has_one :first_post
  has_one :comment_on_first_post,  :through => :first_post, :source => :comments, :order => 'posts.id desc, comments.id asc'
34

35
  has_many :thinking_posts, :class_name => 'Post', :conditions => { :title => 'So I was thinking' }, :dependent => :delete_all
36
  has_many :welcome_posts,  :class_name => 'Post', :conditions => { :title => 'Welcome to the weblog' }
37

38
  has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
39
  has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
40
  has_many :funky_comments, :through => :posts, :source => :comments
41 42
  has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id'
  has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC'
43
  has_many :readonly_comments, :through => :posts, :source => :comments, :readonly => true
44

45 46
  has_many :special_posts
  has_many :special_post_comments, :through => :special_posts, :source => :comments
J
Jeremy Kemper 已提交
47

48 49 50
  has_many :sti_posts, :class_name => 'StiPost'
  has_many :sti_post_comments, :through => :sti_posts, :source => :comments

51 52
  has_many :special_nonexistant_posts, :class_name => "SpecialPost", :conditions => "posts.body = 'nonexistant'"
  has_many :special_nonexistant_post_comments, :through => :special_nonexistant_posts, :source => :comments, :conditions => "comments.post_id = 0"
53
  has_many :nonexistant_comments, :through => :posts
54 55 56

  has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'"
  has_many :hello_post_comments, :through => :hello_posts, :source => :comments
57
  has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments
58

59 60 61 62 63
  has_many :hello_posts_with_hash_conditions, :class_name => "Post",
:conditions => {:body => 'hello'}
  has_many :hello_post_comments_with_hash_conditions, :through =>
:hello_posts_with_hash_conditions, :source => :comments

64
  has_many :other_posts,          :class_name => "Post"
65
  has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
J
Jeremy Kemper 已提交
66
           :after_add     => :log_after_adding,
67 68
           :before_remove => :log_before_removing,
           :after_remove  => :log_after_removing
69
  has_many :posts_with_proc_callbacks, :class_name => "Post",
70 71
           :before_add    => Proc.new {|o, r| o.post_log << "before_adding#{r.id || '<new>'}"},
           :after_add     => Proc.new {|o, r| o.post_log << "after_adding#{r.id || '<new>'}"},
72
           :before_remove => Proc.new {|o, r| o.post_log << "before_removing#{r.id}"},
73
           :after_remove  => Proc.new {|o, r| o.post_log << "after_removing#{r.id}"}
74
  has_many :posts_with_multiple_callbacks, :class_name => "Post",
75 76
           :before_add => [:log_before_adding, Proc.new {|o, r| o.post_log << "before_adding_proc#{r.id || '<new>'}"}],
           :after_add  => [:log_after_adding,  Proc.new {|o, r| o.post_log << "after_adding_proc#{r.id || '<new>'}"}]
77 78
  has_many :unchangable_posts, :class_name => "Post", :before_add => :raise_exception, :after_add => :log_after_adding

79 80
  has_many :categorizations
  has_many :categories, :through => :categorizations
81
  has_many :named_categories, :through => :categorizations
82

83 84 85
  has_many :special_categorizations
  has_many :special_categories, :through => :special_categorizations, :source => :category
  has_one  :special_category,   :through => :special_categorizations, :source => :category
86

87 88
  has_many :categories_like_general, :through => :categorizations, :source => :category, :class_name => 'Category', :conditions => { :name => 'General' }

89 90 91
  has_many :categorized_posts, :through => :categorizations, :source => :post
  has_many :unique_categorized_posts, :through => :categorizations, :source => :post, :uniq => true

92
  has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
93

94 95 96
  has_many :author_favorites
  has_many :favorite_authors, :through => :author_favorites, :order => 'name'

97 98 99
  has_many :tagging,         :through => :posts
  has_many :taggings,        :through => :posts
  has_many :tags,            :through => :posts
100
  has_many :similar_posts,   :through => :tags,  :source => :tagged_posts, :uniq => true
101
  has_many :distinct_tags,   :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name"
102
  has_many :post_categories, :through => :posts, :source => :categories
103
  has_many :tagging_tags,    :through => :taggings, :source => :tag
104
  has_many :tags_with_primary_key, :through => :posts
105

106 107
  has_many :books
  has_many :subscriptions,        :through => :books
J
Jon Leighton 已提交
108
  has_many :subscribers,          :through => :subscriptions, :order => "subscribers.nick" # through has_many :through (on through reflection)
109
  has_many :distinct_subscribers, :through => :subscriptions, :source => :subscriber, :select => "DISTINCT subscribers.*", :order => "subscribers.nick"
J
Jon Leighton 已提交
110

111
  has_one :essay, :primary_key => :name, :as => :writer
112
  has_one :essay_category, :through => :essay, :source => :category
113
  has_one :essay_owner, :through => :essay, :source => :owner
114 115 116 117 118 119

  has_one :essay_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
  has_one :essay_category_2, :through => :essay_2, :source => :category

  has_many :essays, :primary_key => :name, :as => :writer
  has_many :essay_categories, :through => :essays, :source => :category
120
  has_many :essay_owners, :through => :essays, :source => :owner
J
Jon Leighton 已提交
121

122 123
  has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
  has_many :essay_categories_2, :through => :essays_2, :source => :category
124

125 126 127
  belongs_to :owned_essay, :primary_key => :name, :class_name => 'Essay'
  has_one :owned_essay_category, :through => :owned_essay, :source => :category

128
  belongs_to :author_address,       :dependent => :destroy
129
  belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
130

131
  has_many :post_categories, :through => :posts, :source => :categories
132
  has_many :category_post_comments, :through => :categories, :source => :post_comments
J
Jon Leighton 已提交
133

J
Jon Leighton 已提交
134 135
  has_many :misc_posts, :class_name => 'Post',
           :conditions => { :posts => { :title => ['misc post by bob', 'misc post by mary'] } }
136 137
  has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags

J
Jon Leighton 已提交
138 139
  has_many :misc_post_first_blue_tags_2, :through => :posts, :source => :first_blue_tags_2,
           :conditions => { :posts => { :title => ['misc post by bob', 'misc post by mary'] } }
140

141 142 143
  scope :relation_include_posts, includes(:posts)
  scope :relation_include_tags, includes(:tags)

144
  attr_accessor :post_log
145
  after_initialize :set_post_log
146

147
  def set_post_log
148
    @post_log = []
149 150
  end

151 152 153 154
  def label
    "#{id}-#{name}"
  end

155 156 157 158
  def social
    %w(twitter github)
  end

159 160
  validates_presence_of :name

161
  private
162
    def log_before_adding(object)
163
      @post_log << "before_adding#{object.id || '<new>'}"
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    end

    def log_after_adding(object)
      @post_log << "after_adding#{object.id}"
    end

    def log_before_removing(object)
      @post_log << "before_removing#{object.id}"
    end

    def log_after_removing(object)
      @post_log << "after_removing#{object.id}"
    end

    def raise_exception(object)
      raise Exception.new("You can't add a post")
    end
end
182 183 184

class AuthorAddress < ActiveRecord::Base
  has_one :author
185 186

  def self.destroyed_author_address_ids
187
    @destroyed_author_address_ids ||= []
188 189 190
  end

  before_destroy do |author_address|
191
    AuthorAddress.destroyed_author_address_ids << author_address.id
192
  end
193 194 195 196
end

class AuthorFavorite < ActiveRecord::Base
  belongs_to :author
197
  belongs_to :favorite_author, :class_name => "Author"
198
end