From 2a902c68eb72b4e1de495571d3f9c3eec534cfe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Fri, 3 Feb 2017 19:11:04 +0100 Subject: [PATCH] adds creation of null and white results by poll officers --- .../officing/results_controller.rb | 31 +++++++- app/views/officing/results/index.html.erb | 17 ++++- app/views/officing/results/new.html.erb | 6 +- config/locales/officing.en.yml | 4 +- config/locales/officing.es.yml | 4 +- spec/features/officing/results_spec.rb | 16 +++++ spec/models/poll/null_result_spec.rb | 70 +++++++++++++++++++ spec/models/poll/white_result_spec.rb | 70 +++++++++++++++++++ 8 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 spec/models/poll/null_result_spec.rb create mode 100644 spec/models/poll/white_result_spec.rb diff --git a/app/controllers/officing/results_controller.rb b/app/controllers/officing/results_controller.rb index 76e4dff3e..125dc9448 100644 --- a/app/controllers/officing/results_controller.rb +++ b/app/controllers/officing/results_controller.rb @@ -26,6 +26,8 @@ class Officing::ResultsController < Officing::BaseController @partial_results = ::Poll::PartialResult.includes(:question). where(booth_assignment_id: index_params[:booth_assignment_id]). where(date: index_params[:date]) + @whites = ::Poll::WhiteResult.where(booth_assignment_id: @booth_assignment.id, date: index_params[:date]).sum(:amount) + @nulls = ::Poll::NullResult.where(booth_assignment_id: @booth_assignment.id, date: index_params[:date]).sum(:amount) end end @@ -65,6 +67,33 @@ class Officing::ResultsController < Officing::BaseController end end end + + build_white_results + build_null_results + end + + def build_white_results + if results_params[:whites].present? + white_result = ::Poll::WhiteResult.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id, + date: results_params[:date]) + white_result.officer_assignment_id = @officer_assignment.id + white_result.amount = results_params[:whites].to_i + white_result.author = current_user + white_result.origin = 'booth' + @results << white_result + end + end + + def build_null_results + if results_params[:nulls].present? + white_result = ::Poll::WhiteResult.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id, + date: results_params[:date]) + white_result.officer_assignment_id = @officer_assignment.id + white_result.amount = results_params[:nulls].to_i + white_result.author = current_user + white_result.origin = 'booth' + @results << white_result + end end def go_back_to_new(alert = nil) @@ -102,7 +131,7 @@ class Officing::ResultsController < Officing::BaseController end def results_params - params.permit(:officer_assignment_id, :date, :questions) + params.permit(:officer_assignment_id, :date, :questions, :whites, :nulls) end def index_params diff --git a/app/views/officing/results/index.html.erb b/app/views/officing/results/index.html.erb index db839cbeb..70da6d8d3 100644 --- a/app/views/officing/results/index.html.erb +++ b/app/views/officing/results/index.html.erb @@ -8,8 +8,24 @@ +
+ + + + + + + + + + + + + +
<%= t("officing.results.index.table_whites") %><%= t("officing.results.index.table_nulls") %>
<%= @whites %><%= @nulls %>
+ <% by_question = @partial_results.group_by(&:question_id) %> <% @poll.questions.each do |question| %>

<%= question.title %>

@@ -31,7 +47,6 @@ <% end %> - <% end %>
diff --git a/app/views/officing/results/new.html.erb b/app/views/officing/results/new.html.erb index 1d8dee7fd..74af8af10 100644 --- a/app/views/officing/results/new.html.erb +++ b/app/views/officing/results/new.html.erb @@ -39,13 +39,13 @@
-

<%= t("officing.results.new.ballots_blank") %>

- <%= text_field_tag :count, nil, placeholder: "0" %> +

<%= t("officing.results.new.ballots_white") %>

+ <%= text_field_tag :whites, params[:whites].presence, placeholder: "0" %>

<%= t("officing.results.new.ballots_null") %>

- <%= text_field_tag :count, nil, placeholder: "0" %> + <%= text_field_tag :nulls, params[:nulls].presence, placeholder: "0" %>

