records participation of user via web
answering a poll question creates a voter with the user data to record participation in question’s poll
This commit is contained in:
@@ -19,6 +19,7 @@ class Polls::QuestionsController < ApplicationController
|
||||
|
||||
answer.answer = params[:answer]
|
||||
answer.save!
|
||||
answer.record_voter_participation
|
||||
|
||||
@answers_by_question_id = {@question.id => params[:answer]}
|
||||
end
|
||||
|
||||
@@ -3,6 +3,10 @@ class Poll::Answer < ActiveRecord::Base
|
||||
belongs_to :question, -> { with_hidden }
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
|
||||
has_one :voter
|
||||
|
||||
delegate :poll, :poll_id, to: :question
|
||||
|
||||
validates :question, presence: true
|
||||
validates :author, presence: true
|
||||
validates :answer, presence: true
|
||||
@@ -10,4 +14,8 @@ class Poll::Answer < ActiveRecord::Base
|
||||
|
||||
scope :by_author, -> (author_id) { where(author_id: author_id) }
|
||||
scope :by_question, -> (question_id) { where(question_id: question_id) }
|
||||
|
||||
def record_voter_participation
|
||||
Poll::Voter.create_from_user(author, {poll_id: poll_id, answer_id: id})
|
||||
end
|
||||
end
|
||||
@@ -138,7 +138,7 @@ en:
|
||||
attributes:
|
||||
document_number:
|
||||
not_in_census: "Document not in census"
|
||||
has_voted: "%{name} has already voted"
|
||||
has_voted: "User has already voted"
|
||||
proposal:
|
||||
attributes:
|
||||
tag_list:
|
||||
|
||||
@@ -421,7 +421,11 @@ FactoryGirl.define do
|
||||
end
|
||||
|
||||
factory :poll_voter, class: 'Poll::Voter' do
|
||||
association :booth_assignment, factory: :poll_booth_assignment
|
||||
poll
|
||||
|
||||
trait :from_booth do
|
||||
association :booth_assignment, factory: :poll_booth_assignment
|
||||
end
|
||||
|
||||
trait :valid_document do
|
||||
document_type "1"
|
||||
@@ -436,7 +440,7 @@ FactoryGirl.define do
|
||||
|
||||
factory :poll_answer, class: 'Poll::Answer' do
|
||||
association :question, factory: :poll_question
|
||||
association :author, factory: :user
|
||||
association :author, factory: [:user, :level_three]
|
||||
answer { question.valid_answers.sample }
|
||||
end
|
||||
|
||||
|
||||
@@ -93,5 +93,21 @@ feature 'Poll Questions' do
|
||||
expect(page).to have_link('Chewbacca')
|
||||
end
|
||||
|
||||
scenario 'Records participarion', :js do
|
||||
question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
|
||||
user = create(:user, :level_two, geozone: geozone)
|
||||
|
||||
login_as user
|
||||
visit question_path(question)
|
||||
|
||||
click_link 'Han Solo'
|
||||
|
||||
expect(page).to_not have_link('Han Solo')
|
||||
|
||||
answer = Poll::Answer.by_question(question.id).by_author(user.id).first
|
||||
expect(answer.voter.document_number).to eq(user.document_number)
|
||||
expect(answer.voter.poll_id).to eq(poll.id)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,4 +13,18 @@ describe Poll::Answer do
|
||||
end
|
||||
end
|
||||
|
||||
describe "#record_voter_participation" do
|
||||
it "creates a poll_voter with user and poll data" do
|
||||
answer = create(:poll_answer)
|
||||
expect(answer.voter).to be_nil
|
||||
|
||||
answer.record_voter_participation
|
||||
voter = answer.reload.voter
|
||||
|
||||
expect(voter.answer).to eq(answer)
|
||||
expect(voter.document_number).to eq(answer.author.document_number)
|
||||
expect(voter.poll_id).to eq(answer.poll.id)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -64,15 +64,14 @@ describe :poll do
|
||||
|
||||
describe "#document_has_voted?" do
|
||||
it "returns true if Poll::Voter with document exists" do
|
||||
booth_assignment = create(:poll_booth_assignment, poll: poll)
|
||||
voter = create(:poll_voter, :valid_document, booth_assignment: booth_assignment)
|
||||
voter = create(:poll_voter, :valid_document, poll: poll)
|
||||
|
||||
expect(poll.document_has_voted?(voter.document_number, voter.document_type)).to eq(true)
|
||||
end
|
||||
|
||||
it "returns false if Poll::Voter with document does not exists" do
|
||||
booth_assignment = create(:poll_booth_assignment)
|
||||
voter = create(:poll_voter, :valid_document, booth_assignment: booth_assignment)
|
||||
poll_2 = create(:poll)
|
||||
voter = create(:poll_voter, :valid_document, poll: poll_2)
|
||||
|
||||
expect(poll.document_has_voted?(voter.document_number, voter.document_type)).to eq(false)
|
||||
end
|
||||
|
||||
@@ -8,36 +8,37 @@ describe :voter do
|
||||
|
||||
describe "validations" do
|
||||
|
||||
it "should be valid if in census and has not voted" do
|
||||
voter = build(:poll_voter, :valid_document, booth_assignment: booth_assignment)
|
||||
it "should be valid if has not voted" do
|
||||
voter = build(:poll_voter, :valid_document)
|
||||
|
||||
expect(voter).to be_valid
|
||||
end
|
||||
|
||||
it "should not be valid if the user is not in the census" do
|
||||
voter = build(:poll_voter, :invalid_document, booth_assignment: booth_assignment)
|
||||
|
||||
expect(voter).to_not be_valid
|
||||
expect(voter.errors.messages[:document_number]).to eq(["Document not in census"])
|
||||
end
|
||||
|
||||
it "should not be valid if the user has already voted in the same booth/poll" do
|
||||
voter1 = create(:poll_voter, :valid_document, booth_assignment: booth_assignment)
|
||||
voter2 = build(:poll_voter, :valid_document, booth_assignment: booth_assignment)
|
||||
it "should not be valid if the user has already voted in the same poll or booth_assignment" do
|
||||
voter1 = create(:poll_voter, :valid_document, poll: poll)
|
||||
voter2 = build(:poll_voter, :valid_document, poll: poll)
|
||||
|
||||
expect(voter2).to_not be_valid
|
||||
expect(voter2.errors.messages[:document_number]).to eq(["José García has already voted"])
|
||||
expect(voter2.errors.messages[:document_number]).to eq(["User has already voted"])
|
||||
end
|
||||
|
||||
it "should not be valid if the user has already voted in the same poll/booth" do
|
||||
voter1 = create(:poll_voter, :valid_document, poll: poll, booth_assignment: booth_assignment)
|
||||
voter2 = build(:poll_voter, :valid_document, poll: poll, booth_assignment: booth_assignment)
|
||||
|
||||
expect(voter2).to_not be_valid
|
||||
expect(voter2.errors.messages[:document_number]).to eq(["User has already voted"])
|
||||
end
|
||||
|
||||
it "should not be valid if the user has already voted in different booth in the same poll" do
|
||||
booth_assignment1 = create(:poll_booth_assignment, poll: poll)
|
||||
booth_assignment2 = create(:poll_booth_assignment, poll: poll)
|
||||
|
||||
voter1 = create(:poll_voter, :valid_document, booth_assignment: booth_assignment1)
|
||||
voter2 = build(:poll_voter, :valid_document, booth_assignment: booth_assignment2)
|
||||
voter1 = create(:poll_voter, :valid_document, poll: poll, booth_assignment: booth_assignment1)
|
||||
voter2 = build(:poll_voter, :valid_document, poll: poll, booth_assignment: booth_assignment2)
|
||||
|
||||
expect(voter2).to_not be_valid
|
||||
expect(voter2.errors.messages[:document_number]).to eq(["José García has already voted"])
|
||||
expect(voter2.errors.messages[:document_number]).to eq(["User has already voted"])
|
||||
end
|
||||
|
||||
it "should be valid if the user has already voted in the same booth in different poll" do
|
||||
@@ -50,8 +51,13 @@ describe :voter do
|
||||
expect(voter2).to be_valid
|
||||
end
|
||||
|
||||
xit "should not be valid if the user has voted via web" do
|
||||
pending "Implementation for voting via web"
|
||||
it "should not be valid if the user has voted via web" do
|
||||
answer = create(:poll_answer)
|
||||
answer.record_voter_participation
|
||||
|
||||
voter = build(:poll_voter, poll: answer.question.poll, document_number: answer.author.document_number, document_type: "1")
|
||||
expect(voter).to_not be_valid
|
||||
expect(voter.errors.messages[:document_number]).to eq(["User has already voted"])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user