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/models/poll/null_result.rb b/app/models/poll/null_result.rb
new file mode 100644
index 000000000..222432c7f
--- /dev/null
+++ b/app/models/poll/null_result.rb
@@ -0,0 +1,23 @@
+class Poll::NullResult < ActiveRecord::Base
+
+ VALID_ORIGINS = %w{ web booth }
+
+ belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
+ belongs_to :booth_assignment
+ belongs_to :officer_assignment
+
+ validates :author, presence: true
+ validates :origin, inclusion: {in: VALID_ORIGINS}
+
+ scope :by_author, -> (author_id) { where(author_id: author_id) }
+
+ before_save :update_logs
+
+ def update_logs
+ if self.amount_changed? && self.amount_was.present?
+ self.amount_log += ":#{self.amount_was.to_s}"
+ self.officer_assignment_id_log += ":#{self.officer_assignment_id_was.to_s}"
+ self.author_id_log += ":#{self.author_id_was.to_s}"
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/models/poll/white_result.rb b/app/models/poll/white_result.rb
new file mode 100644
index 000000000..5b0aa4966
--- /dev/null
+++ b/app/models/poll/white_result.rb
@@ -0,0 +1,23 @@
+class Poll::WhiteResult < ActiveRecord::Base
+
+ VALID_ORIGINS = %w{ web booth }
+
+ belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
+ belongs_to :booth_assignment
+ belongs_to :officer_assignment
+
+ validates :author, presence: true
+ validates :origin, inclusion: {in: VALID_ORIGINS}
+
+ scope :by_author, -> (author_id) { where(author_id: author_id) }
+
+ before_save :update_logs
+
+ def update_logs
+ if self.amount_changed? && self.amount_was.present?
+ self.amount_log += ":#{self.amount_was.to_s}"
+ self.officer_assignment_id_log += ":#{self.officer_assignment_id_was.to_s}"
+ self.author_id_log += ":#{self.author_id_was.to_s}"
+ end
+ end
+end
\ No newline at end of file
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/db/migrate/20170203163304_create_poll_white_results.rb b/db/migrate/20170203163304_create_poll_white_results.rb
new file mode 100644
index 000000000..2ebfc93b1
--- /dev/null
+++ b/db/migrate/20170203163304_create_poll_white_results.rb
@@ -0,0 +1,18 @@
+class CreatePollWhiteResults < ActiveRecord::Migration
+ def change
+ create_table :poll_white_results do |t|
+ t.integer :author_id
+ t.integer :amount
+ t.string :origin
+ t.date :date
+ t.integer :booth_assignment_id
+ t.integer :officer_assignment_id
+ t.text :amount_log, default: ""
+ t.text :officer_assignment_id_log, default: ""
+ t.text :author_id_log, default: ""
+ end
+
+ add_index :poll_white_results, :officer_assignment_id
+ add_index :poll_white_results, :booth_assignment_id
+ end
+end
diff --git a/db/migrate/20170203163317_create_poll_null_results.rb b/db/migrate/20170203163317_create_poll_null_results.rb
new file mode 100644
index 000000000..9c138cf65
--- /dev/null
+++ b/db/migrate/20170203163317_create_poll_null_results.rb
@@ -0,0 +1,18 @@
+class CreatePollNullResults < ActiveRecord::Migration
+ def change
+ create_table :poll_null_results do |t|
+ t.integer :author_id
+ t.integer :amount
+ t.string :origin
+ t.date :date
+ t.integer :booth_assignment_id
+ t.integer :officer_assignment_id
+ t.text :amount_log, default: ""
+ t.text :officer_assignment_id_log, default: ""
+ t.text :author_id_log, default: ""
+ end
+
+ add_index :poll_null_results, :officer_assignment_id
+ add_index :poll_null_results, :booth_assignment_id
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 95028b472..b44907334 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170202151151) do
+ActiveRecord::Schema.define(version: 20170203163317) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -411,6 +411,21 @@ ActiveRecord::Schema.define(version: 20170202151151) do
add_index "poll_final_recounts", ["booth_assignment_id"], name: "index_poll_final_recounts_on_booth_assignment_id", using: :btree
+ create_table "poll_null_results", force: :cascade do |t|
+ t.integer "author_id"
+ t.integer "amount"
+ t.string "origin"
+ t.date "date"
+ t.integer "booth_assignment_id"
+ t.integer "officer_assignment_id"
+ t.text "amount_log", default: ""
+ t.text "officer_assignment_id_log", default: ""
+ t.text "author_id_log", default: ""
+ end
+
+ add_index "poll_null_results", ["booth_assignment_id"], name: "index_poll_null_results_on_booth_assignment_id", using: :btree
+ add_index "poll_null_results", ["officer_assignment_id"], name: "index_poll_null_results_on_officer_assignment_id", using: :btree
+
create_table "poll_officer_assignments", force: :cascade do |t|
t.integer "booth_assignment_id"
t.integer "officer_id"
@@ -498,6 +513,21 @@ ActiveRecord::Schema.define(version: 20170202151151) do
add_index "poll_voters", ["poll_id"], name: "index_poll_voters_on_poll_id", using: :btree
add_index "poll_voters", ["user_id"], name: "index_poll_voters_on_user_id", using: :btree
+ create_table "poll_white_results", force: :cascade do |t|
+ t.integer "author_id"
+ t.integer "amount"
+ t.string "origin"
+ t.date "date"
+ t.integer "booth_assignment_id"
+ t.integer "officer_assignment_id"
+ t.text "amount_log", default: ""
+ t.text "officer_assignment_id_log", default: ""
+ t.text "author_id_log", default: ""
+ end
+
+ add_index "poll_white_results", ["booth_assignment_id"], name: "index_poll_white_results_on_booth_assignment_id", using: :btree
+ add_index "poll_white_results", ["officer_assignment_id"], name: "index_poll_white_results_on_officer_assignment_id", using: :btree
+
create_table "polls", force: :cascade do |t|
t.string "name"
t.datetime "starts_at"
diff --git a/spec/factories.rb b/spec/factories.rb
index 0e94ecf88..70e3ad01c 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -466,6 +466,16 @@ FactoryGirl.define do
answer { question.valid_answers.sample }
end
+ factory :poll_white_result, class: 'Poll::WhiteResult' do
+ association :author, factory: :user
+ origin { 'web' }
+ end
+
+ factory :poll_null_result, class: 'Poll::NullResult' do
+ association :author, factory: :user
+ origin { 'web' }
+ end
+
factory :officing_residence, class: 'Officing::Residence' do
user
document_number
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