Skip to content

Commit

Permalink
Validate declaration_date only when its changed
Browse files Browse the repository at this point in the history
  • Loading branch information
slawosz committed Jan 9, 2025
1 parent a5d2f2a commit 3c59571
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
5 changes: 2 additions & 3 deletions app/models/declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ class Declaration < ApplicationRecord
duplicate: "duplicate",
}, _suffix: true

attr_accessor :skip_declaration_date_within_schedule_validation

validates :declaration_date, :declaration_type, presence: true
validate :validate_declaration_date_within_schedule, if: -> { !skip_declaration_date_within_schedule_validation }
validate :validate_declaration_date_within_schedule
validate :validate_declaration_date_not_in_the_future
validates :ecf_id, uniqueness: { case_sensitive: false }
validate :validate_max_statement_items_count
Expand Down Expand Up @@ -150,6 +148,7 @@ def duplicate_declarations
def validate_declaration_date_within_schedule
return unless application&.schedule
return unless declaration_date
return if persisted? && !declaration_date_changed?

if declaration_date < application.schedule.applies_from
errors.add(:declaration_date, :declaration_before_schedule_start)
Expand Down
39 changes: 32 additions & 7 deletions spec/models/declaration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,42 @@
end

context "when declaration_date is before the schedule start" do
before { subject.declaration_date = subject.application.schedule.applies_from.prev_week }
let(:declaration_date) { application.schedule.applies_from.prev_week }

it "has a meaningful error" do
expect(subject).to be_invalid
expect(subject).to have_error(:declaration_date, :declaration_before_schedule_start, "Enter a '#/declaration_date' that's on or after the schedule start.")
context "when declaration is being created" do
before { subject.declaration_date = subject.application.schedule.applies_from.prev_week }

it "has a meaningful error" do
expect(subject).to be_invalid
expect(subject).to have_error(:declaration_date, :declaration_before_schedule_start, "Enter a '#/declaration_date' that's on or after the schedule start.")
end
end

context "when `skip_declaration_date_within_schedule_validation` is set to true" do
before { subject.skip_declaration_date_within_schedule_validation = true }
context "when declaration already exists" do
let(:user) { create(:user) }
let(:course) { create(:course) }

let(:application) { create(:application, :accepted, user:, course:) }

subject do
d = build(:declaration, course:, user:, application:, declaration_date:)
d.save!(validate: false)
d
end

context "when declaration_date is not changed" do
it { is_expected.to be_valid }
end

context "when declaration_date is going to be changed" do
let(:declaration_date) { application.schedule.applies_from.next_week }
let(:new_declaration_date) { application.schedule.applies_from.prev_week }

it { is_expected.to be_valid }
it "is not valid" do
subject.declaration_date = new_declaration_date
expect(subject).not_to be_valid
end
end
end
end

Expand Down

0 comments on commit 3c59571

Please sign in to comment.