2005-04-03 06:52:05 -04:00
|
|
|
class Post < ActiveRecord::Base
|
2013-08-29 14:49:23 -04:00
|
|
|
class CategoryPost < ActiveRecord::Base
|
|
|
|
self.table_name = "categories_posts"
|
|
|
|
belongs_to :category
|
|
|
|
belongs_to :post
|
|
|
|
end
|
|
|
|
|
2010-04-02 12:34:48 -04:00
|
|
|
module NamedExtension
|
|
|
|
def author
|
|
|
|
'lifo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-18 07:30:47 -05:00
|
|
|
module NamedExtension2
|
|
|
|
def greeting
|
|
|
|
"hello"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :containing_the_letter_a, -> { where("body LIKE '%a%'") }
|
2014-06-22 23:42:46 -04:00
|
|
|
scope :titled_with_an_apostrophe, -> { where("title LIKE '%''%'") }
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :ranked_by_comments, -> { order("comments_count DESC") }
|
2011-04-08 18:54:54 -04:00
|
|
|
|
2011-04-17 15:47:52 -04:00
|
|
|
scope :limit_by, lambda {|l| limit(l) }
|
2008-06-21 07:41:30 -04:00
|
|
|
|
2013-01-24 04:07:51 -05:00
|
|
|
belongs_to :author
|
2005-11-03 04:06:42 -05:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
belongs_to :author_with_posts, -> { includes(:posts) }, :class_name => "Author", :foreign_key => :author_id
|
|
|
|
belongs_to :author_with_address, -> { includes(:author_address) }, :class_name => "Author", :foreign_key => :author_id
|
2005-11-06 15:39:34 -05:00
|
|
|
|
2011-11-29 12:14:21 -05:00
|
|
|
def first_comment
|
|
|
|
super.body
|
|
|
|
end
|
2012-07-13 14:34:40 -04:00
|
|
|
has_one :first_comment, -> { order('id ASC') }, :class_name => 'Comment'
|
|
|
|
has_one :last_comment, -> { order('id desc') }, :class_name => 'Comment'
|
2008-05-01 19:00:42 -04:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :with_special_comments, -> { joins(:comments).where(:comments => {:type => 'SpecialComment'}) }
|
|
|
|
scope :with_very_special_comments, -> { joins(:comments).where(:comments => {:type => 'VerySpecialComment'}) }
|
2012-04-26 07:03:25 -04:00
|
|
|
scope :with_post, ->(post_id) { joins(:comments).where(:comments => { :post_id => post_id }) }
|
2009-01-24 12:54:10 -05:00
|
|
|
|
2013-01-12 10:22:17 -05:00
|
|
|
scope :with_comments, -> { preload(:comments) }
|
|
|
|
scope :with_tags, -> { preload(:taggings) }
|
|
|
|
|
2014-04-11 19:14:51 -04:00
|
|
|
scope :tagged_with, ->(id) { joins(:taggings).where(taggings: { tag_id: id }) }
|
2014-08-18 17:18:34 -04:00
|
|
|
scope :tagged_with_comment, ->(comment) { joins(:taggings).where(taggings: { comment: comment }) }
|
2014-04-11 19:14:51 -04:00
|
|
|
|
2014-06-22 23:42:46 -04:00
|
|
|
scope :typographically_interesting, -> { containing_the_letter_a.or(titled_with_an_apostrophe) }
|
|
|
|
|
2010-01-18 13:09:19 -05:00
|
|
|
has_many :comments do
|
2005-11-03 04:06:42 -05:00
|
|
|
def find_most_recent
|
2012-07-27 12:27:47 -04:00
|
|
|
order("id DESC").first
|
2005-11-03 04:06:42 -05:00
|
|
|
end
|
2011-06-02 22:58:00 -04:00
|
|
|
|
|
|
|
def newest
|
|
|
|
created.last
|
|
|
|
end
|
2011-12-08 14:58:59 -05:00
|
|
|
|
|
|
|
def the_association
|
|
|
|
proxy_association
|
|
|
|
end
|
2005-11-06 14:05:42 -05:00
|
|
|
end
|
2005-11-03 04:06:42 -05:00
|
|
|
|
2013-01-18 07:30:47 -05:00
|
|
|
has_many :comments_with_extend, extend: NamedExtension, class_name: "Comment", foreign_key: "post_id" do
|
|
|
|
def greeting
|
|
|
|
"hello"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
has_many :comments_with_extend_2, extend: [NamedExtension, NamedExtension2], class_name: "Comment", foreign_key: "post_id"
|
|
|
|
|
2008-10-04 10:42:36 -04:00
|
|
|
has_many :author_favorites, :through => :author
|
2010-12-16 05:29:13 -05:00
|
|
|
has_many :author_categorizations, :through => :author, :source => :categorizations
|
2011-02-05 08:13:49 -05:00
|
|
|
has_many :author_addresses, :through => :author
|
2013-10-27 13:39:22 -04:00
|
|
|
has_many :author_address_extra_with_address,
|
|
|
|
through: :author_with_address,
|
|
|
|
source: :author_address_extra
|
2008-10-04 10:42:36 -04:00
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
has_one :very_special_comment
|
2012-07-13 14:34:40 -04:00
|
|
|
has_one :very_special_comment_with_post, -> { includes(:post) }, :class_name => "VerySpecialComment"
|
2014-11-19 15:05:04 -05:00
|
|
|
has_one :very_special_comment_with_post_with_joins, -> { joins(:post).order('posts.id') }, class_name: "VerySpecialComment"
|
2005-11-04 14:39:50 -05:00
|
|
|
has_many :special_comments
|
2014-12-02 19:19:10 -05:00
|
|
|
has_many :nonexistent_comments, -> { where 'comments.id < 0' }, :class_name => 'Comment'
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2010-10-09 17:00:33 -04:00
|
|
|
has_many :special_comments_ratings, :through => :special_comments, :source => :ratings
|
2011-03-05 15:10:24 -05:00
|
|
|
has_many :special_comments_ratings_taggings, :through => :special_comments_ratings, :source => :taggings
|
2005-11-03 04:06:42 -05:00
|
|
|
|
2013-08-29 14:49:23 -04:00
|
|
|
has_many :category_posts, :class_name => 'CategoryPost'
|
|
|
|
has_many :scategories, through: :category_posts, source: :category
|
2005-04-10 13:24:56 -04:00
|
|
|
has_and_belongs_to_many :categories
|
2006-03-15 21:10:11 -05:00
|
|
|
has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id'
|
2005-12-02 01:03:43 -05:00
|
|
|
|
2014-06-15 11:19:54 -04:00
|
|
|
has_many :taggings, :as => :taggable, :counter_cache => :tags_count
|
2008-02-17 19:14:54 -05:00
|
|
|
has_many :tags, :through => :taggings do
|
2006-03-21 11:33:22 -05:00
|
|
|
def add_joins_and_select
|
2012-07-27 12:27:47 -04:00
|
|
|
select('tags.*, authors.id as author_id')
|
|
|
|
.joins('left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id')
|
|
|
|
.to_a
|
2006-03-21 11:33:22 -05:00
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2010-12-17 15:54:50 -05:00
|
|
|
has_many :taggings_with_delete_all, :class_name => 'Tagging', :as => :taggable, :dependent => :delete_all
|
2011-02-05 08:13:49 -05:00
|
|
|
has_many :taggings_with_destroy, :class_name => 'Tagging', :as => :taggable, :dependent => :destroy
|
|
|
|
|
|
|
|
has_many :tags_with_destroy, :through => :taggings, :source => :tag, :dependent => :destroy
|
|
|
|
has_many :tags_with_nullify, :through => :taggings, :source => :tag, :dependent => :nullify
|
2010-12-17 15:54:50 -05:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :misc_tags, -> { where :tags => { :name => 'Misc' } }, :through => :taggings, :source => :tag
|
2006-03-24 09:46:17 -05:00
|
|
|
has_many :funky_tags, :through => :taggings, :source => :tag
|
2006-03-18 17:38:49 -05:00
|
|
|
has_many :super_tags, :through => :taggings
|
2010-10-19 12:22:42 -04:00
|
|
|
has_many :tags_with_primary_key, :through => :taggings, :source => :tag_with_primary_key
|
2006-02-09 14:37:05 -05:00
|
|
|
has_one :tagging, :as => :taggable
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :first_taggings, -> { where :taggings => { :comment => 'first' } }, :as => :taggable, :class_name => 'Tagging'
|
|
|
|
has_many :first_blue_tags, -> { where :tags => { :name => 'Blue' } }, :through => :first_taggings, :source => :tag
|
2010-10-18 19:27:40 -04:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :first_blue_tags_2, -> { where :taggings => { :comment => 'first' } }, :through => :taggings, :source => :blue_tag
|
2006-02-09 14:37:05 -05:00
|
|
|
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :invalid_taggings, -> { where 'taggings.id < 0' }, :as => :taggable, :class_name => "Tagging"
|
2006-03-24 09:46:17 -05:00
|
|
|
has_many :invalid_tags, :through => :invalid_taggings, :source => :tag
|
2006-03-18 18:14:31 -05:00
|
|
|
|
2006-01-21 18:40:20 -05:00
|
|
|
has_many :categorizations, :foreign_key => :category_id
|
|
|
|
has_many :authors, :through => :categorizations
|
|
|
|
|
2010-12-15 18:27:15 -05:00
|
|
|
has_many :categorizations_using_author_id, :primary_key => :author_id, :foreign_key => :post_id, :class_name => 'Categorization'
|
|
|
|
has_many :authors_using_author_id, :through => :categorizations_using_author_id, :source => :author
|
|
|
|
|
|
|
|
has_many :taggings_using_author_id, :primary_key => :author_id, :as => :taggable, :class_name => 'Tagging'
|
|
|
|
has_many :tags_using_author_id, :through => :taggings_using_author_id, :source => :tag
|
|
|
|
|
2014-12-08 14:43:16 -05:00
|
|
|
has_many :images, :as => :imageable, :foreign_key => :imageable_identifier, :foreign_type => :imageable_class
|
|
|
|
has_one :main_image, :as => :imageable, :foreign_key => :imageable_identifier, :foreign_type => :imageable_class, :class_name => 'Image'
|
|
|
|
|
2010-12-15 18:27:15 -05:00
|
|
|
has_many :standard_categorizations, :class_name => 'Categorization', :foreign_key => :post_id
|
|
|
|
has_many :author_using_custom_pk, :through => :standard_categorizations
|
|
|
|
has_many :authors_using_custom_pk, :through => :standard_categorizations
|
2011-02-14 15:39:51 -05:00
|
|
|
has_many :named_categories, :through => :standard_categorizations
|
2010-12-15 18:27:15 -05:00
|
|
|
|
2006-02-10 00:19:41 -05:00
|
|
|
has_many :readers
|
2012-03-04 22:45:17 -05:00
|
|
|
has_many :secure_readers
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :readers_with_person, -> { includes(:person) }, :class_name => "Reader"
|
2006-02-10 00:19:41 -05:00
|
|
|
has_many :people, :through => :readers
|
2011-02-14 18:14:42 -05:00
|
|
|
has_many :single_people, :through => :readers
|
2008-04-05 20:27:12 -04:00
|
|
|
has_many :people_with_callbacks, :source=>:person, :through => :readers,
|
|
|
|
:before_add => lambda {|owner, reader| log(:added, :before, reader.first_name) },
|
|
|
|
:after_add => lambda {|owner, reader| log(:added, :after, reader.first_name) },
|
|
|
|
:before_remove => lambda {|owner, reader| log(:removed, :before, reader.first_name) },
|
|
|
|
:after_remove => lambda {|owner, reader| log(:removed, :after, reader.first_name) }
|
2012-07-13 14:34:40 -04:00
|
|
|
has_many :skimmers, -> { where :skimmer => true }, :class_name => 'Reader'
|
2009-06-21 12:35:04 -04:00
|
|
|
has_many :impatient_people, :through => :skimmers, :source => :person
|
2008-04-05 20:27:12 -04:00
|
|
|
|
2012-08-28 15:48:59 -04:00
|
|
|
has_many :lazy_readers
|
|
|
|
has_many :lazy_readers_skimmers_or_not, -> { where(skimmer: [ true, false ]) }, :class_name => 'LazyReader'
|
|
|
|
|
2014-04-02 19:52:42 -04:00
|
|
|
has_many :lazy_people, :through => :lazy_readers, :source => :person
|
|
|
|
has_many :lazy_readers_unscope_skimmers, -> { skimmers_or_not }, :class_name => 'LazyReader'
|
|
|
|
has_many :lazy_people_unscope_skimmers, :through => :lazy_readers_unscope_skimmers, :source => :person
|
2012-08-28 15:48:59 -04:00
|
|
|
|
2009-02-13 01:07:39 -05:00
|
|
|
def self.top(limit)
|
2010-01-17 11:04:26 -05:00
|
|
|
ranked_by_comments.limit_by(limit)
|
2009-02-13 01:07:39 -05:00
|
|
|
end
|
|
|
|
|
2014-04-01 23:18:16 -04:00
|
|
|
def self.written_by(author)
|
|
|
|
where(id: author.posts.pluck(:id))
|
|
|
|
end
|
|
|
|
|
2008-04-05 20:27:12 -04:00
|
|
|
def self.reset_log
|
|
|
|
@log = []
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-04-05 20:27:12 -04:00
|
|
|
def self.log(message=nil, side=nil, new_record=nil)
|
|
|
|
return @log if message.nil?
|
|
|
|
@log << [message, side, new_record]
|
|
|
|
end
|
2005-04-10 11:49:49 -04:00
|
|
|
end
|
|
|
|
|
2006-12-01 16:24:47 -05:00
|
|
|
class SpecialPost < Post; end
|
2005-07-03 04:21:22 -04:00
|
|
|
|
|
|
|
class StiPost < Post
|
2006-03-15 21:46:01 -05:00
|
|
|
self.abstract_class = true
|
2005-07-03 04:21:22 -04:00
|
|
|
has_one :special_comment, :class_name => "SpecialComment"
|
|
|
|
end
|
2006-03-15 21:46:01 -05:00
|
|
|
|
|
|
|
class SubStiPost < StiPost
|
2006-12-01 16:24:47 -05:00
|
|
|
self.table_name = Post.table_name
|
2006-03-15 21:46:01 -05:00
|
|
|
end
|
2010-01-23 03:10:38 -05:00
|
|
|
|
2010-12-12 11:35:27 -05:00
|
|
|
class FirstPost < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { where(:id => 1) }
|
2011-04-03 19:07:45 -04:00
|
|
|
|
2010-12-12 11:35:27 -05:00
|
|
|
has_many :comments, :foreign_key => :post_id
|
|
|
|
has_one :comment, :foreign_key => :post_id
|
|
|
|
end
|
2011-05-24 02:21:32 -04:00
|
|
|
|
|
|
|
class PostWithDefaultInclude < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { includes(:comments) }
|
2011-05-24 02:21:32 -04:00
|
|
|
has_many :comments, :foreign_key => :post_id
|
2011-09-04 14:42:57 -04:00
|
|
|
end
|
|
|
|
|
2013-01-14 15:39:51 -05:00
|
|
|
class PostWithSpecialCategorization < Post
|
|
|
|
has_many :categorizations, :foreign_key => :post_id
|
|
|
|
default_scope { where(:type => 'PostWithSpecialCategorization').joins(:categorizations).where(:categorizations => { :special => true }) }
|
|
|
|
end
|
|
|
|
|
2011-09-04 14:42:57 -04:00
|
|
|
class PostWithDefaultScope < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { order(:title) }
|
2011-09-04 14:42:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class SpecialPostWithDefaultScope < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { where(:id => [1, 5,6]) }
|
2011-12-08 14:58:59 -05:00
|
|
|
end
|
2014-05-12 17:30:05 -04:00
|
|
|
|
|
|
|
class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
|
|
|
has_many :comments, class_name: "CommentThatAutomaticallyAltersPostBody", foreign_key: :post_id
|
2014-05-14 19:29:30 -04:00
|
|
|
|
2014-05-12 17:30:05 -04:00
|
|
|
after_save do |post|
|
|
|
|
post.comments.load
|
|
|
|
end
|
|
|
|
end
|
2014-08-31 15:18:02 -04:00
|
|
|
|
2014-10-13 10:50:32 -04:00
|
|
|
class PostWithAfterCreateCallback < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
|
|
|
has_many :comments, foreign_key: :post_id
|
|
|
|
|
|
|
|
after_create do |post|
|
|
|
|
update_attribute(:author_id, comments.first.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-31 15:18:02 -04:00
|
|
|
class PostWithCommentWithDefaultScopeReferencesAssociation < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
|
|
|
has_many :comment_with_default_scope_references_associations, foreign_key: :post_id
|
|
|
|
has_one :first_comment, class_name: "CommentWithDefaultScopeReferencesAssociation", foreign_key: :post_id
|
|
|
|
end
|
2014-10-31 11:43:38 -04:00
|
|
|
|
|
|
|
class SerializedPost < ActiveRecord::Base
|
|
|
|
serialize :title
|
|
|
|
end
|