Skip to content

Commit bd8227b

Browse files
Resolves #4675 Add year to month labels on trend reports (#4793)
* Updated last_12_months test to check for year being included with month * Updated last_12_months() to add appropriate year labels to month categories * Added comment clarifying logic of loop
1 parent 6fa8d65 commit bd8227b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

app/helpers/historical_trends_helper.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ module HistoricalTrendsHelper
1616

1717
def last_12_months
1818
current_month = Time.zone.now.month
19-
MONTHS.rotate(current_month)
19+
current_year = Time.zone.now.year
20+
last_year = current_year - 1
21+
return_array = MONTHS.rotate(current_month)
22+
return_array.each_with_index do |month, index|
23+
# Last current_month entries are in the current year, earlier entries are
24+
# in the previous year.
25+
return_array[index] = if index >= (MONTHS.length - current_month)
26+
"#{month} #{current_year}"
27+
else
28+
"#{month} #{last_year}"
29+
end
30+
end
31+
return_array
2032
end
2133
end

spec/helpers/historical_trends_helper_spec.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
RSpec.describe HistoricalTrendsHelper do
44
describe "#last_12_months" do
5-
it "returns the last 12 months starting from July when the current month is June" do
5+
it "returns the last 12 months starting from July 2023 when the current month is June 2024" do
66
allow_any_instance_of(Time).to receive(:month).and_return(6)
7+
allow_any_instance_of(Time).to receive(:year).and_return(2024)
78

8-
expect(last_12_months).to eq(["Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun"])
9+
expect(last_12_months).to eq(["Jul 2023", "Aug 2023", "Sep 2023", "Oct 2023", "Nov 2023", "Dec 2023", "Jan 2024", "Feb 2024", "Mar 2024", "Apr 2024", "May 2024", "Jun 2024"])
910
end
1011

11-
it "returns the last 12 months starting from Feb when the current month is Jan" do
12+
it "returns the last 12 months starting from Feb 1999 when the current month is Jan 2000" do
1213
allow_any_instance_of(Time).to receive(:month).and_return(1)
14+
allow_any_instance_of(Time).to receive(:year).and_return(2000)
1315

14-
expect(last_12_months).to eq(["Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan"])
16+
expect(last_12_months).to eq(["Feb 1999", "Mar 1999", "Apr 1999", "May 1999", "Jun 1999", "Jul 1999", "Aug 1999", "Sep 1999", "Oct 1999", "Nov 1999", "Dec 1999", "Jan 2000"])
1517
end
1618

17-
it "returns the last 12 months starting from Jan when the current month is Dec" do
19+
it "returns the last 12 months starting from Jan 2010 when the current month is Dec 2010" do
1820
allow_any_instance_of(Time).to receive(:month).and_return(12)
21+
allow_any_instance_of(Time).to receive(:year).and_return(2010)
1922

20-
expect(last_12_months).to eq(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
23+
expect(last_12_months).to eq(["Jan 2010", "Feb 2010", "Mar 2010", "Apr 2010", "May 2010", "Jun 2010", "Jul 2010", "Aug 2010", "Sep 2010", "Oct 2010", "Nov 2010", "Dec 2010"])
2124
end
2225
end
2326
end

0 commit comments

Comments
 (0)