Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate declaration_date only when its changed #2112

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 28 additions & 7 deletions spec/models/declaration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,38 @@
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:) }

it { is_expected.to be_valid }
subject { build(:declaration, course:, user:, application:, declaration_date:).tap { |d| d.save!(validate: false) } }

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 not valid" do
subject.declaration_date = new_declaration_date
expect(subject).not_to be_valid
end
end
end
end

Expand Down
Loading