From b13a76963af73795c5d27b64c2794c06c4e6e95b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Wed, 25 Jan 2017 14:21:03 +0100 Subject: [PATCH] records participation of user via web MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit answering a poll question creates a voter with the user data to record participation in question’s poll --- app/controllers/polls/questions_controller.rb | 1 + app/models/poll/answer.rb | 8 ++++ config/locales/activerecord.en.yml | 2 +- spec/factories.rb | 8 +++- spec/features/polls/questions_spec.rb | 16 +++++++ spec/models/poll/answer_spec.rb | 14 +++++++ spec/models/poll/poll_spec.rb | 7 ++-- spec/models/poll/voter_spec.rb | 42 +++++++++++-------- 8 files changed, 73 insertions(+), 25 deletions(-) diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index e9c7e2bbd..1849dff97 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -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 diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index b0f2d4410..2d08fefa4 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -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 \ No newline at end of file diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 302b6767a..6a7aebcbd 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -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: diff --git a/spec/factories.rb b/spec/factories.rb index 90a725636..7653f9a3a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/features/polls/questions_spec.rb b/spec/features/polls/questions_spec.rb index e925c8fd6..d6d02b0cb 100644 --- a/spec/features/polls/questions_spec.rb +++ b/spec/features/polls/questions_spec.rb @@ -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 diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index 73707ad80..effc28fde 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -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 diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index e410c31ce..b2ec29e5c 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -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 diff --git a/spec/models/poll/voter_spec.rb b/spec/models/poll/voter_spec.rb index 3a1844f4b..88541dc82 100644 --- a/spec/models/poll/voter_spec.rb +++ b/spec/models/poll/voter_spec.rb @@ -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