diff --git a/app/models/poll/voter.rb b/app/models/poll/voter.rb index da05b1bc6..1e4a0d204 100644 --- a/app/models/poll/voter.rb +++ b/app/models/poll/voter.rb @@ -12,6 +12,8 @@ class Poll validates :poll_id, presence: true validates :user_id, presence: true + validates :booth_assignment_id, presence: true, if: ->(voter) { voter.origin == "booth" } + validates :officer_assignment_id, presence: true, if: ->(voter) { voter.origin == "booth" } validates :document_number, presence: true, uniqueness: { scope: [:poll_id, :document_type], message: :has_voted } validates :origin, inclusion: { in: VALID_ORIGINS } diff --git a/spec/models/poll/voter_spec.rb b/spec/models/poll/voter_spec.rb index 85c72aa7d..3ebab9818 100644 --- a/spec/models/poll/voter_spec.rb +++ b/spec/models/poll/voter_spec.rb @@ -6,6 +6,7 @@ describe Poll::Voter do let(:booth) { create(:poll_booth) } let(:booth_assignment) { create(:poll_booth_assignment, poll: poll, booth: booth) } let(:voter) { create(:poll_voter) } + let(:officer_assignment) { create(:poll_officer_assignment) } describe "validations" do @@ -97,6 +98,7 @@ describe Poll::Voter do it "is valid with a booth origin" do voter.origin = "booth" + voter.officer_assignment = officer_assignment expect(voter).to be_valid end @@ -104,7 +106,27 @@ describe Poll::Voter do voter.origin = "web" expect(voter).to be_valid end + end + context "assignments" do + it "should not be valid without a booth_assignment_id when origin is booth" do + voter.origin = "booth" + voter.booth_assignment_id = nil + expect(voter).not_to be_valid + end + + it "should not be valid without an officer_assignment_id when origin is booth" do + voter.origin = "booth" + voter.officer_assignment_id = nil + expect(voter).not_to be_valid + end + + it "should be valid without assignments when origin is web" do + voter.origin = "web" + voter.booth_assignment_id = nil + voter.officer_assignment_id = nil + expect(voter).to be_valid + end end end