adds origin to poll voters

This commit is contained in:
rgarcia
2017-10-02 16:18:08 +02:00
parent 60fb142fff
commit 0147401fbe
5 changed files with 73 additions and 1 deletions

View File

@@ -1,5 +1,8 @@
class Poll
class Voter < ActiveRecord::Base
VALID_ORIGINS = %w{ web booth }
belongs_to :poll
belongs_to :user
belongs_to :geozone
@@ -10,9 +13,13 @@ class Poll
validates :user_id, presence: true
validates :document_number, presence: true, uniqueness: { scope: [:poll_id, :document_type], message: :has_voted }
validates :origin, inclusion: { in: VALID_ORIGINS }
before_validation :set_demographic_info, :set_document_info
scope :web, -> { where(origin: 'web') }
scope :booth, -> { where(origin: 'booth') }
def set_demographic_info
return if user.blank?

View File

@@ -0,0 +1,5 @@
class AddOriginToPollVoters < ActiveRecord::Migration
def change
add_column :poll_voters, :origin, :string
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170927110953) do
ActiveRecord::Schema.define(version: 20171002121658) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -733,6 +733,7 @@ ActiveRecord::Schema.define(version: 20170927110953) do
t.integer "answer_id"
t.integer "officer_assignment_id"
t.integer "user_id"
t.string "origin"
end
add_index "poll_voters", ["booth_assignment_id"], name: "index_poll_voters_on_booth_assignment_id", using: :btree

View File

@@ -525,6 +525,7 @@ FactoryGirl.define do
factory :poll_voter, class: 'Poll::Voter' do
poll
association :user, :level_two
origin "web"
trait :from_booth do
association :booth_assignment, factory: :poll_booth_assignment

View File

@@ -83,6 +83,64 @@ describe :voter do
expect(voter.errors.messages[:document_number]).to eq(["User has already voted"])
end
context "origin" do
it "should not be valid without an origin" do
voter.origin = nil
expect(voter).to_not be_valid
end
it "should not be valid without a valid origin" do
voter.origin = "invalid_origin"
expect(voter).to_not be_valid
end
it "should be valid with a booth origin" do
voter.origin = "booth"
expect(voter).to be_valid
end
it "should be valid with a web origin" do
voter.origin = "web"
expect(voter).to be_valid
end
end
end
describe "scopes" do
describe "#web" do
it "returns voters with a web origin" do
voter1 = create(:poll_voter, origin: "web")
voter2 = create(:poll_voter, origin: "web")
voter3 = create(:poll_voter, origin: "booth")
web_voters = Poll::Voter.web
expect(web_voters.count).to eq(2)
expect(web_voters).to include(voter1)
expect(web_voters).to include(voter2)
expect(web_voters).to_not include(voter3)
end
end
describe "#booth" do
it "returns voters with a booth origin" do
voter1 = create(:poll_voter, origin: "booth")
voter2 = create(:poll_voter, origin: "booth")
voter3 = create(:poll_voter, origin: "web")
booth_voters = Poll::Voter.booth
expect(booth_voters.count).to eq(2)
expect(booth_voters).to include(voter1)
expect(booth_voters).to include(voter2)
expect(booth_voters).to_not include(voter3)
end
end
end
describe "save" do