From d68be1f6f007a39ddb3866b28906274c0b85111a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Tue, 14 Feb 2017 19:44:19 +0100 Subject: [PATCH] adds automatic recount to admin poll and paginates recounts --- .../admin/poll/recounts_controller.rb | 5 +++- app/helpers/poll_recounts_helper.rb | 8 ++++++ app/views/admin/poll/recounts/index.html.erb | 25 ++++++++++++++----- config/locales/admin.en.yml | 1 + config/locales/admin.es.yml | 1 + spec/features/admin/poll/polls_spec.rb | 4 +++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/poll/recounts_controller.rb b/app/controllers/admin/poll/recounts_controller.rb index 6df6e8ed5..12b78aa3f 100644 --- a/app/controllers/admin/poll/recounts_controller.rb +++ b/app/controllers/admin/poll/recounts_controller.rb @@ -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 diff --git a/app/helpers/poll_recounts_helper.rb b/app/helpers/poll_recounts_helper.rb index 95ce54341..c47402163 100644 --- a/app/helpers/poll_recounts_helper.rb +++ b/app/helpers/poll_recounts_helper.rb @@ -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 \ No newline at end of file diff --git a/app/views/admin/poll/recounts/index.html.erb b/app/views/admin/poll/recounts/index.html.erb index bb1f90364..98055f553 100644 --- a/app/views/admin/poll/recounts/index.html.erb +++ b/app/views/admin/poll/recounts/index.html.erb @@ -14,32 +14,45 @@ <%= t("admin.recounts.index.table_booth_name") %> <%= t("admin.recounts.index.table_recounts") %> <%= t("admin.recounts.index.table_final_recount") %> + <%= t("admin.recounts.index.table_system_count") %> <% @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 %> <%= link_to booth_assignment.booth.name, admin_poll_booth_assignment_path(@poll, booth_assignment, anchor: 'tab-recounts') %> - - <% if booth_assignment.recounts.any? %> - <%= booth_assignment.recounts.to_a.sum(&:count) %> + + <% if recount.present? %> + <%= recount %> + <% else %> + - + <% end %> + + + <% if final_recount.present? %> + <%= final_recount %> <% else %> - <% end %> - <% if booth_assignment.final_recounts.any? %> - <%= booth_assignment.final_recounts.to_a.sum(&:count) %> + <% if system_count.present? %> + <%= system_count %> <% else %> - - + 0 <% end %> <% end %> + + <%= paginate @booth_assignments %> <% end %> \ No newline at end of file diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 8d5e113cd..564137a09 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -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" diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 2b087f385..aaf83e275 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -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" diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index ebe21b335..cb5a34265 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -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