diff --git a/config/locales/officing.en.yml b/config/locales/officing.en.yml index 03336efdd..2ba703672 100644 --- a/config/locales/officing.en.yml +++ b/config/locales/officing.en.yml @@ -66,7 +66,7 @@ en: date: "Date" select_booth: "Select booth" select_date: "Select date" - ballots_blank: "Blank ballots" + ballots_white: "Blank ballots" ballots_null: "Invalid ballots" submit: "Save" results_list: "Your results" @@ -76,6 +76,8 @@ en: results: Results table_answer: Answer table_votes: Votes + table_whites: "Blank ballots" + table_nulls: "Invalid ballots" residence: flash: create: "Document verified with Census" diff --git a/config/locales/officing.es.yml b/config/locales/officing.es.yml index 0f8c87067..205818971 100644 --- a/config/locales/officing.es.yml +++ b/config/locales/officing.es.yml @@ -66,7 +66,7 @@ es: date: "Día" select_booth: "Elige urna" select_date: "Elige día" - ballots_blank: "Papeletas en blanco" + ballots_white: "Papeletas en blanco" ballots_null: "Papeletas nulas" submit: "Guardar" results_list: "Tus resultados" @@ -76,6 +76,8 @@ es: results: "Resultados" table_answer: Respuesta table_votes: Votos + table_whites: Papeletas en blanco + table_nulls: Papeletas en blanco residence: flash: create: "Documento verificado con el Padrón" diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index 586ed387e..e918edd59 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -62,6 +62,10 @@ feature 'Officing Results' do fill_in "questions[#{@question_2.id}][0]", with: '333' fill_in "questions[#{@question_2.id}][1]", with: '444' + + fill_in "whites", with: '66' + fill_in "nulls", with: '77' + click_button 'Save' expect(page).to have_content('Your results') @@ -116,6 +120,16 @@ feature 'Officing Results' do date: @poll.ends_at, question: @question_1, amount: 33) + white_result = create(:poll_white_result, + officer_assignment: @officer_assignment, + booth_assignment: @officer_assignment.booth_assignment, + date: @poll.ends_at, + amount: 21) + null_result = create(:poll_null_result, + officer_assignment: @officer_assignment, + booth_assignment: @officer_assignment.booth_assignment, + date: @poll.ends_at, + amount: 44) visit officing_poll_results_path(@poll, date: I18n.l(@poll.ends_at.to_date), @@ -134,6 +148,8 @@ feature 'Officing Results' do within("#question_#{@question_2.id}_#{i}_result") { expect(page).to have_content(answer) } end + within('#white_results') { expect(page).to have_content('21') } + within('#null_results') { expect(page).to have_content('44') } end end \ No newline at end of file diff --git a/spec/models/poll/null_result_spec.rb b/spec/models/poll/null_result_spec.rb new file mode 100644 index 000000000..9293027d1 --- /dev/null +++ b/spec/models/poll/null_result_spec.rb @@ -0,0 +1,70 @@ +require 'rails_helper' + +describe Poll::NullResult do + + describe "logging changes" do + it "should update amount_log if amount changes" do + null_result = create(:poll_null_result, amount: 33) + + expect(null_result.amount_log).to eq("") + + null_result.amount = 33 + null_result.save + null_result.amount = 32 + null_result.save + null_result.amount = 34 + null_result.save + + expect(null_result.amount_log).to eq(":33:32") + end + + it "should update officer_assignment_id_log if amount changes" do + null_result = create(:poll_null_result, amount: 33) + + expect(null_result.amount_log).to eq("") + expect(null_result.officer_assignment_id_log).to eq("") + + null_result.amount = 33 + null_result.officer_assignment_id = 1 + null_result.save + + null_result.amount = 32 + null_result.officer_assignment_id = 2 + null_result.save + + null_result.amount = 34 + null_result.officer_assignment_id = 3 + null_result.save + + expect(null_result.amount_log).to eq(":33:32") + expect(null_result.officer_assignment_id_log).to eq(":1:2") + end + + it "should update author_id if amount changes" do + null_result = create(:poll_null_result, amount: 33) + + expect(null_result.amount_log).to eq("") + expect(null_result.author_id_log).to eq("") + + author_A = create(:poll_officer).user + author_B = create(:poll_officer).user + author_C = create(:poll_officer).user + + null_result.amount = 33 + null_result.author_id = author_A.id + null_result.save! + + null_result.amount = 32 + null_result.author_id = author_B.id + null_result.save! + + null_result.amount = 34 + null_result.author_id = author_C.id + null_result.save! + + expect(null_result.amount_log).to eq(":33:32") + expect(null_result.author_id_log).to eq(":#{author_A.id}:#{author_B.id}") + end + end + +end \ No newline at end of file diff --git a/spec/models/poll/white_result_spec.rb b/spec/models/poll/white_result_spec.rb new file mode 100644 index 000000000..66a036cfe --- /dev/null +++ b/spec/models/poll/white_result_spec.rb @@ -0,0 +1,70 @@ +require 'rails_helper' + +describe Poll::WhiteResult do + + describe "logging changes" do + it "should update amount_log if amount changes" do + white_result = create(:poll_white_result, amount: 33) + + expect(white_result.amount_log).to eq("") + + white_result.amount = 33 + white_result.save + white_result.amount = 32 + white_result.save + white_result.amount = 34 + white_result.save + + expect(white_result.amount_log).to eq(":33:32") + end + + it "should update officer_assignment_id_log if amount changes" do + white_result = create(:poll_white_result, amount: 33) + + expect(white_result.amount_log).to eq("") + expect(white_result.officer_assignment_id_log).to eq("") + + white_result.amount = 33 + white_result.officer_assignment_id = 1 + white_result.save + + white_result.amount = 32 + white_result.officer_assignment_id = 2 + white_result.save + + white_result.amount = 34 + white_result.officer_assignment_id = 3 + white_result.save + + expect(white_result.amount_log).to eq(":33:32") + expect(white_result.officer_assignment_id_log).to eq(":1:2") + end + + it "should update author_id if amount changes" do + white_result = create(:poll_white_result, amount: 33) + + expect(white_result.amount_log).to eq("") + expect(white_result.author_id_log).to eq("") + + author_A = create(:poll_officer).user + author_B = create(:poll_officer).user + author_C = create(:poll_officer).user + + white_result.amount = 33 + white_result.author_id = author_A.id + white_result.save! + + white_result.amount = 32 + white_result.author_id = author_B.id + white_result.save! + + white_result.amount = 34 + white_result.author_id = author_C.id + white_result.save! + + expect(white_result.amount_log).to eq(":33:32") + expect(white_result.author_id_log).to eq(":#{author_A.id}:#{author_B.id}") + end + end + +end \ No newline at end of file