Skip to content

Commit

Permalink
Reminder note should not be more than 80 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiru committed Nov 22, 2024
1 parent ae3f77d commit 9627c5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/contracts/reminders/base_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

module Reminders
class BaseContract < ::ModelContract
MAX_NOTE_CHARS_LENGTH = 80

attribute :creator_id
attribute :remindable_id
attribute :remindable_type
Expand All @@ -39,6 +41,7 @@ class BaseContract < ::ModelContract
validate :validate_remindable_exists
validate :validate_manage_reminders_permissions
validate :validate_remind_at_is_in_future
validate :validate_note_length

def self.model = Reminder

Expand All @@ -62,6 +65,12 @@ def validate_remind_at_is_in_future
end
end

def validate_note_length
if model.note.present? && model.note.length > MAX_NOTE_CHARS_LENGTH
errors.add :note, :too_long, count: MAX_NOTE_CHARS_LENGTH
end
end

def validate_manage_reminders_permissions
return if errors.added?(:remindable, :not_found)

Expand Down
14 changes: 14 additions & 0 deletions spec/contracts/reminders/base_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,19 @@
end
end

describe "validate note length" do
context "when note is too long" do
before { reminder.note = "a" * (described_class::MAX_NOTE_CHARS_LENGTH + 1) }

it_behaves_like "contract is invalid", note: :too_long
end

context "when note is within the limit" do
before { reminder.note = "a" * described_class::MAX_NOTE_CHARS_LENGTH }

it_behaves_like "contract is valid"
end
end

include_examples "contract reuses the model errors"
end

0 comments on commit 9627c5b

Please sign in to comment.