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:
Juanjo Bazán
2017-01-25 14:21:03 +01:00
parent 086f4a4170
commit b13a76963a
8 changed files with 73 additions and 25 deletions

View File

@@ -19,6 +19,7 @@ class Polls::QuestionsController < ApplicationController
answer.answer = params[:answer] answer.answer = params[:answer]
answer.save! answer.save!
answer.record_voter_participation
@answers_by_question_id = {@question.id => params[:answer]} @answers_by_question_id = {@question.id => params[:answer]}
end end

View File

@@ -3,6 +3,10 @@ class Poll::Answer < ActiveRecord::Base
belongs_to :question, -> { with_hidden } belongs_to :question, -> { with_hidden }
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' 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 :question, presence: true
validates :author, presence: true validates :author, presence: true
validates :answer, 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_author, -> (author_id) { where(author_id: author_id) }
scope :by_question, -> (question_id) { where(question_id: question_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 end

View File

@@ -138,7 +138,7 @@ en:
attributes: attributes:
document_number: document_number:
not_in_census: "Document not in census" not_in_census: "Document not in census"
has_voted: "%{name} has already voted" has_voted: "User has already voted"
proposal: proposal:
attributes: attributes:
tag_list: tag_list:

View File

@@ -421,7 +421,11 @@ FactoryGirl.define do
end end
factory :poll_voter, class: 'Poll::Voter' do factory :poll_voter, class: 'Poll::Voter' do
poll
trait :from_booth do
association :booth_assignment, factory: :poll_booth_assignment association :booth_assignment, factory: :poll_booth_assignment
end
trait :valid_document do trait :valid_document do
document_type "1" document_type "1"
@@ -436,7 +440,7 @@ FactoryGirl.define do
factory :poll_answer, class: 'Poll::Answer' do factory :poll_answer, class: 'Poll::Answer' do
association :question, factory: :poll_question association :question, factory: :poll_question
association :author, factory: :user association :author, factory: [:user, :level_three]
answer { question.valid_answers.sample } answer { question.valid_answers.sample }
end end

View File

@@ -93,5 +93,21 @@ feature 'Poll Questions' do
expect(page).to have_link('Chewbacca') expect(page).to have_link('Chewbacca')
end 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
end end

View File

@@ -13,4 +13,18 @@ describe Poll::Answer do
end end
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 end

View File

@@ -64,15 +64,14 @@ describe :poll do
describe "#document_has_voted?" do describe "#document_has_voted?" do
it "returns true if Poll::Voter with document exists" 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, poll: poll)
voter = create(:poll_voter, :valid_document, booth_assignment: booth_assignment)
expect(poll.document_has_voted?(voter.document_number, voter.document_type)).to eq(true) expect(poll.document_has_voted?(voter.document_number, voter.document_type)).to eq(true)
end end
it "returns false if Poll::Voter with document does not exists" do it "returns false if Poll::Voter with document does not exists" do
booth_assignment = create(:poll_booth_assignment) poll_2 = create(:poll)
voter = create(:poll_voter, :valid_document, booth_assignment: booth_assignment) voter = create(:poll_voter, :valid_document, poll: poll_2)
expect(poll.document_has_voted?(voter.document_number, voter.document_type)).to eq(false) expect(poll.document_has_voted?(voter.document_number, voter.document_type)).to eq(false)
end end

View File

@@ -8,36 +8,37 @@ describe :voter do
describe "validations" do describe "validations" do
it "should be valid if in census and has not voted" do it "should be valid if has not voted" do
voter = build(:poll_voter, :valid_document, booth_assignment: booth_assignment) voter = build(:poll_voter, :valid_document)
expect(voter).to be_valid expect(voter).to be_valid
end end
it "should not be valid if the user is not in the census" do it "should not be valid if the user has already voted in the same poll or booth_assignment" do
voter = build(:poll_voter, :invalid_document, booth_assignment: booth_assignment) voter1 = create(:poll_voter, :valid_document, poll: poll)
voter2 = build(:poll_voter, :valid_document, poll: poll)
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)
expect(voter2).to_not be_valid 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 end
it "should not be valid if the user has already voted in different booth in the same poll" do 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_assignment1 = create(:poll_booth_assignment, poll: poll)
booth_assignment2 = create(:poll_booth_assignment, poll: poll) booth_assignment2 = create(:poll_booth_assignment, poll: poll)
voter1 = create(:poll_voter, :valid_document, booth_assignment: booth_assignment1) voter1 = create(:poll_voter, :valid_document, poll: poll, booth_assignment: booth_assignment1)
voter2 = build(:poll_voter, :valid_document, booth_assignment: booth_assignment2) voter2 = build(:poll_voter, :valid_document, poll: poll, booth_assignment: booth_assignment2)
expect(voter2).to_not be_valid 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 end
it "should be valid if the user has already voted in the same booth in different poll" do 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 expect(voter2).to be_valid
end end
xit "should not be valid if the user has voted via web" do it "should not be valid if the user has voted via web" do
pending "Implementation for voting via web" 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
end end