module CensorBear class ReviewLog < ApplicationRecord include AASM belongs_to :record, polymorphic: true, optional: true belongs_to :user, class_name: CensorBear.config.user_class, foreign_key: "user_id", optional: true belongs_to :operator, class_name: CensorBear.config.user_class, foreign_key: "operator_id", optional: true delegate *CensorBear::RecordHook.public_instance_methods, to: :record, allow_nil: true aasm timestamps: true do state :pending, initial: true state :rejected, :passed, :suspended, :removed event :reject do transitions from: %i[pending suspended], to: :rejected, after: Proc.new { |*args| censor_reject; update!(operator: args[0]) } end event :undo_reject do transitions from: %i[rejected], to: :pending, after: Proc.new { |*args| censor_undo_reject; update!(operator: args[0]) } end event :remove do transitions from: %i[pending suspended], to: :removed, after: Proc.new { |*args| censor_remove; update!(operator: args[0]) } end event :undo_remove do transitions from: %i[removed], to: :pending, after: Proc.new { |*args| censor_undo_remove; update!(operator: args[0]) } end event :approve do transitions from: %i[pending suspended], to: :passed, after: Proc.new { |*args| censor_approve; update!(operator: args[0]) } end event :undo_approve do transitions from: %i[passed rejected], to: :pending, after: Proc.new { |*args| censor_undo_approve; update!(operator: args[0]) } end end before_save :sync_operated_time def ban_user(_operator = nil) censor_ban_user end def unset_reason update_columns(reason: nil) end def set_reason(reason) update_columns(reason: reason) end private def sync_operated_time self.operated_at = Time.current if aasm_state_changed? end end end