Skip to content

Commit

Permalink
pfmp: fix the day count validation logic
Browse files Browse the repository at this point in the history
A full week means 5 days which wasn't allowed before.
  • Loading branch information
freesteph committed Apr 5, 2024
1 parent dc94cc6 commit d761dcb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Pfmp < ApplicationRecord
only_integer: true,
allow_nil: true,
greater_than: 0,
less_than_or_equal_to: ->(pfmp) { (pfmp.end_date - pfmp.start_date).to_i }
less_than_or_equal_to: ->(pfmp) { (pfmp.end_date - pfmp.start_date).to_i + 1 }
}

scope :finished, -> { where("pfmps.end_date <= (?)", Time.zone.today) }
Expand Down
12 changes: 10 additions & 2 deletions spec/models/pfmp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@
end

describe "day count" do
subject(:pfmp) { build(:pfmp, start_date: Time.zone.now, end_date: 7.days.from_now) }
subject(:pfmp) do
start = Time.zone.now.beginning_of_week

build(
:pfmp,
start_date: start,
end_date: start.end_of_week(:saturday),
)
end

context "when the number of days doesn't fit in the date range" do
before { pfmp.day_count = 8 }
Expand All @@ -62,7 +70,7 @@
end

context "when the number fits exactly in the day range" do
before { pfmp.day_count = 7 }
before { pfmp.day_count = 5 }

it { is_expected.to be_valid }
end
Expand Down

0 comments on commit d761dcb

Please sign in to comment.