2022-02-14 20:27:53 +00:00
|
|
|
# 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
|
2022-02-16 21:29:48 +00:00
|
|
|
MAX_STRIKE_AGE = 20.days
|
|
|
|
|
2024-06-10 15:23:17 +00:00
|
|
|
TEXT_LENGTH_LIMIT = 2_000
|
|
|
|
|
2022-02-14 20:27:53 +00:00
|
|
|
belongs_to :account
|
2023-04-30 12:06:53 +00:00
|
|
|
belongs_to :strike, class_name: 'AccountWarning', foreign_key: 'account_warning_id', inverse_of: :appeal
|
2024-01-18 12:29:41 +00:00
|
|
|
|
|
|
|
with_options class_name: 'Account', optional: true do
|
|
|
|
belongs_to :approved_by_account
|
|
|
|
belongs_to :rejected_by_account
|
|
|
|
end
|
2022-02-14 20:27:53 +00:00
|
|
|
|
2024-06-10 15:23:17 +00:00
|
|
|
validates :text, presence: true, length: { maximum: TEXT_LENGTH_LIMIT }
|
2022-02-14 20:27:53 +00:00
|
|
|
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
|
|
|
|
|
2022-08-25 18:39:40 +00:00
|
|
|
def to_log_human_identifier
|
|
|
|
account.acct
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_log_route_param
|
|
|
|
account_warning_id
|
|
|
|
end
|
|
|
|
|
2022-02-14 20:27:53 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_time_frame
|
2022-02-16 21:29:48 +00:00
|
|
|
errors.add(:base, I18n.t('strikes.errors.too_late')) if strike.created_at < MAX_STRIKE_AGE.ago
|
2022-02-14 20:27:53 +00:00
|
|
|
end
|
|
|
|
end
|