diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index f5a605b84..5ff689641 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -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 diff --git a/app/views/budgets/stats/show.html.erb b/app/views/budgets/stats/show.html.erb index c38852615..5c55be1dd 100644 --- a/app/views/budgets/stats/show.html.erb +++ b/app/views/budgets/stats/show.html.erb @@ -96,7 +96,7 @@
| - <%= age_group.gsub("+", t("budgets.stats.more_than")) + " " + t("budgets.stats.years") %> - | -
-
- <%
- 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 %>)
-
-
-
+ <% @stats[:age_groups].values.each do |group| %>
+ <%= group[:range] %> |
+
+ <%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %>
+ |
+
enviadas" diff --git a/config/locales/es/stats.yml b/config/locales/es/stats.yml index 3ef82915e..926a024f8 100644 --- a/config/locales/es/stats.yml +++ b/config/locales/es/stats.yml @@ -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" diff --git a/spec/models/budget/stats_spec.rb b/spec/models/budget/stats_spec.rb index 7beafb4e0..aa0a836de 100644 --- a/spec/models/budget/stats_spec.rb +++ b/spec/models/budget/stats_spec.rb @@ -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 |