2005-04-03 06:52:05 -04:00
|
|
|
class Post < ActiveRecord::Base
|
2010-04-02 12:34:48 -04:00
|
|
|
module NamedExtension
|
|
|
|
def author
|
|
|
|
'lifo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :containing_the_letter_a, -> { where("body LIKE '%a%'") }
|
|
|
|
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
|
|
|
|
2005-11-06 14:05:42 -05:00
|
|
|
belongs_to :author do
|
2005-11-03 04:06:42 -05:00
|
|
|
def greeting
|
|
|
|
"hello"
|
|
|
|
end
|
2005-11-06 14:05:42 -05:00
|
|
|
end
|
2005-11-03 04:06:42 -05:00
|
|
|
|
2006-09-15 03:02:05 -04:00
|
|
|
belongs_to :author_with_posts, :class_name => "Author", :foreign_key => :author_id, :include => :posts
|
2008-09-26 23:28:21 -04:00
|
|
|
belongs_to :author_with_address, :class_name => "Author", :foreign_key => :author_id, :include => :author_address
|
2005-11-06 15:39:34 -05:00
|
|
|
|
2011-11-29 12:14:21 -05:00
|
|
|
def first_comment
|
|
|
|
super.body
|
|
|
|
end
|
|
|
|
has_one :first_comment, :class_name => 'Comment', :order => 'id ASC'
|
2008-05-01 19:00:42 -04:00
|
|
|
has_one :last_comment, :class_name => 'Comment', :order => 'id desc'
|
|
|
|
|
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
|
|
|
|
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-04-27 06:42:22 -04:00
|
|
|
scoped(: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
|
|
|
|
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
|
2008-10-04 10:42:36 -04:00
|
|
|
|
2008-01-18 23:19:53 -05:00
|
|
|
has_many :comments_with_interpolated_conditions, :class_name => 'Comment',
|
2011-02-11 17:22:19 -05:00
|
|
|
:conditions => proc { ["#{"#{aliased_table_name}." rescue ""}body = ?", 'Thank you for the welcome'] }
|
2008-01-18 23:19:53 -05:00
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
has_one :very_special_comment
|
2005-11-06 15:39:34 -05:00
|
|
|
has_one :very_special_comment_with_post, :class_name => "VerySpecialComment", :include => :post
|
2005-11-04 14:39:50 -05:00
|
|
|
has_many :special_comments
|
2007-11-10 20:03:43 -05:00
|
|
|
has_many :nonexistant_comments, :class_name => 'Comment', :conditions => 'comments.id < 0'
|
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
|
|
|
|
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
|
|
|
|
|
|
|
has_many :taggings, :as => :taggable
|
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-04-27 06:42:22 -04:00
|
|
|
scoped(: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').all
|
2006-03-21 11:33:22 -05:00
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2011-02-11 17:22:19 -05:00
|
|
|
has_many :interpolated_taggings, :class_name => 'Tagging', :as => :taggable, :conditions => proc { "1 = #{1}" }
|
|
|
|
has_many :interpolated_tags, :through => :taggings
|
|
|
|
has_many :interpolated_tags_2, :through => :interpolated_taggings, :source => :tag
|
|
|
|
|
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
|
|
|
|
2011-03-16 14:54:34 -04:00
|
|
|
has_many :misc_tags, :through => :taggings, :source => :tag, :conditions => { :tags => { :name => 'Misc' } }
|
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
|
|
|
|
2011-03-16 14:54:34 -04:00
|
|
|
has_many :first_taggings, :as => :taggable, :class_name => 'Tagging', :conditions => { :taggings => { :comment => 'first' } }
|
|
|
|
has_many :first_blue_tags, :through => :first_taggings, :source => :tag, :conditions => { :tags => { :name => 'Blue' } }
|
2010-10-18 19:27:40 -04:00
|
|
|
|
2011-03-16 14:54:34 -04:00
|
|
|
has_many :first_blue_tags_2, :through => :taggings, :source => :blue_tag, :conditions => { :taggings => { :comment => 'first' } }
|
2006-02-09 14:37:05 -05:00
|
|
|
|
2006-03-18 18:14:31 -05:00
|
|
|
has_many :invalid_taggings, :as => :taggable, :class_name => "Tagging", :conditions => 'taggings.id < 0'
|
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
|
|
|
|
|
|
|
|
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
|
2010-05-15 11:33:18 -04:00
|
|
|
has_many :readers_with_person, :include => :person, :class_name => "Reader"
|
2006-02-10 00:19:41 -05:00
|
|
|
has_many :people, :through => :readers
|
2012-03-04 22:45:17 -05:00
|
|
|
has_many :secure_people, :through => :secure_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) }
|
2009-06-21 12:35:04 -04:00
|
|
|
has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true }
|
|
|
|
has_many :impatient_people, :through => :skimmers, :source => :person
|
2008-04-05 20:27:12 -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
|
|
|
|
|
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
|
2006-02-10 00:19:41 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def self.what_are_you
|
|
|
|
'a post...'
|
|
|
|
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
|
|
|
|
2011-04-03 19:07:45 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
class DeprecatedPostWithComment < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
|
|
|
default_scope where("posts.comments_count > 0").order("posts.comments_count ASC")
|
|
|
|
end
|
2010-01-23 03:10:38 -05:00
|
|
|
end
|
2010-10-14 23:31:05 -04:00
|
|
|
|
|
|
|
class PostForAuthor < ActiveRecord::Base
|
|
|
|
self.table_name = 'posts'
|
|
|
|
cattr_accessor :selected_author
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
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
|