2017-07-09 17:41:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 16:26:20 +00:00
|
|
|
require "models/topic"
|
2005-11-04 05:46:28 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
class Reply < Topic
|
2016-08-06 17:37:57 +00:00
|
|
|
belongs_to :topic, foreign_key: "parent_id", counter_cache: true
|
2018-05-27 00:28:27 +00:00
|
|
|
belongs_to :topic_with_primary_key, class_name: "Topic", primary_key: "title", foreign_key: "parent_title", counter_cache: "replies_count", touch: true
|
2016-08-06 17:37:57 +00:00
|
|
|
has_many :replies, class_name: "SillyReply", dependent: :destroy, foreign_key: "parent_id"
|
2018-06-06 19:28:23 +00:00
|
|
|
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
|
2019-02-13 19:34:00 +00:00
|
|
|
|
|
|
|
scope :ordered, -> { Reply.order(:id) }
|
2020-05-09 21:14:29 +00:00
|
|
|
|
2020-06-01 13:07:34 +00:00
|
|
|
alias_attribute :new_content, :content
|
|
|
|
alias_attribute :new_parent_id, :parent_id
|
|
|
|
|
2020-05-12 18:10:39 +00:00
|
|
|
# Method on Kernel
|
2020-05-09 21:14:29 +00:00
|
|
|
def self.open
|
|
|
|
approved
|
|
|
|
end
|
2020-05-12 18:10:39 +00:00
|
|
|
|
|
|
|
# Methods both on Kernel and Relation
|
|
|
|
def self.load(data:); end
|
|
|
|
def self.select(data:); end
|
2010-01-01 02:20:38 +00:00
|
|
|
end
|
|
|
|
|
2018-09-18 16:11:07 +00:00
|
|
|
class SillyReply < Topic
|
|
|
|
belongs_to :reply, foreign_key: "parent_id", counter_cache: :replies_count
|
|
|
|
end
|
|
|
|
|
2011-01-09 17:13:05 +00:00
|
|
|
class UniqueReply < Reply
|
2016-08-06 17:37:57 +00:00
|
|
|
belongs_to :topic, foreign_key: "parent_id", counter_cache: true
|
|
|
|
validates_uniqueness_of :content, scope: "parent_id"
|
2011-01-09 17:13:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class SillyUniqueReply < UniqueReply
|
2018-05-22 13:34:24 +00:00
|
|
|
validates :content, uniqueness: true
|
|
|
|
end
|
|
|
|
|
2010-01-01 02:20:38 +00:00
|
|
|
class WrongReply < Reply
|
2004-12-09 12:50:18 +00:00
|
|
|
validate :errors_on_empty_content
|
2016-08-06 17:37:57 +00:00
|
|
|
validate :title_is_wrong_create, on: :create
|
2008-01-18 07:27:03 +00:00
|
|
|
|
2009-03-21 19:05:09 +00:00
|
|
|
validate :check_empty_title
|
2016-08-06 17:37:57 +00:00
|
|
|
validate :check_content_mismatch, on: :create
|
|
|
|
validate :check_wrong_update, on: :update
|
|
|
|
validate :check_author_name_is_secret, on: :special_case
|
2009-03-21 19:05:09 +00:00
|
|
|
|
|
|
|
def check_empty_title
|
2018-03-31 14:42:40 +00:00
|
|
|
errors.add(:title, "Empty") unless attribute_present?("title")
|
2004-12-09 12:50:18 +00:00
|
|
|
end
|
2008-01-18 07:27:03 +00:00
|
|
|
|
2004-12-09 12:50:18 +00:00
|
|
|
def errors_on_empty_content
|
2018-03-31 14:42:40 +00:00
|
|
|
errors.add(:content, "Empty") unless attribute_present?("content")
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:27:03 +00:00
|
|
|
|
2009-03-21 19:05:09 +00:00
|
|
|
def check_content_mismatch
|
2004-11-24 01:04:44 +00:00
|
|
|
if attribute_present?("title") && attribute_present?("content") && content == "Mismatch"
|
2018-03-31 14:42:40 +00:00
|
|
|
errors.add(:title, "is Content Mismatch")
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-12-09 12:50:18 +00:00
|
|
|
def title_is_wrong_create
|
2018-03-31 14:42:40 +00:00
|
|
|
errors.add(:title, "is Wrong Create") if attribute_present?("title") && title == "Wrong Create"
|
2004-12-09 12:50:18 +00:00
|
|
|
end
|
|
|
|
|
2009-03-21 19:05:09 +00:00
|
|
|
def check_wrong_update
|
2018-03-31 14:42:40 +00:00
|
|
|
errors.add(:title, "is Wrong Update") if attribute_present?("title") && title == "Wrong Update"
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2010-05-10 09:28:38 +00:00
|
|
|
|
2010-05-21 14:20:56 +00:00
|
|
|
def check_author_name_is_secret
|
2018-03-31 14:42:40 +00:00
|
|
|
errors.add(:author_name, "Invalid") unless author_name == "secret"
|
2010-05-10 09:28:38 +00:00
|
|
|
end
|
2004-12-12 18:12:57 +00:00
|
|
|
end
|
|
|
|
|
2008-12-31 09:43:13 +00:00
|
|
|
module Web
|
|
|
|
class Reply < Web::Topic
|
2016-08-06 17:37:57 +00:00
|
|
|
belongs_to :topic, foreign_key: "parent_id", counter_cache: true, class_name: "Web::Topic"
|
2008-12-31 09:43:13 +00:00
|
|
|
end
|
2009-09-08 15:10:14 +00:00
|
|
|
end
|