The value of cweek did not manage well the last week of the year

grouping_key_for method when params[:group] == "week" always returned "#{date.cweek}/#{date.year}" but that not always is true.
When a date belongs to the first_week/last_week of the year we have exceptions for example "31/12/2018".  His cweek is equal to "1", old code returned "1/2018" value but correct result would be "1/2019".
This commit is contained in:
taitus
2019-02-02 17:18:02 +01:00
parent b6b49c435c
commit ead765a9c1

View File

@@ -7,8 +7,8 @@ module Dashboard::GroupSupports
end
def grouping_key_for(date)
return "#{date.cweek}/#{date.year}" if params[:group_by] == 'week'
return "#{date.year}-#{date.month}" if params[:group_by] == 'month'
return calculate_week(date) if params[:group_by] == "week"
return "#{date.year}-#{date.month}" if params[:group_by] == "month"
date
end
@@ -50,4 +50,29 @@ module Dashboard::GroupSupports
1.day
end
end
private
def calculate_week(date)
week = date.cweek
year = calculate_year_of_week(date)
"#{week}/#{year}"
end
def calculate_year_of_week(date)
year = date.year
if first_week_of_year?(date) && date.end_of_week.year != date.year
year = year + 1
elsif last_week_of_year?(date) && date.beginning_of_week.year != date.year
year = year - 1
end
year
end
def first_week_of_year?(date)
date.cweek == 1
end
def last_week_of_year?(date)
date.cweek == 52 || date.cweek == 53
end
end