From 9bf2e2eda0ffea6382e161f7ebb43d73aaf658ca Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 10 Jun 2024 11:23:17 -0400 Subject: [PATCH] Extract `TEXT_LENGTH_LIMIT` constant in `Appeal` class (#30638) --- app/models/appeal.rb | 4 +++- spec/models/appeal_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/appeal.rb b/app/models/appeal.rb index 395056b76f5..fafa75e69d8 100644 --- a/app/models/appeal.rb +++ b/app/models/appeal.rb @@ -18,6 +18,8 @@ class Appeal < ApplicationRecord MAX_STRIKE_AGE = 20.days + TEXT_LENGTH_LIMIT = 2_000 + belongs_to :account belongs_to :strike, class_name: 'AccountWarning', foreign_key: 'account_warning_id', inverse_of: :appeal @@ -26,7 +28,7 @@ class Appeal < ApplicationRecord belongs_to :rejected_by_account end - validates :text, presence: true, length: { maximum: 2_000 } + validates :text, presence: true, length: { maximum: TEXT_LENGTH_LIMIT } validates :account_warning_id, uniqueness: true validate :validate_time_frame, on: :create diff --git a/spec/models/appeal_spec.rb b/spec/models/appeal_spec.rb index 12373a9494e..13ca3a2d901 100644 --- a/spec/models/appeal_spec.rb +++ b/spec/models/appeal_spec.rb @@ -3,6 +3,19 @@ require 'rails_helper' describe Appeal do + describe 'Validations' do + it 'validates text length is under limit' do + appeal = Fabricate.build( + :appeal, + strike: Fabricate(:account_warning), + text: 'a' * described_class::TEXT_LENGTH_LIMIT * 2 + ) + + expect(appeal).to_not be_valid + expect(appeal).to model_have_error_on_field(:text) + end + end + describe 'scopes' do describe 'approved' do let(:approved_appeal) { Fabricate(:appeal, approved_at: 10.days.ago) }