# frozen_string_literal: true require 'fancy_searchable/searchable' # == Schema Information # # Table name: comments # # id :integer not null, primary key # anonymous :boolean default(FALSE) # body :string not null # deletion_reason :string default(""), not null # destroyed_content :boolean default(FALSE) # edit_reason :string # edited_at :datetime # hidden_from_users :boolean default(FALSE), not null # ip :inet # name_at_post_time :string # referrer :string default("") # show_staff_tag :boolean default(FALSE), not null # tripcode :text # user_agent :string default("") # created_at :datetime not null # updated_at :datetime not null # deleted_by_id :integer # post_id :integer # user_id :integer # # Indexes # # index_comments_on_created_at (created_at) # index_comments_on_deleted_by_id (deleted_by_id) WHERE (deleted_by_id IS NOT NULL) # index_comments_on_post_id (post_id) # index_comments_on_user_id (user_id) # # Foreign Keys # # fk_rails_... (deleted_by_id => users.id) ON DELETE => nullify ON UPDATE => cascade # fk_rails_... (post_id => posts.id) # fk_rails_... (user_id => users.id) ON DELETE => nullify ON UPDATE => cascade # class Comment < ApplicationRecord include Hidable include Reportable include FancySearchable::Searchable include Indexable include AnonUserAttributable include ModLoggable resourcify belongs_to :post belongs_to :deleted_by, class_name: 'User', optional: true validates_with CommentValidator validates_with DestroyOnlyIfHiddenValidator validates :body, presence: { message: "can't be blank.", unless: :destroyed_content } validates :edit_reason, length: { maximum: 70, message: 'too long (70 character max).' } validates :post, presence: true has_paper_trail on: [:update, :destroy] audited only: [:body, :edit_reason, :deletion_reason, :hidden_from_users], if: Proc.new { |comment| User.audit_current_user&.staff? && comment.user != User.audit_current_user } after_create do # Update image comment count. Post.increment_counter(:comments_count, post_id) post.update(updated_at: Time.zone.now) end after_commit(on: :create) do # Send notifications. Notification.async_notify(post, 'commented on', self) end after_commit :update_index def link_to_route "/#{post.id}#comment_#{id}" end def for_route [post, self] end delegate :id, :title, to: :parent, prefix: true alias parent post end