Skip to content

Commit

Permalink
use Date#cwyear when comparing years
Browse files Browse the repository at this point in the history
  • Loading branch information
fermion committed Dec 30, 2024
1 parent 46fa738 commit 88155ea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions app/models/work_week.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class WorkWeek < ApplicationRecord
validate :no_future_actual_hours

def is_future_work_week?
Date.today.year < year || (
year == Date.today.year && cweek > Date.today.cweek
Date.today.cwyear < year || (
year == Date.today.cwyear && cweek > Date.today.cweek
)
end

Expand All @@ -39,6 +39,6 @@ def actual_hours_allowed?
return false if year_zero? || cweek_zero?

today = Date.today
today.year > year || (today.year == year && today.cweek >= cweek)
today.cwyear > year || (today.cwyear == year && today.cweek >= cweek)
end
end
2 changes: 1 addition & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
factory :work_week do
assignment
cweek { Date.today.cweek }
year { Date.today.year }
year { Date.today.cwyear }
estimated_hours { rand(2..8) }
actual_hours { rand(2..8) }

Expand Down
18 changes: 10 additions & 8 deletions spec/graphql/mutations/upsert_work_week_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
}.to raise_error(ActiveRecord::RecordNotFound)
end

it "does not delete a future work week when passed a null or 0 value for estimatedHours" do
it "does not delete a past work week when passed a null or 0 value for estimatedHours if there are actualHours" do
query_string = <<-GRAPHQL
mutation($assignmentId: ID!, $cweek: Int!, $year: Int!, $actualHours: Int, $estimatedHours: Int) {
upsertWorkWeek(assignmentId: $assignmentId, cweek: $cweek, year: $year, actualHours: $actualHours, estimatedHours: $estimatedHours) {
Expand All @@ -137,9 +137,11 @@
company = create(:company)
assignment = tbd_assignment_for_company(company:)
user = company.users.first

past_date = Date.today - 2.months
work_week = create(:work_week,
cweek: (Date.today - 1.month).cweek,
year: (Date.today - 1.month).year,
cweek: past_date.cweek,
year: past_date.year,
estimated_hours: 10,
actual_hours: 10,
assignment:
Expand All @@ -155,13 +157,13 @@
assignmentId: work_week.assignment_id,
cweek: work_week.cweek,
year: work_week.year,
actualHours: nil,
estimatedHours: nil
estimatedHours: nil,
actualHours: 10
}
)

post_result = result["data"]["upsertWorkWeek"]
expect(post_result["actualHours"]).to eq(0)
expect(post_result["actualHours"]).to eq(10)
expect(post_result["estimatedHours"]).to eq(0)
expect {
work_week.reload
Expand Down Expand Up @@ -297,14 +299,14 @@
variables: {
assignmentId: assignment.id,
cweek: today.cweek,
year: today.year,
year: today.cwyear,
estimatedHours: 15
}
)

post_result = result["data"]["upsertWorkWeek"]
expect(post_result["cweek"]).to eq(today.cweek)
expect(post_result["year"]).to eq(today.year)
expect(post_result["year"]).to eq(today.cwyear)
expect(post_result["actualHours"]).to eq(0)
expect(post_result["estimatedHours"]).to eq(15)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/work_week_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

context "#is_future_work_week?" do
it "returns true if the work week is in the future" do
work_week = build(:work_week, year: Date.today.year, cweek: Date.today.cweek + 1)
work_week = build(:work_week, year: Date.today.cwyear, cweek: Date.today.cweek + 1)
expect(work_week.is_future_work_week?).to be_truthy
end

it "returns false if the work week is in the past" do
work_week = build(:work_week, year: Date.today.year, cweek: Date.today.cweek - 1)
work_week = build(:work_week, year: Date.today.cwyear, cweek: Date.today.cweek - 1)
expect(work_week.is_future_work_week?).to be_falsey
end
end
Expand Down

0 comments on commit 88155ea

Please sign in to comment.