2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-22 14:09:56 -04:00
|
|
|
# `counter_cache` requires association class before `attr_readonly`.
|
|
|
|
class Post < ActiveRecord::Base; end
|
|
|
|
|
2005-04-03 06:52:05 -04:00
|
|
|
class Comment < ActiveRecord::Base
|
2016-08-16 03:30:11 -04:00
|
|
|
scope :limit_by, lambda { |l| limit(l) }
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :containing_the_letter_e, -> { where("comments.body LIKE '%e%'") }
|
|
|
|
scope :not_again, -> { where("comments.body NOT LIKE '%again%'") }
|
2016-08-06 13:37:57 -04:00
|
|
|
scope :for_first_post, -> { where(post_id: 1) }
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :for_first_author, -> { joins(:post).where("posts.author_id" => 1) }
|
2012-07-27 12:27:47 -04:00
|
|
|
scope :created, -> { all }
|
2011-07-11 19:04:01 -04:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
belongs_to :post, counter_cache: true
|
2014-04-23 22:51:00 -04:00
|
|
|
belongs_to :author, polymorphic: true
|
|
|
|
belongs_to :resource, polymorphic: true
|
|
|
|
|
2010-10-09 17:00:33 -04:00
|
|
|
has_many :ratings
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
belongs_to :first_post, foreign_key: :post_id
|
2014-12-20 04:57:06 -05:00
|
|
|
belongs_to :special_post_with_default_scope, foreign_key: :post_id
|
2012-04-03 15:13:24 -04:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :children, class_name: "Comment", foreign_key: :parent_id
|
|
|
|
belongs_to :parent, class_name: "Comment", counter_cache: :children_count
|
2011-07-11 19:04:01 -04:00
|
|
|
|
2017-05-15 23:10:37 -04:00
|
|
|
class ::OopsError < RuntimeError; end
|
|
|
|
|
|
|
|
module OopsExtension
|
|
|
|
def destroy_all(*)
|
|
|
|
raise OopsError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
default_scope { extending OopsExtension }
|
|
|
|
|
2017-05-31 12:12:07 -04:00
|
|
|
scope :oops_comments, -> { extending OopsExtension }
|
|
|
|
|
2017-04-21 20:26:18 -04:00
|
|
|
# Should not be called if extending modules that having the method exists on an association.
|
|
|
|
def self.greeting
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def self.what_are_you
|
2016-08-06 12:26:20 -04:00
|
|
|
"a comment..."
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def self.search_by_type(q)
|
2012-07-27 12:27:47 -04:00
|
|
|
where("#{QUOTED_TYPE} = ?", q)
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
2011-01-02 15:33:18 -05:00
|
|
|
|
|
|
|
def self.all_as_method
|
|
|
|
all
|
|
|
|
end
|
2012-07-27 12:27:47 -04:00
|
|
|
scope :all_as_scope, -> { all }
|
2014-05-05 03:48:59 -04:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
body
|
|
|
|
end
|
2005-04-10 11:49:49 -04:00
|
|
|
end
|
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
class SpecialComment < Comment
|
2017-07-17 21:22:05 -04:00
|
|
|
default_scope { where(deleted_at: nil) }
|
2017-11-05 18:29:40 -05:00
|
|
|
|
|
|
|
def self.what_are_you
|
|
|
|
"a special comment..."
|
|
|
|
end
|
2005-11-04 14:39:50 -05:00
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
|
2010-10-06 07:06:51 -04:00
|
|
|
class SubSpecialComment < SpecialComment
|
|
|
|
end
|
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
class VerySpecialComment < Comment
|
2008-01-26 01:23:03 -05:00
|
|
|
end
|
2014-05-12 17:30:05 -04:00
|
|
|
|
|
|
|
class CommentThatAutomaticallyAltersPostBody < Comment
|
|
|
|
belongs_to :post, class_name: "PostThatLoadsCommentsInAnAfterSaveHook", foreign_key: :post_id
|
2014-05-14 19:29:30 -04:00
|
|
|
|
2014-05-12 17:30:05 -04:00
|
|
|
after_save do |comment|
|
|
|
|
comment.post.update_attributes(body: "Automatically altered")
|
|
|
|
end
|
2014-05-14 19:29:30 -04:00
|
|
|
end
|
2014-08-31 15:18:02 -04:00
|
|
|
|
|
|
|
class CommentWithDefaultScopeReferencesAssociation < Comment
|
2016-08-16 03:30:11 -04:00
|
|
|
default_scope -> { includes(:developer).order("developers.name").references(:developer) }
|
2014-08-31 15:18:02 -04:00
|
|
|
belongs_to :developer
|
|
|
|
end
|
2017-07-12 10:22:20 -04:00
|
|
|
|
|
|
|
class CommentWithAfterCreateUpdate < Comment
|
|
|
|
after_create do
|
|
|
|
update_attributes(body: "bar")
|
|
|
|
end
|
|
|
|
end
|