Refactor age groups method

We try to make the method return data which is easier to handle in the
view.
This commit is contained in:
Javi Martín
2018-12-20 16:39:00 +01:00
parent be68c8cdbf
commit 9a01ff5323
7 changed files with 52 additions and 45 deletions

View File

@@ -39,7 +39,6 @@ module Statisticable
end
def age_groups
groups = Hash.new(0)
[[16, 19],
[20, 24],
[25, 29],
@@ -55,14 +54,20 @@ module Statisticable
[75, 79],
[80, 84],
[85, 89],
[90, 140]].each do |start, finish|
group_name = (finish == 140 ? "+ 90" : "#{start} - #{finish}")
groups[group_name] = User.where(id: participants)
.where("date_of_birth > ? AND date_of_birth < ?",
finish.years.ago.beginning_of_year,
start.years.ago.end_of_year).count
[90, 300]].reduce({}) do |groups, (start, finish)|
users = User.where(id: participants)
.where("date_of_birth > ? AND date_of_birth < ?",
finish.years.ago.beginning_of_year,
start.years.ago.end_of_year)
groups.tap do |age_groups|
age_groups["#{start} - #{finish}"] = {
range: range_description(start, finish),
count: users.count,
percentage: calculate_percentage(users.count, total_participants)
}
end
end
groups
end
def calculate_percentage(fraction, total)
@@ -70,6 +75,14 @@ module Statisticable
(fraction * 100.0 / total).round(3)
end
def range_description(start, finish)
if finish > 200
I18n.t("stats.age_more_than", start: start)
else
I18n.t("stats.age_range", start: start, finish: finish)
end
end
end
class_methods do

View File

@@ -96,7 +96,7 @@
<div class="row margin">
<div class="small-12 column">
<h2 class="margin-bottom"><%= t("budgets.stats.by_gender") %></h2>
<h2 class="margin-bottom"><%= t("stats.by_gender") %></h2>
</div>
<div class="small-12 medium-6 column text-center">
@@ -130,7 +130,7 @@
<div class="row margin">
<div class="small-12 column">
<h2 class="margin-bottom"><%= t("budgets.stats.by_age") %></h2>
<h2 class="margin-bottom"><%= t("stats.by_age") %></h2>
<table>
<thead>
@@ -140,23 +140,13 @@
</tr>
</thead>
<tbody>
<% all_ages_count = @stats[:age_groups].values.sum.to_f %>
<% @stats[:age_groups].each do |age_group, count| %>
<tr id="age_group_<%= age_group.gsub(" - ", "_to_").gsub("+ ", "up_to_") %>">
<td class="border-right">
<%= age_group.gsub("+", t("budgets.stats.more_than")) + " " + t("budgets.stats.years") %>
</td>
<td class="border-left">
<strong>
<%
percentage_age_count = all_ages_count == 0 ? 0 : (count / all_ages_count * 100)
formatted_percentage_age_count = number_to_stats_percentage(percentage_age_count)
%>
<%= count %>
(<%= formatted_percentage_age_count %>)
</strong>
<div class="progress" role="progressbar" tabindex="0" aria-valuenow="20" aria-valuemin="0" aria-valuetext="<%= percentage_age_count %>" aria-valuemax="100">
<span class="progress-meter" style="width: <%= number_to_stats_percentage(percentage_age_count*5, locale: :en) %>;"></span>
<% @stats[:age_groups].values.each do |group| %>
<tr>
<td><%= group[:range] %></td>
<td>
<%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %>
<div class="progress" role="progressbar" tabindex="0" aria-valuenow="20" aria-valuemin="0" aria-valuetext="<%= group[:percentage] %>" aria-valuemax="100">
<span class="progress-meter" style="width: <%= number_to_stats_percentage(group[:percentage]*5, locale: :en) %>;"></span>
</div>
</td>
</tr>

View File

@@ -204,11 +204,8 @@ en:
supports: Supports
total_male_participants: Mens
total_female_participants: Women
by_age: "Participants by age groups"
age: Age
total: Total
more_than: More than
years: years
by_heading: "Participants by heading"
heading: Heading
investments_sent_html: "Investment proposals sent"

View File

@@ -3,5 +3,10 @@ en:
title: "Participation data"
total_participants: "Participants"
by_gender: "Participants by gender"
by_age: "Participants by age"
men_percentage: "%{percentage} Men"
women_percentage: "%{percentage} Women"
age: "Age"
age_more_than: "%{start} years old and older"
age_range: "%{start} - %{finish} years old"
total: "Total"

View File

@@ -205,10 +205,7 @@ es:
total: Total
total_male_participants: Hombres
total_female_participants: Mujeres
by_age: "Participación por grupos de edad"
age: Edad
more_than: Más de
years: años
by_heading: "Participación por distritos"
heading: Distrito
investments_sent_html: "Propuestas<br>enviadas"

View File

@@ -3,5 +3,10 @@ es:
title: "Estadísticas de participación"
total_participants: "Participantes"
by_gender: "Participación por género"
by_age: "Participación por grupos de edad"
men_percentage: "%{percentage} Hombres"
women_percentage: "%{percentage} Mujeres"
age: "Edad"
age_more_than: "De %{start} y más años"
age_range: "De %{start} a %{finish} años"
total: "Total"

View File

@@ -151,18 +151,18 @@ describe Budget::Stats do
context "#age_groups" do
it "returns the age groups hash" do
expect(@stats[:age_groups]["16 - 19"]).to be 0
expect(@stats[:age_groups]["20 - 24"]).to be 7
expect(@stats[:age_groups]["25 - 29"]).to be 1
expect(@stats[:age_groups]["30 - 34"]).to be 0
expect(@stats[:age_groups]["35 - 39"]).to be 1
expect(@stats[:age_groups]["40 - 44"]).to be 1
expect(@stats[:age_groups]["45 - 49"]).to be 0
expect(@stats[:age_groups]["50 - 54"]).to be 1
expect(@stats[:age_groups]["55 - 59"]).to be 0
expect(@stats[:age_groups]["60 - 64"]).to be 0
expect(@stats[:age_groups]["65 - 69"]).to be 0
expect(@stats[:age_groups]["70 - 140"]).to be 0
expect(@stats[:age_groups]["16 - 19"][:count]).to be 0
expect(@stats[:age_groups]["20 - 24"][:count]).to be 7
expect(@stats[:age_groups]["25 - 29"][:count]).to be 1
expect(@stats[:age_groups]["30 - 34"][:count]).to be 0
expect(@stats[:age_groups]["35 - 39"][:count]).to be 1
expect(@stats[:age_groups]["40 - 44"][:count]).to be 1
expect(@stats[:age_groups]["45 - 49"][:count]).to be 0
expect(@stats[:age_groups]["50 - 54"][:count]).to be 1
expect(@stats[:age_groups]["55 - 59"][:count]).to be 0
expect(@stats[:age_groups]["60 - 64"][:count]).to be 0
expect(@stats[:age_groups]["65 - 69"][:count]).to be 0
expect(@stats[:age_groups]["70 - 74"][:count]).to be 0
end
end