author.rb 9.9 KB
Newer Older
1
class Author < ActiveRecord::Base
2
  has_many :posts
3
  has_many :serialized_posts
4
  has_one :post
5
  has_many :very_special_comments, :through => :posts
6 7 8 9 10 11
  has_many :posts_with_comments, -> { includes(:comments) }, :class_name => "Post"
  has_many :popular_grouped_posts, -> { includes(:comments).group("type").having("SUM(comments_count) > 1").select("type") }, :class_name => "Post"
  has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order('comments.id') }, :class_name => "Post"
  has_many :posts_sorted_by_id_limited, -> { order('posts.id').limit(1) }, :class_name => "Post"
  has_many :posts_with_categories, -> { includes(:categories) }, :class_name => "Post"
  has_many :posts_with_comments_and_categories, -> { includes(:comments, :categories).order("posts.id") }, :class_name => "Post"
12
  has_many :posts_with_special_categorizations, :class_name => 'PostWithSpecialCategorization'
13 14
  has_one  :post_about_thinking, -> { where("posts.title like '%thinking%'") }, :class_name => 'Post'
  has_one  :post_about_thinking_with_last_comment, -> { where("posts.title like '%thinking%'").includes(:last_comment) }, :class_name => 'Post'
15 16 17 18 19
  has_many :comments, through: :posts do
    def ratings
      Rating.joins(:comment).merge(self)
    end
  end
20
  has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
21 22
  has_many :comments_with_order_and_conditions, -> { order('comments.body').where("comments.body like 'Thank%'") }, :through => :posts, :source => :comments
  has_many :comments_with_include, -> { includes(:post) }, :through => :posts, :source => :comments
23

24
  has_many :first_posts
25
  has_many :comments_on_first_posts, -> { order('posts.id desc, comments.id asc') }, :through => :first_posts, :source => :comments
26 27

  has_one :first_post
28
  has_one :comment_on_first_post, -> { order('posts.id desc, comments.id asc') }, :through => :first_post, :source => :comments
29

30 31
  has_many :thinking_posts, -> { where(:title => 'So I was thinking') }, :dependent => :delete_all, :class_name => 'Post'
  has_many :welcome_posts, -> { where(:title => 'Welcome to the weblog') }, :class_name => 'Post'
32

33
  has_many :welcome_posts_with_one_comment,
34 35 36 37 38 39
           -> { where(title: 'Welcome to the weblog').where('comments_count = ?', 1) },
           class_name: 'Post'
  has_many :welcome_posts_with_comments,
           -> { where(title: 'Welcome to the weblog').where(Post.arel_table[:comments_count].gt(0)) },
           class_name: 'Post'

40
  has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments
41
  has_many :funky_comments, :through => :posts, :source => :comments
42 43
  has_many :ordered_uniq_comments, -> { distinct.order('comments.id') }, :through => :posts, :source => :comments
  has_many :ordered_uniq_comments_desc, -> { distinct.order('comments.id DESC') }, :through => :posts, :source => :comments
44
  has_many :readonly_comments, -> { readonly }, :through => :posts, :source => :comments
45

46 47
  has_many :special_posts
  has_many :special_post_comments, :through => :special_posts, :source => :comments
48
  has_many :special_posts_with_default_scope, :class_name => 'SpecialPostWithDefaultScope'
J
Jeremy Kemper 已提交
49

50 51 52
  has_many :sti_posts, :class_name => 'StiPost'
  has_many :sti_post_comments, :through => :sti_posts, :source => :comments

53 54
  has_many :special_nonexistant_posts, -> { where("posts.body = 'nonexistant'") }, :class_name => "SpecialPost"
  has_many :special_nonexistant_post_comments, -> { where('comments.post_id' => 0) }, :through => :special_nonexistant_posts, :source => :comments
55
  has_many :nonexistant_comments, :through => :posts
56

57
  has_many :hello_posts, -> { where "posts.body = 'hello'" }, :class_name => "Post"
58
  has_many :hello_post_comments, :through => :hello_posts, :source => :comments
59
  has_many :posts_with_no_comments, -> { where('comments.id' => nil).includes(:comments) }, :class_name => 'Post'
60

