about summary refs log tree commit diff
path: root/app/models/appeal.rb
blob: 6fbf60b39e172b87c063591fe4565d44e38767b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

# == Schema Information
#
# Table name: appeals
#
#  id                     :bigint(8)        not null, primary key
#  account_id             :bigint(8)        not null
#  account_warning_id     :bigint(8)        not null
#  text                   :text             default(""), not null
#  approved_at            :datetime
#  approved_by_account_id :bigint(8)
#  rejected_at            :datetime
#  rejected_by_account_id :bigint(8)
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#
class Appeal < ApplicationRecord
  MAX_STRIKE_AGE = 20.days

  belongs_to :account
  belongs_to :strike, class_name: 'AccountWarning', foreign_key: 'account_warning_id'
  belongs_to :approved_by_account, class_name: 'Account', optional: true
  belongs_to :rejected_by_account, class_name: 'Account', optional: true

  validates :text, presence: true, length: { maximum: 2_000 }
  validates :account_warning_id, uniqueness: true

  validate :validate_time_frame, on: :create

  scope :approved, -> { where.not(approved_at: nil) }
  scope :rejected, -> { where.not(rejected_at: nil) }
  scope :pending, -> { where(approved_at: nil, rejected_at: nil) }

  def pending?
    !approved? && !rejected?
  end

  def approved?
    approved_at.present?
  end

  def rejected?
    rejected_at.present?
  end

  def approve!(current_account)
    update!(approved_at: Time.now.utc, approved_by_account: current_account)
  end

  def reject!(current_account)
    update!(rejected_at: Time.now.utc, rejected_by_account: current_account)
  end

  def to_log_human_identifier
    account.acct
  end

  def to_log_route_param
    account_warning_id
  end

  private

  def validate_time_frame
    errors.add(:base, I18n.t('strikes.errors.too_late')) if strike.created_at < MAX_STRIKE_AGE.ago
  end
end