module BeetleReporter class Report < ApplicationRecord include AASM include Discard::Model REASON_TYPES = %w(垃圾广告营销 侮辱谩骂内容 淫秽色情内容 涉政敏感信息 违法有害信息 内容令人不适 内容存在错误) belongs_to :record, polymorphic: true, required: false belongs_to :user, class_name: BeetleReporter.config.user_class, optional: true aasm do state :pending, initial: true state :ignored, :hidden, :removed, :muted, :forbiden, :suspended event :ignore do transitions from: :pending, to: :ignored end event :hide do transitions from: %i[pending ignored], to: :hidden, after: Proc.new {|*args| set_reason(*args) } end event :remove do transitions from: %i[pending ignored], to: :removed, after: Proc.new {|*args| set_reason(*args) } end event :mute do transitions from: %i[pending ignored], to: :muted, after: Proc.new {|*args| set_reason(*args) } end event :forbid do transitions from: %i[pending muted], to: :forbiden, after: Proc.new {|*args| set_reason(*args) } end event :suspend do transitions from: :pending, to: :suspended end event :undo do transitions from: %i[ignored hidden removed muted forbiden suspended], to: :pending, after: :undo_callback end end def beetle_hide(reason) return if record.blank? raise NoMethodError unless record.respond_to?(:beetle_hide) ret = record.beetle_hide hide!(reason) if ret end def beetle_remove(reason) return if record.blank? raise NoMethodError unless record.respond_to?(:beetle_remove) ret = record.beetle_remove remove!(reason) if ret end def beetle_mute(reason) return if record.blank? return if record&.user.blank? raise NoMethodError unless record.respond_to?(:beetle_mute) ret = record.beetle_mute mute!(reason) if ret end def beetle_mute(reason) return if record.blank? return if record&.user.blank? raise NoMethodError unless record.respond_to?(:beetle_mute) ret = record.beetle_mute mute!(reason) if ret end def beetle_forbid(reason) return if record.blank? return if record&.user.blank? raise NoMethodError unless record.respond_to?(:beetle_forbid) ret = record.beetle_forbid forbid!(reason) if ret end def record_path return '#' if record.blank? record.beetle_record_path end def record_partial_path record.beetle_record_partial_path end def undo_callback if hidden? ret = record.beetle_undo_hide update_columns(reason: nil) if ret elsif removed? ret = record.beetle_undo_remove update_columns(reason: nil) if ret elsif muted? ret = record.beetle_undo_mute update_columns(reason: nil) if ret elsif forbiden? ret = record.beetle_undo_forbid update_columns(reason: nil) if ret else update_columns(reason: nil) end end def set_reason(reason) update_columns(reason: reason) end def self.record_type_options select(:record_type).where.not(record_type: nil).distinct.map{|t| [t.record_type, t.record_type.downcase]} end end end