Display only poll stats for used channels

So if there's no vote using by mail (which is the case in some places),
no stats related to voting via mail are displayed.
This commit is contained in:
Javi Martín
2019-03-18 19:43:44 +01:00
parent 4843959c7d
commit 6b0b9db969
3 changed files with 57 additions and 7 deletions

View File

@@ -21,6 +21,10 @@ class Poll::Stats
total_participants_web + total_participants_booth
end
def channels
CHANNELS.select { |channel| send(:"total_participants_#{channel}") > 0 }
end
CHANNELS.each do |channel|
define_method :"total_participants_#{channel}" do
send(:"total_#{channel}_valid") +
@@ -58,7 +62,7 @@ class Poll::Stats
end
def total_letter_valid
0 # TODO
voters.where(origin: "letter").count # TODO: count only valid votes
end
def total_letter_white

View File

@@ -29,7 +29,7 @@
<div id="stats_by_channel" class="stats-group">
<h4><%= t("stats.polls.by_channel") %></h4>
<% Poll::Stats::CHANNELS.each do |channel| %>
<% @stats.channels.each do |channel| %>
<%= number_with_info_tags(
@stats.send("total_participants_#{channel}"),
t("stats.polls.#{channel}_percentage",
@@ -47,7 +47,7 @@
<thead>
<tr>
<th scope="col"><%= t("polls.show.stats.votes") %></th>
<% Poll::Stats::CHANNELS.each do |channel| %>
<% @stats.channels.each do |channel| %>
<th scope="col"><%= t("polls.show.stats.#{channel}") %></th>
<% end %>
<th scope="col"><%= t("polls.show.stats.total") %></th>
@@ -57,7 +57,7 @@
<tr>
<th scope="row"><%= t("polls.show.stats.valid") %></th>
<% Poll::Stats::CHANNELS.each do |channel| %>
<% @stats.channels.each do |channel| %>
<td>
<%= @stats.send(:"total_#{channel}_valid") %>
<small><em>(<%= @stats.send(:"valid_percentage_#{channel}").round(2) %>%)</em></small>
@@ -73,7 +73,7 @@
<tr>
<th scope="row"><%= t("polls.show.stats.white") %></th>
<% Poll::Stats::CHANNELS.each do |channel| %>
<% @stats.channels.each do |channel| %>
<td>
<%= @stats.send(:"total_#{channel}_white") %>
<small><em>(<%= @stats.send(:"white_percentage_#{channel}").round(2) %>%)</em></small>
@@ -87,7 +87,7 @@
<tr>
<th scope="row"><%= t("polls.show.stats.null_votes") %></th>
<% Poll::Stats::CHANNELS.each do |channel| %>
<% @stats.channels.each do |channel| %>
<td>
<%= @stats.send(:"total_#{channel}_null") %>
<small><em>(<%= @stats.send(:"null_percentage_#{channel}").round(2) %>%)</em></small>
@@ -102,7 +102,7 @@
<tr>
<th scope="row"><%= t("polls.show.stats.total") %></th>
<% Poll::Stats::CHANNELS.each do |channel| %>
<% @stats.channels.each do |channel| %>
<td>
<%= @stats.send(:"total_participants_#{channel}") %>
<small><em>(<%= @stats.send(:"total_participants_#{channel}_percentage").round(2) %>%)</em></small>

View File

@@ -183,4 +183,50 @@ describe Poll::Stats do
expect(stats.participants_by_geozone["Midgar"][:percentage]).to eq(33.333)
end
end
describe "#channels" do
context "no participants" do
it "returns no channels" do
expect(stats.channels).to eq []
end
end
context "only participants from web" do
before { create(:poll_voter, :from_web, poll: poll) }
it "returns the web channel" do
expect(stats.channels).to eq ["web"]
end
end
context "only participants from booth" do
before do
create(:poll_recount, :from_booth, poll: poll, total_amount: 1)
end
it "returns the booth channel" do
expect(stats.channels).to eq ["booth"]
end
end
context "only participants from letter" do
before { create(:poll_voter, origin: "letter", poll: poll) }
it "returns the web channel" do
expect(stats.channels).to eq ["letter"]
end
end
context "participants from all channels" do
before do
create(:poll_voter, :from_web, poll: poll)
create(:poll_recount, :from_booth, poll: poll, total_amount: 1)
create(:poll_voter, origin: "letter", poll: poll)
end
it "returns all channels" do
expect(stats.channels).to eq %w[web booth letter]
end
end
end
end