adds poll results to admin

simple page shows results by question, white and null results
This commit is contained in:
Juanjo Bazán
2017-02-06 17:12:54 +01:00
parent d51a3507a1
commit 35540cdcf1
9 changed files with 144 additions and 1 deletions

View File

@@ -8,7 +8,13 @@ class Admin::Poll::PollsController < Admin::BaseController
end
def show
@poll = Poll.includes(:questions, booth_assignments: [:booth, :final_recounts, :recounts], officers: [:user]).order('poll_questions.title', 'poll_booths.name', 'users.username').find(params[:id])
@poll = Poll.includes(:questions,
booth_assignments: [:booth,
:final_recounts,
:recounts],
officers: [:user]).
order('poll_questions.title', 'poll_booths.name', 'users.username').
find(params[:id])
end
def new

View File

@@ -1,6 +1,9 @@
class Poll < ActiveRecord::Base
has_many :booth_assignments, class_name: "Poll::BoothAssignment"
has_many :booths, through: :booth_assignments
has_many :partial_results, through: :booth_assignments
has_many :white_results, through: :booth_assignments
has_many :null_results, through: :booth_assignments
has_many :voters
has_many :officer_assignments, through: :booth_assignments
has_many :officers, through: :officer_assignments

View File

@@ -9,5 +9,7 @@ class Poll
has_many :officers, through: :officer_assignments
has_many :voters
has_many :partial_results
has_many :white_results
has_many :null_results
end
end

View File

@@ -22,4 +22,9 @@
<%= t("admin.polls.show.recounts_tab") %>
<% end %>
</li>
<li class="tabs-title">
<%= link_to "#tab-results" do %>
<%= t("admin.polls.show.results_tab") %>
<% end %>
</li>
</ul>

View File

@@ -0,0 +1,45 @@
<h3><%= t("admin.polls.show.results_title") %></h3>
<% if @poll.partial_results.empty? %>
<div class="callout primary margin-top">
<%= t("admin.polls.show.no_results") %>
</div>
<% else %>
<table class="margin">
<thead>
<th><%= t("admin.polls.show.table_whites") %></th>
<th><%= t("admin.polls.show.table_nulls") %></th>
</thead>
<tbody>
<tr>
<td id="white_results"><%= @poll.white_results.sum(:amount) %></td>
<td id="null_results"><%= @poll.null_results.sum(:amount) %></td>
</tr>
</tbody>
</table>
<% by_question = @poll.partial_results.group_by(&:question_id) %>
<% @poll.questions.each do |question| %>
<h3><%= question.title %></h3>
<table class="margin">
<thead>
<tr>
<th><%= t("admin.polls.show.table_answer") %></th>
<th class="text-center"><%= t("admin.polls.show.table_votes") %></th>
</tr>
</thead>
<tbody>
<% question.valid_answers.each_with_index do |answer, i| %>
<% by_answer = by_question[question.id].present? ? by_question[question.id].group_by(&:answer) : {} %>
<tr id="question_<%= question.id %>_<%= i %>_result">
<td><%= answer %></td>
<td class="text-center"><%= by_answer[answer].present? ? by_answer[answer].sum(&:amount) : 0 %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% end %>

View File

@@ -37,4 +37,8 @@
<div class="tabs-panel" id="tab-recounts">
<%= render 'recounting' %>
</div>
<div class="tabs-panel" id="tab-results">
<%= render 'results' %>
</div>
</div>

View File

