diff --git a/app/models/poll/partial_result.rb b/app/models/poll/partial_result.rb index 037395277..e42589a03 100644 --- a/app/models/poll/partial_result.rb +++ b/app/models/poll/partial_result.rb @@ -15,4 +15,14 @@ class Poll::PartialResult < ActiveRecord::Base scope :by_author, -> (author_id) { where(author_id: author_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 \ No newline at end of file diff --git a/db/migrate/20170202151151_add_log_fields_to_poll_partial_results.rb b/db/migrate/20170202151151_add_log_fields_to_poll_partial_results.rb new file mode 100644 index 000000000..86966aed4 --- /dev/null +++ b/db/migrate/20170202151151_add_log_fields_to_poll_partial_results.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 13ad1dce7..95028b472 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: 20170201113206) do +ActiveRecord::Schema.define(version: 20170202151151) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -433,6 +433,9 @@ ActiveRecord::Schema.define(version: 20170201113206) do 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_partial_results", ["answer"], name: "index_poll_partial_results_on_answer", using: :btree diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index 141bcd687..586ed387e 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -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) - 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) diff --git a/spec/models/poll/partial_result_spec.rb b/spec/models/poll/partial_result_spec.rb index 1d203862b..70651eda6 100644 --- a/spec/models/poll/partial_result_spec.rb +++ b/spec/models/poll/partial_result_spec.rb @@ -13,4 +13,69 @@ describe Poll::PartialResult do 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