diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d359361a4..4704696e5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -287,6 +287,7 @@ Lint/StringConversionInInterpolation: - 'app/models/poll/null_result.rb' - 'app/models/poll/partial_result.rb' - 'app/models/poll/white_result.rb' + - 'app/models/poll/total_result.rb' # Offense count: 15 # Cop supports --auto-correct. @@ -562,6 +563,7 @@ Style/MutableConstant: - 'app/models/poll/null_result.rb' - 'app/models/poll/partial_result.rb' - 'app/models/poll/white_result.rb' + - 'app/models/poll/total_result.rb' - 'app/models/proposal.rb' - 'app/models/signature_sheet.rb' - 'app/models/site_customization/content_block.rb' diff --git a/app/models/poll.rb b/app/models/poll.rb index 4ba313963..3873ac5e0 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -4,6 +4,7 @@ class Poll < ActiveRecord::Base has_many :partial_results, through: :booth_assignments has_many :white_results, through: :booth_assignments has_many :null_results, through: :booth_assignments + has_many :total_results, through: :booth_assignments has_many :voters has_many :officer_assignments, through: :booth_assignments has_many :officers, through: :officer_assignments diff --git a/app/models/poll/booth_assignment.rb b/app/models/poll/booth_assignment.rb index 5ef9d687e..47cb01bfe 100644 --- a/app/models/poll/booth_assignment.rb +++ b/app/models/poll/booth_assignment.rb @@ -10,5 +10,6 @@ class Poll has_many :partial_results has_many :white_results has_many :null_results + has_many :total_results end end diff --git a/app/models/poll/total_result.rb b/app/models/poll/total_result.rb new file mode 100644 index 000000000..2df01929e --- /dev/null +++ b/app/models/poll/total_result.rb @@ -0,0 +1,23 @@ +class Poll::TotalResult < 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 amount_changed? && amount_was.present? + self.amount_log += ":#{amount_was.to_s}" + self.officer_assignment_id_log += ":#{officer_assignment_id_was.to_s}" + self.author_id_log += ":#{author_id_was.to_s}" + end + end +end diff --git a/db/migrate/20170914114427_create_poll_total_results.rb b/db/migrate/20170914114427_create_poll_total_results.rb new file mode 100644 index 000000000..1a733dce4 --- /dev/null +++ b/db/migrate/20170914114427_create_poll_total_results.rb @@ -0,0 +1,18 @@ +class CreatePollTotalResults < ActiveRecord::Migration + def change + create_table :poll_total_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_total_results, :officer_assignment_id + add_index :poll_total_results, :booth_assignment_id + end +end diff --git a/db/schema.rb b/db/schema.rb index ba856107c..fda556a03 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: 20170908175149) do +ActiveRecord::Schema.define(version: 20170914114427) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -691,6 +691,21 @@ ActiveRecord::Schema.define(version: 20170908175149) do add_index "poll_shifts", ["booth_id"], name: "index_poll_shifts_on_booth_id", using: :btree add_index "poll_shifts", ["officer_id"], name: "index_poll_shifts_on_officer_id", using: :btree + create_table "poll_total_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_total_results", ["booth_assignment_id"], name: "index_poll_total_results_on_booth_assignment_id", using: :btree + add_index "poll_total_results", ["officer_assignment_id"], name: "index_poll_total_results_on_officer_assignment_id", using: :btree + create_table "poll_voters", force: :cascade do |t| t.string "document_number" t.string "document_type" diff --git a/spec/factories.rb b/spec/factories.rb index 9a038816a..b1c6e7174 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -556,6 +556,11 @@ FactoryGirl.define do origin { 'web' } end + factory :poll_total_result, class: 'Poll::TotalResult' do + association :author, factory: :user + origin { 'web' } + end + factory :officing_residence, class: 'Officing::Residence' do user association :officer, factory: :poll_officer diff --git a/spec/models/poll/total_result_spec.rb b/spec/models/poll/total_result_spec.rb new file mode 100644 index 000000000..baa877227 --- /dev/null +++ b/spec/models/poll/total_result_spec.rb @@ -0,0 +1,70 @@ +require 'rails_helper' + +describe Poll::TotalResult do + + describe "logging changes" do + it "should update amount_log if amount changes" do + total_result = create(:poll_total_result, amount: 33) + + expect(total_result.amount_log).to eq("") + + total_result.amount = 33 + total_result.save + total_result.amount = 32 + total_result.save + total_result.amount = 34 + total_result.save + + expect(total_result.amount_log).to eq(":33:32") + end + + it "should update officer_assignment_id_log if amount changes" do + total_result = create(:poll_total_result, amount: 33) + + expect(total_result.amount_log).to eq("") + expect(total_result.officer_assignment_id_log).to eq("") + + total_result.amount = 33 + total_result.officer_assignment = create(:poll_officer_assignment, id: 101) + total_result.save + + total_result.amount = 32 + total_result.officer_assignment = create(:poll_officer_assignment, id: 102) + total_result.save + + total_result.amount = 34 + total_result.officer_assignment = create(:poll_officer_assignment, id: 103) + total_result.save + + expect(total_result.amount_log).to eq(":33:32") + expect(total_result.officer_assignment_id_log).to eq(":101:102") + end + + it "should update author_id if amount changes" do + total_result = create(:poll_total_result, amount: 33) + + expect(total_result.amount_log).to eq("") + expect(total_result.author_id_log).to eq("") + + author_A = create(:poll_officer).user + author_B = create(:poll_officer).user + author_C = create(:poll_officer).user + + total_result.amount = 33 + total_result.author_id = author_A.id + total_result.save! + + total_result.amount = 32 + total_result.author_id = author_B.id + total_result.save! + + total_result.amount = 34 + total_result.author_id = author_C.id + total_result.save! + + expect(total_result.amount_log).to eq(":33:32") + expect(total_result.author_id_log).to eq(":#{author_A.id}:#{author_B.id}") + end + end + +end