module CensorBear class ModLog < ApplicationRecord include AASM REASONS = %w(无 广告/SPAM 恶意灌水 违规内容 文不对题 重复发布 其它).freeze belongs_to :record, polymorphic: true, required: false def record_path return '#' if record.blank? record.censor_show_path end def remove(reason) return if record.blank? return if record.discarded? # ugc内容需要开软删,便于撤销&查看统计,可以定时删除统计并删除软删内容,类似回收站机制 raise NoMethodError, 'undefined censor_remove for record' unless record.respond_to?(:censor_remove) ret = record.censor_remove reject!(reason) if ret end def approve return if record.blank? raise NoMethodError unless record.respond_to?(:censor_approve) ret = record.censor_approve pass! if ret end def cancel return if record.blank? raise NoMethodError unless record.respond_to?(:censor_cancel) ret = record.censor_cancel undo! if ret end def ban_user return if record.blank? raise NoMethodError unless record.respond_to?(:censor_ban_user) ret = record.censor_ban_user reject!('已被封禁该用户') if ret end aasm do state :pending, initial: true state :rejected, :passed, :suspended event :reject do transitions from: %i[pending suspended], to: :rejected, after: Proc.new {|*args| set_reason(*args) } end event :pass do transitions from: %i[pending suspended], to: :passed end event :suspend do transitions from: :pending, to: :suspended, after: :unset_reason end event :undo do transitions from: %i[passed rejected], to: :pending, after: :unset_reason end end def unset_reason update_columns(reason: nil) end def set_reason(reason) update_columns(reason: reason) end end end