Add geozone stats to polls

This commit is contained in:
Javi Martín
2019-01-04 17:08:33 +01:00
parent 49f4a53569
commit 90fe746d27
6 changed files with 82 additions and 3 deletions

View File

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

View File

@@ -9,4 +9,7 @@
<li>
<%= link_to t("stats.by_age"), "#participants_by_age" %>
</li>
<li>
<%= link_to t("stats.by_geozone"), "#participants_by_geozone" %>
</li>
</ul>

View File

@@ -56,4 +56,28 @@
</tbody>
</table>
</div>
<div id="participants_by_geozone" class="stats-group">
<h4><%= t("stats.by_geozone") %></h4>
<table>
<thead>
<tr>
<th><%= t("stats.geozone") %></th>
<th><%= t("stats.total") %></th>
<th><%= t("stats.geozone_participation") %></th>
</tr>
</thead>
<tbody>
<% stats[:participants_by_geozone].each do |geozone, participants| %>
<tr>
<td><%= geozone %></td>
<td><%= "#{participants[:total][:count]} (#{number_to_stats_percentage(participants[:total][:percentage])})" %></td>
<td><%= number_to_stats_percentage(participants[:percentage]) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>

View File

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

View File

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

View File

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