Add poll stats by gender and channel

This commit is contained in:
Javi Martín
2019-01-04 18:24:06 +01:00
parent 553af8e95b
commit 7b408a4b88
8 changed files with 170 additions and 3 deletions

View File

@@ -81,4 +81,66 @@
border-bottom-left-radius: rem-calc(20);
border-top-left-radius: rem-calc(20);
}
.gender-and-channel {
thead,
tbody {
border: 0;
}
thead,
tr,
th {
background-color: transparent;
}
td,
th {
text-align: center;
text-transform: uppercase;
&::before {
display: inline-block;
transform: scale(0.6);
vertical-align: middle;
}
}
th {
font-weight: bold;
font-size: rem-calc(21);
&.gender {
opacity: 0;
}
&.web::before {
content: image-url('stats_web.png');
}
&.booth::before {
content: image-url('stats_booth.png');
}
&.mail::before {
content: image-url('stats_mail.png');
}
}
td {
&.gender {
font-weight: bold;
font-size: rem-calc(21);
text-align: left;
}
&.male::before {
content: image-url('stats_man.png');
}
&.female::before {
content: image-url('stats_woman.png');
}
}
}
}

View File

@@ -13,11 +13,11 @@ module Statisticable
end
def total_male_participants
participants.where(gender: "male").count
participants.male.count
end
def total_female_participants
participants.where(gender: "female").count
participants.female.count
end
def total_unknown_gender_or_age

View File

@@ -14,7 +14,11 @@ class Poll::Stats
total_participants_mail_percentage
valid_percentage_web valid_percentage_booth valid_percentage_mail total_valid_percentage
white_percentage_web white_percentage_booth white_percentage_mail total_white_percentage
null_percentage_web null_percentage_booth null_percentage_mail total_null_percentage]
null_percentage_web null_percentage_booth null_percentage_mail total_null_percentage
total_male_web total_male_booth total_male_mail
total_female_web total_female_booth total_female_mail
male_web_percentage male_booth_percentage male_mail_percentage
female_web_percentage female_booth_percentage female_mail_percentage]
end
def total_participants
@@ -31,6 +35,23 @@ class Poll::Stats
define_method :"total_participants_#{channel}_percentage" do
calculate_percentage(send(:"total_participants_#{channel}"), total_participants)
end
define_method :"#{channel}_participants" do
User.where(id: voters.where(origin: channel).pluck(:user_id))
end
%i[male female].each do |gender|
define_method :"total_#{gender}_#{channel}" do
send(:"#{channel}_participants").public_send(gender).count
end
define_method :"#{gender}_#{channel}_percentage" do
calculate_percentage(
send(:"total_#{gender}_#{channel}"),
send(:"total_#{gender}_participants")
)
end
end
end
def total_web_valid

View File

@@ -55,6 +55,8 @@ class User < ApplicationRecord
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }
scope :officials, -> { where("official_level > 0") }
scope :male, -> { where(gender: "male") }
scope :female, -> { where(gender: "female") }
scope :newsletter, -> { where(newsletter: true) }
scope :for_render, -> { includes(:organization) }
scope :by_document, ->(document_type, document_number) do

View File

@@ -14,6 +14,9 @@
<li>
<%= link_to t("stats.polls.by_channel"), "#stats_by_channel" %>
</li>
<li>
<%= link_to t("stats.polls.by_gender_and_channel"), "#stats_by_gender_and_channel" %>
</li>
<li>
<%= link_to t("stats.polls.vote_by_channel"), "#vote_stats_by_channel" %>
</li>
@@ -40,6 +43,34 @@
<% end %>
</div>
<div id="stats_by_gender_and_channel" class="stats-group">
<h4><%= t("stats.polls.by_gender_and_channel") %></h4>
<table class="gender-and-channel">
<thead>
<tr>
<th class="gender"></th>
<% Poll::Stats::CHANNELS.each do |channel| %>
<th class="<%= channel %>"><%= t("stats.polls.#{channel}") %></th>
<% end %>
</tr>
</thead>
<tbody>
<% %i[male female].each do |gender| %>
<tr>
<td class="<%= gender %> gender"><%= t("stats.#{gender}") %></td>
<% Poll::Stats::CHANNELS.each do |channel| %>
<td>
<%= "#{@stats[:"total_#{gender}_#{channel}"]}
(#{number_to_stats_percentage(@stats[:"#{gender}_#{channel}_percentage"])})" %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
<div id="vote_stats_by_channel" class="stats-group">
<h4><%= t("stats.polls.vote_by_channel") %></h4>

View File

@@ -6,6 +6,8 @@ en:
by_gender: "Participants by gender"
by_age: "Participants by age"
by_geozone: "Participants by district"
male: "Male"
female: "Female"
men_percentage: "%{percentage} Men"
women_percentage: "%{percentage} Women"
age: "Age"
@@ -16,7 +18,11 @@ en:
geozone_participation: "% District population participation"
polls:
by_channel: "Participants by channel"
by_gender_and_channel: "Participants by gender and channel"
vote_by_channel: "Vote type by channel"
web: "Web"
booth: "Booths"
mail: "Mail"
web_percentage: "%{percentage} Web"
booth_percentage: "%{percentage} Booths"
mail_percentage: "%{percentage} Mail"

View File

@@ -8,6 +8,8 @@ es:
by_geozone: "Participación por distritos"
men_percentage: "%{percentage} Hombres"
women_percentage: "%{percentage} Mujeres"
male: "Hombres"
female: "Mujeres"
age: "Edad"
age_more_than: "De %{start} y más años"
age_range: "De %{start} a %{finish} años"
@@ -16,7 +18,11 @@ es:
geozone_participation: "% Partipación población del distrito"
polls:
by_channel: "Participación por medio"
by_gender_and_channel: "Participación por género y medio"
vote_by_channel: "Votos emitidos por medio"
web: "Web"
booth: "Urnas"
mail: "Correo"
web_percentage: "%{percentage} Web"
booth_percentage: "%{percentage} Urnas"
mail_percentage: "%{percentage} Correo"

View File

@@ -182,6 +182,45 @@ describe Poll::Stats do
expect(stats.participants_by_geozone["Midgar"][:percentage]).to eq(33.333)
end
describe "participants by gender and channel" do
before do
4.times do
create :poll_voter, :from_web, poll: poll,
user: create(:user, :level_two, gender: "female")
end
3.times do
create :poll_voter, :from_web, poll: poll,
user: create(:user, :level_two, gender: "male")
end
2.times do
create :poll_voter, :from_booth, poll: poll,
user: create(:user, :level_two, gender: "female")
end
1.times do
create :poll_voter, :from_booth, poll: poll,
user: create(:user, :level_two, gender: "male")
end
end
it "calculates total participants" do
expect(stats.total_female_web).to eq(4)
expect(stats.total_male_web).to eq(3)
expect(stats.total_female_booth).to eq(2)
expect(stats.total_male_booth).to eq(1)
end
it "calculates percentage relative to the participants for that gender" do
expect(stats.female_web_percentage).to eq(66.667)
expect(stats.female_booth_percentage).to eq(33.333)
expect(stats.male_web_percentage).to eq(75.0)
expect(stats.male_booth_percentage).to eq(25.0)
end
end
end
describe "#generate" do