Simplify age groups method

This commit is contained in:
Javi Martín
2018-12-20 13:12:03 +01:00
parent c2489e3209
commit c1b76a7ebf

View File

@@ -40,28 +40,27 @@ module Statisticable
def age_groups def age_groups
groups = Hash.new(0) groups = Hash.new(0)
["16 - 19", [[16, 19],
"20 - 24", [20, 24],
"25 - 29", [25, 29],
"30 - 34", [30, 34],
"35 - 39", [35, 39],
"40 - 44", [40, 44],
"45 - 49", [45, 49],
"50 - 54", [50, 54],
"55 - 59", [55, 59],
"60 - 64", [60, 64],
"65 - 69", [65, 69],
"70 - 74", [70, 74],
"75 - 79", [75, 79],
"80 - 84", [80, 84],
"85 - 89", [85, 89],
"90 - 140"].each do |group| [90, 140]].each do |start, finish|
start, finish = group.split(" - ") group_name = (finish == 140 ? "+ 90" : "#{start} - #{finish}")
group_name = (group == "90 - 140" ? "+ 90" : group)
groups[group_name] = User.where(id: participants) groups[group_name] = User.where(id: participants)
.where("date_of_birth > ? AND date_of_birth < ?", .where("date_of_birth > ? AND date_of_birth < ?",
finish.to_i.years.ago.beginning_of_year, finish.years.ago.beginning_of_year,
start.to_i.years.ago.end_of_year).count start.years.ago.end_of_year).count
end end
groups groups
end end