adds automatic recount to admin poll and paginates recounts

This commit is contained in:
Juanjo Bazán
2017-02-14 19:44:19 +01:00
parent 2b68f02b95
commit d68be1f6f0
6 changed files with 37 additions and 7 deletions

View File

@@ -2,7 +2,10 @@ class Admin::Poll::RecountsController < Admin::BaseController
before_action :load_poll
def index
@booth_assignments = @poll.booth_assignments.includes(:recounts, :final_recounts)
@booth_assignments = @poll.booth_assignments.
includes(:booth, :recounts, :final_recounts, :voters).
order(created_at: :desc).
page(params[:page]).per(50)
end
private

View File

@@ -4,4 +4,12 @@ module PollRecountsHelper
recounts.select {|r| r.date == date}.first
end
def booth_assignment_sum_recounts(ba)
ba.recounts.any? ? ba.recounts.to_a.sum(&:count) : nil
end
def booth_assignment_sum_final_recounts(ba)
ba.final_recounts.any? ? ba.final_recounts.to_a.sum(&:count) :nil
end
end

View File

@@ -14,32 +14,45 @@
<th><%= t("admin.recounts.index.table_booth_name") %></th>
<th class="text-center"><%= t("admin.recounts.index.table_recounts") %></th>
<th class="text-center"><%= t("admin.recounts.index.table_final_recount") %></th>
<th class="text-center"><%= t("admin.recounts.index.table_system_count") %></th>
</thead>
<tbody>
<% @booth_assignments.each do |booth_assignment| %>
<% recount = booth_assignment_sum_recounts(booth_assignment) %>
<% final_recount = booth_assignment_sum_final_recounts(booth_assignment) %>
<% system_count = booth_assignment.voters.size %>
<tr id="<%= dom_id(booth_assignment) %>_recounts" class="booth_recounts">
<td>
<strong>
<%= link_to booth_assignment.booth.name, admin_poll_booth_assignment_path(@poll, booth_assignment, anchor: 'tab-recounts') %>
</strong>
</td>
<td class="text-center">
<% if booth_assignment.recounts.any? %>
<%= booth_assignment.recounts.to_a.sum(&:count) %>
<td class="text-center <%= 'count-error' if recount.to_i != system_count %>">
<% if recount.present? %>
<%= recount %>
<% else %>
<span>-</span>
<% end %>
</td>
<td class="text-center <%= 'count-error' if final_recount.to_i != system_count %>">
<% if final_recount.present? %>
<strong><%= final_recount %></strong>
<% else %>
<span>-</span>
<% end %>
</td>
<td class="text-center">
<% if booth_assignment.final_recounts.any? %>
<strong><%= booth_assignment.final_recounts.to_a.sum(&:count) %></strong>
<% if system_count.present? %>
<strong><%= system_count %></strong>
<% else %>
<span>-</span>
<span>0</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate @booth_assignments %>
<% end %>
</div>

View File

@@ -381,6 +381,7 @@ en:
table_booth_name: "Booth"
table_recounts: "Accumulated daily recounts (by officer)"
table_final_recount: "Final recount (by officer)"
table_system_count: "Votes (automatic)"
results:
index:
title: "Results"

View File

@@ -381,6 +381,7 @@ es:
table_booth_name: "Urna"
table_recounts: "Recuentos diarios acumulados (presidente de mesa)"
table_final_recount: "Recuento final (presidente de mesa)"
table_system_count: "Votos (automático)"
results:
index:
title: "Resultados"

View File

@@ -258,6 +258,9 @@ feature 'Admin polls' do
date: poll.starts_at + i.days,
count: 21) }
2.times { create(:poll_voter,
booth_assignment: booth_assignment_final_recounted) }
create(:poll_recount,
booth_assignment: booth_assignment_recounted,
date: poll.ends_at,
@@ -290,6 +293,7 @@ feature 'Admin polls' do
expect(page).to have_content(booth_assignment_final_recounted.booth.name)
expect(page).to have_content('-')
expect(page).to have_content('55555')
expect(page).to have_content('2')
end
end
end