diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb
index cd24aed8e..2d588a59a 100644
--- a/app/models/concerns/statisticable.rb
+++ b/app/models/concerns/statisticable.rb
@@ -49,6 +49,23 @@ module Statisticable
end.to_h
end
+ def participants_by_geozone
+ Geozone.all.order("name").map do |geozone|
+ count = participants.where(geozone: geozone).count
+
+ [
+ geozone.name,
+ {
+ total: {
+ count: count,
+ percentage: calculate_percentage(count, total_participants)
+ },
+ percentage: calculate_percentage(count, geozone.users.count)
+ }
+ ]
+ end.to_h
+ end
+
private
def total_participants_with_gender
@@ -92,9 +109,10 @@ module Statisticable
class_methods do
def stats_methods
- %i[total_participants total_male_participants
- total_female_participants total_unknown_gender_or_age
- male_percentage female_percentage participants_by_age]
+ %i[total_participants
+ total_male_participants total_female_participants total_unknown_gender_or_age
+ male_percentage female_percentage
+ participants_by_age participants_by_geozone]
end
def stats_cache(*method_names)
diff --git a/app/views/shared/stats/_links.html.erb b/app/views/shared/stats/_links.html.erb
index 7b7d6d7ac..581cce4b2 100644
--- a/app/views/shared/stats/_links.html.erb
+++ b/app/views/shared/stats/_links.html.erb
@@ -9,4 +9,7 @@
<%= link_to t("stats.by_age"), "#participants_by_age" %>
+
+ <%= link_to t("stats.by_geozone"), "#participants_by_geozone" %>
+
diff --git a/app/views/shared/stats/_participation.html.erb b/app/views/shared/stats/_participation.html.erb
index 88faf3575..ed9119f2a 100644
--- a/app/views/shared/stats/_participation.html.erb
+++ b/app/views/shared/stats/_participation.html.erb
@@ -56,4 +56,28 @@
+
+
+
<%= t("stats.by_geozone") %>
+
+
+
+
+ | <%= t("stats.geozone") %> |
+ <%= t("stats.total") %> |
+ <%= t("stats.geozone_participation") %> |
+
+
+
+
+ <% stats[:participants_by_geozone].each do |geozone, participants| %>
+
+ | <%= geozone %> |
+ <%= "#{participants[:total][:count]} (#{number_to_stats_percentage(participants[:total][:percentage])})" %> |
+ <%= number_to_stats_percentage(participants[:percentage]) %> |
+
+ <% end %>
+
+
+
diff --git a/config/locales/en/stats.yml b/config/locales/en/stats.yml
index 947e87b11..3cb6a3385 100644
--- a/config/locales/en/stats.yml
+++ b/config/locales/en/stats.yml
@@ -4,9 +4,12 @@ en:
total_participants: "Participants"
by_gender: "Participants by gender"
by_age: "Participants by age"
+ by_geozone: "Participants by district"
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"
+ geozone: "District"
+ geozone_participation: "% District population participation"
diff --git a/config/locales/es/stats.yml b/config/locales/es/stats.yml
index 926a024f8..2055060bf 100644
--- a/config/locales/es/stats.yml
+++ b/config/locales/es/stats.yml
@@ -4,9 +4,12 @@ es:
total_participants: "Participantes"
by_gender: "Participación por género"
by_age: "Participación por grupos de edad"
+ by_geozone: "Participación por distritos"
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"
+ geozone: "Distrito"
+ geozone_participation: "% Partipación población del distrito"
diff --git a/spec/models/poll/stats_spec.rb b/spec/models/poll/stats_spec.rb
index 5f6e345da..2b2fe6c7b 100644
--- a/spec/models/poll/stats_spec.rb
+++ b/spec/models/poll/stats_spec.rb
@@ -156,6 +156,34 @@ describe Poll::Stats do
end
end
+ describe "#participants_by_geozone" do
+ it "groups by geozones in alphabetic order" do
+ %w[Oceania Eurasia Eastasia].each { |name| create(:geozone, name: name) }
+
+ expect(stats.participants_by_geozone.keys).to eq %w[Eastasia Eurasia Oceania]
+ end
+
+ it "calculates percentage relative to total participants" do
+ hobbiton = create(:geozone, name: "Hobbiton")
+ rivendel = create(:geozone, name: "Rivendel")
+
+ 3.times { create :poll_voter, poll: poll, user: create(:user, :level_two, geozone: hobbiton) }
+ 2.times { create :poll_voter, poll: poll, user: create(:user, :level_two, geozone: rivendel) }
+
+ expect(stats.participants_by_geozone["Hobbiton"][:total]).to eq(count: 3, percentage: 60.0)
+ expect(stats.participants_by_geozone["Rivendel"][:total]).to eq(count: 2, percentage: 40.0)
+ end
+
+ it "calculates percentage relative to the geozone population" do
+ midgar = create(:geozone, name: "Midgar")
+
+ create(:poll_voter, poll: poll, user: create(:user, :level_two, geozone: midgar))
+ 2.times { create :user, :level_two, geozone: midgar }
+
+ expect(stats.participants_by_geozone["Midgar"][:percentage]).to eq(33.333)
+ end
+ end
+
describe "#generate" do
it "generates the correct stats" do
poll = create(:poll)