61
  has_many :hello_posts_with_hash_conditions, -> { where(:body => 'hello') }, :class_name => "Post"
62 63 64
  has_many :hello_post_comments_with_hash_conditions, :through =>
:hello_posts_with_hash_conditions, :source => :comments

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

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

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

88
  has_many :categories_like_general, -> { where(:name => 'General') }, :through => :categorizations, :source => :category, :class_name => 'Category'
89

90
  has_many :categorized_posts, :through => :categorizations, :source => :post
91
  has_many :unique_categorized_posts, -> { distinct }, :through => :categorizations, :source => :post
92

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

95
  has_many :author_favorites
96
  has_many :favorite_authors, -> { order('name') }, :through => :author_favorites
97

98
  has_many :taggings,        :through => :posts, :source => :taggings
99
  has_many :taggings_2,      :through => :posts, :source => :tagging
100
  has_many :tags,            :through => :posts
101
  has_many :post_categories, :through => :posts, :source => :categories
102
  has_many :tagging_tags,    :through => :taggings, :source => :tag
103

104
  has_many :similar_posts, -> { distinct }, :through => :tags, :source => :tagged_posts
105 106
  has_many :distinct_tags, -> { select("DISTINCT tags.*").order("tags.name") }, :through => :posts, :source => :tags

107
  has_many :tags_with_primary_key, :through => :posts
108

109 110
  has_many :books
  has_many :subscriptions,        :through => :books
111 112
  has_many :subscribers, -> { order("subscribers.nick") }, :through => :subscriptions
  has_many :distinct_subscribers, -> { select("DISTINCT subscribers.*").order("subscribers.nick") }, :through => :subscriptions, :source => :subscriber
J
Jon Leighton 已提交
113

114
  has_one :essay, :primary_key => :name, :as => :writer
115
  has_one :essay_category, :through => :essay, :source => :category
116
  has_one :essay_owner, :through => :essay, :source => :owner
117 118 119 120 121 122

  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
123
  has_many :essay_owners, :through => :essays, :source => :owner
J
Jon Leighton 已提交
124

125 126
  has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
  has_many :essay_categories_2, :through => :essays_2, :source => :category
127

128 129 130
  belongs_to :owned_essay, :primary_key => :name, :class_name => 'Essay'
  has_one :owned_essay_category, :through => :owned_essay, :source => :category

131
  belongs_to :author_address,       :dependent => :destroy
132
  belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
133

134
  has_many :category_post_comments, :through => :categories, :source => :post_comments
J
Jon Leighton 已提交
135

136
  has_many :misc_posts, -> { where(:posts => { :title => ['misc post by bob', 'misc post by mary'] }) }, :class_name => 'Post'
137 138
  has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags

139 140
  has_many :misc_post_first_blue_tags_2, -> { where(:posts => { :title => ['misc post by bob', 'misc post by mary'] }) },
           :through => :posts, :source => :first_blue_tags_2
141

142 143 144
  has_many :posts_with_default_include, :class_name => 'PostWithDefaultInclude'
  has_many :comments_on_posts_with_default_include, :through => :posts_with_default_include, :source => :comments

A
Arthur Neves 已提交
145
  has_many :posts_with_signature, ->(record) { where("posts.title LIKE ?", "%by #{record.name.downcase}%") }, class_name: "Post"
146

J
Jon Leighton 已提交
147 148
  scope :relation_include_posts, -> { includes(:posts) }
  scope :relation_include_tags,  -> { includes(:tags) }
149

150
  attr_accessor :post_log
151
  after_initialize :set_post_log
152

153
  def set_post_log
154
    @post_log = []
155 156
  end

157 158 159 160
  def label
    "#{id}-#{name}"
  end

161 162 163 164
  def social
    %w(twitter github)
  end

165 166
  validates_presence_of :name

167
  private
168
    def log_before_adding(object)
169
      @post_log << "before_adding#{object.id || '<new>'}"
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    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
188 189 190

class AuthorAddress < ActiveRecord::Base
  has_one :author
191 192

  def self.destroyed_author_address_ids
193
    @destroyed_author_address_ids ||= []
194 195 196
  end

  before_destroy do |author_address|
197
    AuthorAddress.destroyed_author_address_ids << author_address.id
198
  end
199 200 201 202
end

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