appeal.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: appeals
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8) not null
  8. # account_warning_id :bigint(8) not null
  9. # text :text default(""), not null
  10. # approved_at :datetime
  11. # approved_by_account_id :bigint(8)
  12. # rejected_at :datetime
  13. # rejected_by_account_id :bigint(8)
  14. # created_at :datetime not null
  15. # updated_at :datetime not null
  16. #
  17. class Appeal < ApplicationRecord
  18. MAX_STRIKE_AGE = 20.days
  19. TEXT_LENGTH_LIMIT = 2_000
  20. belongs_to :account
  21. belongs_to :strike, class_name: 'AccountWarning', foreign_key: 'account_warning_id', inverse_of: :appeal
  22. with_options class_name: 'Account', optional: true do
  23. belongs_to :approved_by_account
  24. belongs_to :rejected_by_account
  25. end
  26. validates :text, presence: true, length: { maximum: TEXT_LENGTH_LIMIT }
  27. validates :account_warning_id, uniqueness: true
  28. validate :validate_time_frame, on: :create
  29. scope :approved, -> { where.not(approved_at: nil) }
  30. scope :rejected, -> { where.not(rejected_at: nil) }
  31. scope :pending, -> { where(approved_at: nil, rejected_at: nil) }
  32. def pending?
  33. !approved? && !rejected?
  34. end
  35. def approved?
  36. approved_at.present?
  37. end
  38. def rejected?
  39. rejected_at.present?
  40. end
  41. def approve!(current_account)
  42. update!(approved_at: Time.now.utc, approved_by_account: current_account)
  43. end
  44. def reject!(current_account)
  45. update!(rejected_at: Time.now.utc, rejected_by_account: current_account)
  46. end
  47. def to_log_human_identifier
  48. account.acct
  49. end
  50. def to_log_route_param
  51. account_warning_id
  52. end
  53. private
  54. def validate_time_frame
  55. errors.add(:base, I18n.t('strikes.errors.too_late')) if strike.created_at < MAX_STRIKE_AGE.ago
  56. end
  57. end