Add new model Poll Total Result with specs and relations
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -10,5 +10,6 @@ class Poll
|
||||
has_many :partial_results
|
||||
has_many :white_results
|
||||
has_many :null_results
|
||||
has_many :total_results
|
||||
end
|
||||
end
|
||||
|
||||
23
app/models/poll/total_result.rb
Normal file
23
app/models/poll/total_result.rb
Normal file
@@ -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
|
||||
18
db/migrate/20170914114427_create_poll_total_results.rb
Normal file
18
db/migrate/20170914114427_create_poll_total_results.rb
Normal file
@@ -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
|
||||
17
db/schema.rb
17
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"
|
||||
|
||||
@@ -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
|
||||
|
||||
70
spec/models/poll/total_result_spec.rb
Normal file
70
spec/models/poll/total_result_spec.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user