2005-04-03 06:52:05 -04:00
|
|
|
class Author < ActiveRecord::Base
|
2008-09-10 13:50:01 -04:00
|
|
|
has_many :posts
|
2012-04-05 15:21:48 -04:00
|
|
|
has_one :post
|
2009-08-09 15:53:22 -04:00
|
|
|
has_many :very_special_comments, :through => :posts
|
2012-07-13 14:34:40 -04:00
|
|
|
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"
|
2013-01-14 15:39:51 -05:00
|
|
|
has_many :posts_with_special_categorizations, :class_name => 'PostWithSpecialCategorization'
|
2012-07-13 14:34:40 -04:00
|
|
|
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'
|
2013-07-08 06:51:15 -04:00
|
|
|
has_many :comments, through: :posts do
|
|
|
|
def ratings
|
|
|
|
Rating.joins(:comment).merge(self)
|
|
|
|
end
|
|
|
|
end
|
2008-03-23 22:50:02 -04:00
|
|
|
has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
|
2012-07-13 14:34:40 -04:00
|
|
|
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
|
2008-05-07 16:35:41 -04:00
|
|
|
|
2010-12-12 11:35:27 -05:00
|
|
|
has_many :first_posts
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :comments_on_first_posts, -> { order('posts.id desc, comments.id asc') }, :through => :first_posts, :source => :comments
|
2010-12-19 09:17:29 -05:00
|
|
|
|
|
|
|
has_one :first_post
|
2012-07-13 14:34:40 -04:00
|
|
|
has_one :comment_on_first_post, -> { order('posts.id desc, comments.id asc') }, :through => :first_post, :source => :comments
|
2010-12-12 11:35:27 -05:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
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'
|
2008-05-07 16:35:41 -04:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments
|
2006-03-24 09:46:17 -05:00
|
|
|
has_many :funky_comments, :through => :posts, :source => :comments
|
2013-03-12 05:23:08 -04:00
|
|
|
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
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :readonly_comments, -> { readonly }, :through => :posts, :source => :comments
|
2010-03-07 19:53:21 -05:00
|
|
|
|
2006-10-15 12:37:11 -04:00
|
|
|
has_many :special_posts
|
|
|
|
has_many :special_post_comments, :through => :special_posts, :source => :comments
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2008-08-16 17:20:55 -04:00
|
|
|
has_many :sti_posts, :class_name => 'StiPost'
|
|
|
|
has_many :sti_post_comments, :through => :sti_posts, :source => :comments
|
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
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
|
2007-11-10 20:03:43 -05:00
|
|
|
has_many :nonexistant_comments, :through => :posts
|
2006-10-15 12:37:11 -04:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :hello_posts, -> { where "posts.body = 'hello'" }, :class_name => "Post"
|
2006-10-15 12:37:11 -04:00
|
|
|
has_many :hello_post_comments, :through => :hello_posts, :source => :comments
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :posts_with_no_comments, -> { where('comments.id' => nil).includes(:comments) }, :class_name => 'Post'
|
2006-10-15 12:37:11 -04:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :hello_posts_with_hash_conditions, -> { where(:body => 'hello') }, :class_name => "Post"
|
2008-03-28 12:13:57 -04:00
|
|
|
has_many :hello_post_comments_with_hash_conditions, :through =>
|
|
|
|
:hello_posts_with_hash_conditions, :source => :comments
|
|
|
|
|
2006-10-08 22:46:57 -04:00
|
|
|
has_many :other_posts, :class_name => "Post"
|
2005-07-04 04:43:57 -04:00
|
|
|
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
|
2008-01-18 02:27:03 -05:00
|
|
|
:after_add => :log_after_adding,
|
2006-03-18 18:14:31 -05:00
|
|
|
:before_remove => :log_before_removing,
|
|
|
|
:after_remove => :log_after_removing
|
2005-11-07 23:36:37 -05:00
|
|
|
has_many :posts_with_proc_callbacks, :class_name => "Post",
|
2007-10-16 01:07:58 -04:00
|
|
|
: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>'}"},
|
2005-11-07 23:36:37 -05:00
|
|
|
:before_remove => Proc.new {|o, r| o.post_log << "before_removing#{r.id}"},
|
2006-03-18 18:14:31 -05:00
|
|
|
:after_remove => Proc.new {|o, r| o.post_log << "after_removing#{r.id}"}
|
2005-11-07 23:36:37 -05:00
|
|
|
has_many :posts_with_multiple_callbacks, :class_name => "Post",
|
2007-10-16 01:07:58 -04:00
|
|
|
: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>'}"}]
|
2005-07-04 04:43:57 -04:00
|
|
|
has_many :unchangable_posts, :class_name => "Post", :before_add => :raise_exception, :after_add => :log_after_adding
|
|
|
|
|
2005-12-11 13:06:51 -05:00
|
|
|
has_many :categorizations
|
|
|
|
has_many :categories, :through => :categorizations
|
2009-08-13 15:18:43 -04:00
|
|
|
has_many :named_categories, :through => :categorizations
|
2006-03-20 20:07:16 -05:00
|
|
|
|
2010-12-12 12:03:41 -05:00
|
|
|
has_many :special_categorizations
|
|
|
|
has_many :special_categories, :through => :special_categorizations, :source => :category
|
|
|
|
has_one :special_category, :through => :special_categorizations, :source => :category
|
2006-03-20 20:07:16 -05:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :categories_like_general, -> { where(:name => 'General') }, :through => :categorizations, :source => :category, :class_name => 'Category'
|
2006-09-01 01:31:56 -04:00
|
|
|
|
2006-05-06 19:37:56 -04:00
|
|
|
has_many :categorized_posts, :through => :categorizations, :source => :post
|
2013-03-12 05:23:08 -04:00
|
|
|
has_many :unique_categorized_posts, -> { distinct }, :through => :categorizations, :source => :post
|
2006-05-06 19:37:56 -04:00
|
|
|
|
2006-03-17 22:39:18 -05:00
|
|
|
has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
|
2005-12-11 13:06:51 -05:00
|
|
|
|
2006-03-20 20:07:16 -05:00
|
|
|
has_many :author_favorites
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :favorite_authors, -> { order('name') }, :through => :author_favorites
|
2006-03-20 20:07:16 -05:00
|
|
|
|
2013-06-13 12:46:30 -04:00
|
|
|
has_many :taggings, :through => :posts, :source => :taggings
|
2012-08-10 12:42:48 -04:00
|
|
|
has_many :taggings_2, :through => :posts, :source => :tagging
|
2010-10-12 13:16:31 -04:00
|
|
|
has_many :tags, :through => :posts
|
2006-04-05 11:36:02 -04:00
|
|
|
has_many :post_categories, :through => :posts, :source => :categories
|
2010-10-14 08:07:28 -04:00
|
|
|
has_many :tagging_tags, :through => :taggings, :source => :tag
|
2012-07-13 14:34:40 -04:00
|
|
|
|
2013-03-12 05:23:08 -04:00
|
|
|
has_many :similar_posts, -> { distinct }, :through => :tags, :source => :tagged_posts
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :distinct_tags, -> { select("DISTINCT tags.*").order("tags.name") }, :through => :posts, :source => :tags
|
|
|
|
|
2010-10-19 12:22:42 -04:00
|
|
|
has_many :tags_with_primary_key, :through => :posts
|
2006-04-05 11:36:02 -04:00
|
|
|
|
2010-09-26 08:17:18 -04:00
|
|
|
has_many :books
|
|
|
|
has_many :subscriptions, :through => :books
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :subscribers, -> { order("subscribers.nick") }, :through => :subscriptions
|
|
|
|
has_many :distinct_subscribers, -> { select("DISTINCT subscribers.*").order("subscribers.nick") }, :through => :subscriptions, :source => :subscriber
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2009-07-15 16:22:25 -04:00
|
|
|
has_one :essay, :primary_key => :name, :as => :writer
|
2010-10-19 07:47:19 -04:00
|
|
|
has_one :essay_category, :through => :essay, :source => :category
|
2010-10-19 09:14:06 -04:00
|
|
|
has_one :essay_owner, :through => :essay, :source => :owner
|
2010-10-19 07:47:19 -04:00
|
|
|
|
|
|
|
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
|
2010-10-19 09:14:06 -04:00
|
|
|
has_many :essay_owners, :through => :essays, :source => :owner
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2010-10-19 07:47:19 -04:00
|
|
|
has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
|
|
|
|
has_many :essay_categories_2, :through => :essays_2, :source => :category
|
2009-07-15 16:22:25 -04:00
|
|
|
|
2010-10-19 11:13:06 -04:00
|
|
|
belongs_to :owned_essay, :primary_key => :name, :class_name => 'Essay'
|
|
|
|
has_one :owned_essay_category, :through => :owned_essay, :source => :category
|
|
|
|
|
2010-09-26 08:17:18 -04:00
|
|
|
belongs_to :author_address, :dependent => :destroy
|
2008-01-19 00:30:42 -05:00
|
|
|
belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
|
2006-03-19 14:32:07 -05:00
|
|
|
|
2010-10-14 07:59:16 -04:00
|
|
|
has_many :category_post_comments, :through => :categories, :source => :post_comments
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :misc_posts, -> { where(:posts => { :title => ['misc post by bob', 'misc post by mary'] }) }, :class_name => 'Post'
|
2010-10-18 19:27:40 -04:00
|
|
|
has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags
|
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
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
|
2010-10-12 13:16:31 -04:00
|
|
|
|
2011-05-24 02:21:32 -04:00
|
|
|
has_many :posts_with_default_include, :class_name => 'PostWithDefaultInclude'
|
|
|
|
has_many :comments_on_posts_with_default_include, :through => :posts_with_default_include, :source => :comments
|
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :relation_include_posts, -> { includes(:posts) }
|
|
|
|
scope :relation_include_tags, -> { includes(:tags) }
|
2010-08-03 17:26:59 -04:00
|
|
|
|
2005-07-04 04:43:57 -04:00
|
|
|
attr_accessor :post_log
|
2009-09-08 11:22:45 -04:00
|
|
|
after_initialize :set_post_log
|
2005-07-04 04:43:57 -04:00
|
|
|
|
2009-09-08 11:22:45 -04:00
|
|
|
def set_post_log
|
2005-11-07 23:36:37 -05:00
|
|
|
@post_log = []
|
2005-07-04 04:43:57 -04:00
|
|
|
end
|
|
|
|
|
2007-06-29 23:31:48 -04:00
|
|
|
def label
|
|
|
|
"#{id}-#{name}"
|
|
|
|
end
|
|
|
|
|
2010-04-29 17:39:05 -04:00
|
|
|
def social
|
|
|
|
%w(twitter github)
|
|
|
|
end
|
|
|
|
|
2010-07-13 15:30:23 -04:00
|
|
|
validates_presence_of :name
|
|
|
|
|
2005-07-04 04:43:57 -04:00
|
|
|
private
|
2005-11-07 23:36:37 -05:00
|
|
|
def log_before_adding(object)
|
2007-10-16 01:07:58 -04:00
|
|
|
@post_log << "before_adding#{object.id || '<new>'}"
|
2005-11-07 23:36:37 -05:00
|
|
|
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
|
2006-03-19 14:32:07 -05:00
|
|
|
|
|
|
|
class AuthorAddress < ActiveRecord::Base
|
|
|
|
has_one :author
|
2008-01-19 00:30:42 -05:00
|
|
|
|
|
|
|
def self.destroyed_author_address_ids
|
2010-03-07 19:53:21 -05:00
|
|
|
@destroyed_author_address_ids ||= []
|
2008-01-19 00:30:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before_destroy do |author_address|
|
2010-03-07 19:53:21 -05:00
|
|
|
AuthorAddress.destroyed_author_address_ids << author_address.id
|
2008-01-19 00:30:42 -05:00
|
|
|
end
|
2006-03-20 20:07:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class AuthorFavorite < ActiveRecord::Base
|
|
|
|
belongs_to :author
|
2007-12-20 20:49:01 -05:00
|
|
|
belongs_to :favorite_author, :class_name => "Author"
|
2006-05-06 19:37:56 -04:00
|
|
|
end
|