diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb
index 9cc13c726..62b53a15b 100644
--- a/app/models/poll/stats.rb
+++ b/app/models/poll/stats.rb
@@ -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
diff --git a/app/views/polls/stats.html.erb b/app/views/polls/stats.html.erb
index da2a8351c..7fb485e83 100644
--- a/app/views/polls/stats.html.erb
+++ b/app/views/polls/stats.html.erb
@@ -29,7 +29,7 @@
<%= t("stats.polls.by_channel") %>
- <% 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 @@
| <%= t("polls.show.stats.votes") %> |
- <% Poll::Stats::CHANNELS.each do |channel| %>
+ <% @stats.channels.each do |channel| %>
<%= t("polls.show.stats.#{channel}") %> |
<% end %>
<%= t("polls.show.stats.total") %> |
@@ -57,7 +57,7 @@
| <%= t("polls.show.stats.valid") %> |
- <% Poll::Stats::CHANNELS.each do |channel| %>
+ <% @stats.channels.each do |channel| %>
<%= @stats.send(:"total_#{channel}_valid") %>
(<%= @stats.send(:"valid_percentage_#{channel}").round(2) %>%)
@@ -73,7 +73,7 @@
|
| <%= t("polls.show.stats.white") %> |
- <% Poll::Stats::CHANNELS.each do |channel| %>
+ <% @stats.channels.each do |channel| %>
<%= @stats.send(:"total_#{channel}_white") %>
(<%= @stats.send(:"white_percentage_#{channel}").round(2) %>%)
@@ -87,7 +87,7 @@
|
| <%= t("polls.show.stats.null_votes") %> |
- <% Poll::Stats::CHANNELS.each do |channel| %>
+ <% @stats.channels.each do |channel| %>
<%= @stats.send(:"total_#{channel}_null") %>
(<%= @stats.send(:"null_percentage_#{channel}").round(2) %>%)
@@ -102,7 +102,7 @@
|
| <%= t("polls.show.stats.total") %> |
- <% Poll::Stats::CHANNELS.each do |channel| %>
+ <% @stats.channels.each do |channel| %>
<%= @stats.send(:"total_participants_#{channel}") %>
(<%= @stats.send(:"total_participants_#{channel}_percentage").round(2) %>%)
diff --git a/spec/models/poll/stats_spec.rb b/spec/models/poll/stats_spec.rb
index 720d70a8a..8199a593c 100644
--- a/spec/models/poll/stats_spec.rb
+++ b/spec/models/poll/stats_spec.rb
@@ -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
|