Simplify code

This commit is contained in:
Javi Martín
2018-12-28 20:39:01 +01:00
parent 7dbe1dce86
commit 88daaee9fe

View File

@@ -54,20 +54,21 @@ module Statisticable
[75, 79], [75, 79],
[80, 84], [80, 84],
[85, 89], [85, 89],
[90, 300]].reduce({}) do |groups, (start, finish)| [90, 300]
users = User.where(id: participants) ].map do |start, finish|
.where("date_of_birth > ? AND date_of_birth < ?", users = participants.where("date_of_birth > ? AND date_of_birth < ?",
finish.years.ago.beginning_of_year, finish.years.ago.beginning_of_year,
start.years.ago.end_of_year) start.years.ago.end_of_year)
groups.tap do |age_groups| [
age_groups["#{start} - #{finish}"] = { "#{start} - #{finish}",
{
range: range_description(start, finish), range: range_description(start, finish),
count: users.count, count: users.count,
percentage: calculate_percentage(users.count, total_participants) percentage: calculate_percentage(users.count, total_participants)
} }
end ]
end end.to_h
end end
def calculate_percentage(fraction, total) def calculate_percentage(fraction, total)