Skip to content

Commit

Permalink
Add more date range selection options #4518 (#4533)
Browse files Browse the repository at this point in the history
* changes to add "last 12 months" and "prior year"

* Update spec for "Prior Year" and "Last 12 Months"

* fix failing test and rubocop error

* Fix filter by prior year and last 12 months specs

---------

Co-authored-by: Cory Streiff <c.streiff@pm.me>
  • Loading branch information
pshong79 and coalest authored Nov 30, 2024
1 parent 6322688 commit a01e964
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/helpers/date_range_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def date_range_label
"this month"
when "last month"
"last month"
when "last 12 months"
"last 12 months"
when "prior year"
"prior year"
else
selected_range_described
end
Expand Down
4 changes: 3 additions & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ $(document).ready(function(){
'This Month': [today.startOf('month').toJSDate(), today.endOf('month').toJSDate()],
'Last Month': [today.minus({'months': 1}).startOf('month').toJSDate(),
today.minus({'month': 1}).endOf('month').toJSDate()],
'This Year': [today.startOf('year').toJSDate(), today.endOf('year').toJSDate()]
'Last 12 Months': [today.minus({'months': 12}).plus({'days': 1}).toJSDate(), today.toJSDate()],
'Prior Year': [today.startOf('year').minus({'years': 1}).toJSDate(), today.minus({'year': 1}).endOf('year').toJSDate()],
'This Year': [today.startOf('year').toJSDate(), today.endOf('year').toJSDate()],
}
}
});
Expand Down
48 changes: 46 additions & 2 deletions spec/system/distributions_by_county_system_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
RSpec.feature "Distributions by County", type: :system do
include_examples "distribution_by_county"

let(:year) { Time.current.year }
let(:issued_at_last_year) { Time.current.utc.change(year: year - 1).to_datetime }
let(:current_year) { Time.current.year }
let(:issued_at_last_year) { Time.current.utc.change(year: current_year - 1).to_datetime }

before do
sign_in(user)
Expand Down Expand Up @@ -34,6 +34,50 @@
expect(page).to have_text("25", count: 4)
expect(page).to have_text("$262.50", count: 4)
end

it("works for prior year") do
# Should NOT return distribution issued before previous calendar year
last_day_of_two_years_ago = Time.current.utc.change(year: current_year - 2, month: 12, day: 31).to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: last_day_of_two_years_ago)

# Should return distribution issued during previous calendar year
one_year_ago = issued_at_last_year
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: one_year_ago)

# Should NOT return distribution issued after previous calendar year
first_day_of_current_year = Time.current.utc.change(year: current_year, month: 1, day: 1).to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: first_day_of_current_year)

visit_distribution_by_county_with_specified_date_range("Prior Year")

partner_1.profile.served_areas.each do |served_area|
expect(page).to have_text(served_area.county.name)
end
expect(page).to have_text("25", count: 4)
expect(page).to have_text("$262.50", count: 4)
end

it("works for last 12 months") do
# Should NOT return disitribution issued before 12 months ago
one_year_and_one_day_ago = 1.year.ago.prev_day.to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: one_year_and_one_day_ago)

# Should return distribution issued during previous 12 months
today = issued_at_present
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: today)

# Should NOT return distribution issued in the future
tomorrow = 1.day.from_now.to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: tomorrow)

visit_distribution_by_county_with_specified_date_range("Last 12 Months")

partner_1.profile.served_areas.each do |served_area|
expect(page).to have_text(served_area.county.name)
end
expect(page).to have_text("25", count: 4)
expect(page).to have_text("$262.50", count: 4)
end
end

def visit_distribution_by_county_with_specified_date_range(date_range_string)
Expand Down

0 comments on commit a01e964

Please sign in to comment.