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 @@
-

<%= t("budgets.stats.by_gender") %>

+

<%= t("stats.by_gender") %>

@@ -130,7 +130,7 @@
-

<%= t("budgets.stats.by_age") %>

+

<%= t("stats.by_age") %>

@@ -140,23 +140,13 @@ - <% all_ages_count = @stats[:age_groups].values.sum.to_f %> - <% @stats[:age_groups].each do |age_group, count| %> - "> - - + + diff --git a/config/locales/en/budgets.yml b/config/locales/en/budgets.yml index e1757b7a8..4aa814def 100644 --- a/config/locales/en/budgets.yml +++ b/config/locales/en/budgets.yml @@ -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" diff --git a/config/locales/en/stats.yml b/config/locales/en/stats.yml index 101419948..947e87b11 100644 --- a/config/locales/en/stats.yml +++ b/config/locales/en/stats.yml @@ -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" diff --git a/config/locales/es/budgets.yml b/config/locales/es/budgets.yml index 7b5c25b8e..67599b725 100644 --- a/config/locales/es/budgets.yml +++ b/config/locales/es/budgets.yml @@ -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
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
- <%= 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])})" %> +
+