appeal.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. belongs_to :account
  20. belongs_to :strike, class_name: 'AccountWarning', foreign_key: 'account_warning_id', inverse_of: :appeal
  21. belongs_to :approved_by_account, class_name: 'Account', optional: true
  22. belongs_to :rejected_by_account, class_name: 'Account', optional: true
  23. validates :text, presence: true, length: { maximum: 2_000 }
  24. validates :account_warning_id, uniqueness: true
  25. validate :validate_time_frame, on: :create
  26. scope :approved, -> { where.not(approved_at: nil) }
  27. scope :rejected, -> { where.not(rejected_at: nil) }
  28. scope :pending, -> { where(approved_at: nil, rejected_at: nil) }
  29. def pending?
  30. !approved? && !rejected?
  31. end
  32. def approved?
  33. approved_at.present?
  34. end
  35. def rejected?
  36. rejected_at.present?
  37. end
  38. def approve!(current_account)
  39. update!(approved_at: Time.now.utc, approved_by_account: current_account)
  40. end
  41. def reject!(current_account)
  42. update!(rejected_at: Time.now.utc, rejected_by_account: current_account)
  43. end
  44. def to_log_human_identifier
  45. account.acct
  46. end
  47. def to_log_route_param
  48. account_warning_id
  49. end
  50. private
  51. def validate_time_frame
  52. errors.add(:base, I18n.t('strikes.errors.too_late')) if strike.created_at < MAX_STRIKE_AGE.ago
  53. end
  54. end