adds origin to poll voters
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
class Poll
|
class Poll
|
||||||
class Voter < ActiveRecord::Base
|
class Voter < ActiveRecord::Base
|
||||||
|
|
||||||
|
VALID_ORIGINS = %w{ web booth }
|
||||||
|
|
||||||
belongs_to :poll
|
belongs_to :poll
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :geozone
|
belongs_to :geozone
|
||||||
@@ -10,9 +13,13 @@ class Poll
|
|||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
|
|
||||||
validates :document_number, presence: true, uniqueness: { scope: [:poll_id, :document_type], message: :has_voted }
|
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
|
before_validation :set_demographic_info, :set_document_info
|
||||||
|
|
||||||
|
scope :web, -> { where(origin: 'web') }
|
||||||
|
scope :booth, -> { where(origin: 'booth') }
|
||||||
|
|
||||||
def set_demographic_info
|
def set_demographic_info
|
||||||
return if user.blank?
|
return if user.blank?
|
||||||
|
|
||||||
|
|||||||
5
db/migrate/20171002121658_add_origin_to_poll_voters.rb
Normal file
5
db/migrate/20171002121658_add_origin_to_poll_voters.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class AddOriginToPollVoters < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :poll_voters, :origin, :string
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -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: 20170927110953) do
|
ActiveRecord::Schema.define(version: 20171002121658) 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"
|
||||||
@@ -733,6 +733,7 @@ ActiveRecord::Schema.define(version: 20170927110953) do
|
|||||||
t.integer "answer_id"
|
t.integer "answer_id"
|
||||||
t.integer "officer_assignment_id"
|
t.integer "officer_assignment_id"
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
|
t.string "origin"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "poll_voters", ["booth_assignment_id"], name: "index_poll_voters_on_booth_assignment_id", using: :btree
|
add_index "poll_voters", ["booth_assignment_id"], name: "index_poll_voters_on_booth_assignment_id", using: :btree
|
||||||
|
|||||||
@@ -525,6 +525,7 @@ FactoryGirl.define do
|
|||||||
factory :poll_voter, class: 'Poll::Voter' do
|
factory :poll_voter, class: 'Poll::Voter' do
|
||||||
poll
|
poll
|
||||||
association :user, :level_two
|
association :user, :level_two
|
||||||
|
origin "web"
|
||||||
|
|
||||||
trait :from_booth do
|
trait :from_booth do
|
||||||
association :booth_assignment, factory: :poll_booth_assignment
|
association :booth_assignment, factory: :poll_booth_assignment
|
||||||
|
|||||||
@@ -83,6 +83,64 @@ describe :voter do
|
|||||||
expect(voter.errors.messages[:document_number]).to eq(["User has already voted"])
|
expect(voter.errors.messages[:document_number]).to eq(["User has already voted"])
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "save" do
|
describe "save" do
|
||||||
|
|||||||
Reference in New Issue
Block a user