adds changes logging to poll partial results

This commit is contained in:
Juanjo Bazán
2017-02-02 16:26:21 +01:00
parent eed03d0e0e
commit f060969422
5 changed files with 87 additions and 2 deletions

View File

@@ -15,4 +15,14 @@ class Poll::PartialResult < ActiveRecord::Base
scope :by_author, -> (author_id) { where(author_id: author_id) } scope :by_author, -> (author_id) { where(author_id: author_id) }
scope :by_question, -> (question_id) { where(question_id: question_id) } scope :by_question, -> (question_id) { where(question_id: question_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 end

View File

@@ -0,0 +1,7 @@
class AddLogFieldsToPollPartialResults < ActiveRecord::Migration
def change
add_column :poll_partial_results, :amount_log, :text, default: ""
add_column :poll_partial_results, :officer_assignment_id_log, :text, default: ""
add_column :poll_partial_results, :author_id_log, :text, default: ""
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170201113206) do ActiveRecord::Schema.define(version: 20170202151151) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -433,6 +433,9 @@ ActiveRecord::Schema.define(version: 20170201113206) do
t.date "date" t.date "date"
t.integer "booth_assignment_id" t.integer "booth_assignment_id"
t.integer "officer_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 end
add_index "poll_partial_results", ["answer"], name: "index_poll_partial_results_on_answer", using: :btree add_index "poll_partial_results", ["answer"], name: "index_poll_partial_results_on_answer", using: :btree

View File

@@ -84,7 +84,7 @@ feature 'Officing Results' do
visit officing_poll_results_path(@poll, date: I18n.l(partial_result.date), booth_assignment_id: partial_result.booth_assignment_id) visit officing_poll_results_path(@poll, date: I18n.l(partial_result.date), booth_assignment_id: partial_result.booth_assignment_id)
expect(page).to have_content('7777') within("#question_#{@question_1.id}_0_result") { expect(page).to have_content('7777') }
visit new_officing_poll_result_path(@poll) visit new_officing_poll_result_path(@poll)

View File

@@ -13,4 +13,69 @@ describe Poll::PartialResult do
end end
end end
describe "logging changes" do
it "should update amount_log if amount changes" do
partial_result = create(:poll_partial_result, amount: 33)
expect(partial_result.amount_log).to eq("")
partial_result.amount = 33
partial_result.save
partial_result.amount = 32
partial_result.save
partial_result.amount = 34
partial_result.save
expect(partial_result.amount_log).to eq(":33:32")
end
it "should update officer_assignment_id_log if amount changes" do
partial_result = create(:poll_partial_result, amount: 33)
expect(partial_result.amount_log).to eq("")
expect(partial_result.officer_assignment_id_log).to eq("")
partial_result.amount = 33
partial_result.officer_assignment_id = 1
partial_result.save
partial_result.amount = 32
partial_result.officer_assignment_id = 2
partial_result.save
partial_result.amount = 34
partial_result.officer_assignment_id = 3
partial_result.save
expect(partial_result.amount_log).to eq(":33:32")
expect(partial_result.officer_assignment_id_log).to eq(":1:2")
end
it "should update author_id if amount changes" do
partial_result = create(:poll_partial_result, amount: 33)
expect(partial_result.amount_log).to eq("")
expect(partial_result.author_id_log).to eq("")
author_A = create(:poll_officer).user
author_B = create(:poll_officer).user
author_C = create(:poll_officer).user
partial_result.amount = 33
partial_result.author_id = author_A.id
partial_result.save!
partial_result.amount = 32
partial_result.author_id = author_B.id
partial_result.save!
partial_result.amount = 34
partial_result.author_id = author_C.id
partial_result.save!
expect(partial_result.amount_log).to eq(":33:32")
expect(partial_result.author_id_log).to eq(":#{author_A.id}:#{author_B.id}")
end
end
end end