@@ -312,15 +312,18 @@ en:
booths_tab: Booths
officers_tab: Officers
recounts_tab: Recounting
results_tab: Results
no_booths: "There are no booths assigned to this poll."
no_questions: "There are no questions assigned to this poll."
no_officers: "There are no officers assigned to this poll."
no_recounts: "There is nothing to be recounted"
no_results: "There are no results"
remove_booth: "Remove booth from poll"
booths_title: "List of booths"
officers_title: "List of officers"
questions_title: "List of questions"
recounting_title: "Recounts"
results_title: "Results"
remove_question: "Remove question from poll"
add_booth: "Assign booth"
add_question: "Include question"
@@ -336,6 +339,10 @@ en:
table_booth_name: "Booth"
table_final_recount: "Final recount (by officer)"
table_recounts: "Accumulated daily recounts (by officer)"
table_whites: "Blank ballots"
table_nulls: "Invalid ballots"
table_answer: Answer
table_votes: Votes
flash:
question_added: "Question added to this poll"
error_on_question_added: "Question could not be assigned to this poll"

View File

@@ -312,15 +312,18 @@ es:
booths_tab: Urnas
officers_tab: Presidentes de mesa
recounts_tab: Recuentos
results_tab: Resultados
no_booths: "No hay urnas asignadas a esta votación."
no_questions: "No hay preguntas asignadas a esta votación."
no_officers: "No hay presidentes de mesa asignados a esta votación."
no_recounts: "No hay nada de lo que hacer recuento"
no_results: "No hay resultados"
remove_booth: "Desasignar urna"
booths_title: "Listado de urnas asignadas"
officers_title: "Listado de presidentes de mesa asignados"
questions_title: "Listado de preguntas asignadas"
recounting_title: "Recuentos"
results_title: "Resultados"
remove_question: "Desasignar pregunta"
add_booth: "Asignar urna"
add_question: "Incluir pregunta"
@@ -336,6 +339,10 @@ es:
table_booth_name: "Urna"
table_final_recount: "Recuento final (presidente de mesa)"
table_recounts: "Recuentos diarios acumulados (presidente de mesa)"
table_whites: Papeletas en blanco
table_nulls: Papeletas nulas
table_answer: Respuesta
table_votes: Votos
flash:
question_added: "Pregunta añadida a esta votación"
error_on_question_added: "No se pudo asignar la pregunta"

View File

@@ -304,4 +304,68 @@ feature 'Admin polls' do
end
end
context "Results" do
context "Poll show" do
scenario "No results", :js do
poll = create(:poll)
visit admin_poll_path(poll)
click_link "Results"
expect(page).to have_content "There are no results"
end
scenario "Results by answer", :js do
poll = create(:poll)
booth_assignment_1 = create(:poll_booth_assignment, poll: poll)
booth_assignment_2 = create(:poll_booth_assignment, poll: poll)
booth_assignment_3 = create(:poll_booth_assignment, poll: poll)
question_1 = create(:poll_question, poll: poll, valid_answers: "Yes,No")
question_2 = create(:poll_question, poll: poll, valid_answers: "Today,Tomorrow")
[booth_assignment_1, booth_assignment_2, booth_assignment_3].each do |ba|
create(:poll_partial_result,
booth_assignment: ba,
question: question_1,
answer: 'Yes',
amount: 11)
create(:poll_partial_result,
booth_assignment: ba,
question: question_2,
answer: 'Tomorrow',
amount: 5)
end
create(:poll_white_result,
booth_assignment: booth_assignment_1,
amount: 21)
create(:poll_null_result,
booth_assignment: booth_assignment_3,
amount: 44)
visit admin_poll_path(poll)
click_link "Results"
expect(page).to have_content(question_1.title)
question_1.valid_answers.each_with_index do |answer, i|
within("#question_#{question_1.id}_#{i}_result") do
expect(page).to have_content(answer)
expect(page).to have_content([33, 0][i])
end
end
expect(page).to have_content(question_2.title)
question_2.valid_answers.each_with_index do |answer, i|
within("#question_#{question_2.id}_#{i}_result") do
expect(page).to have_content(answer)
expect(page).to have_content([0,15][i])
end
end
within('#white_results') { expect(page).to have_content('21') }
within('#null_results') { expect(page).to have_content('44') }
end
end
end
end