From cb1542874241e115f39af6bad5abf7095b29d8f5 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Fri, 6 Oct 2017 19:57:31 +0200 Subject: [PATCH 1/3] Add scenario for re-voting after sing out the same answer --- spec/features/polls/polls_spec.rb | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index c3211e1ab..91b90c23f 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -255,5 +255,39 @@ feature 'Polls' do expect(page).to have_link('Han Solo') end + scenario 'Level 2 votes, signs out, signs in, votes again', :js do + poll.update(geozone_restricted: true) + poll.geozones << geozone + + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') + + user = create(:user, :level_two, geozone: geozone) + + login_as user + visit poll_path(poll) + click_link 'Han Solo' + + expect(page).to_not have_link('Han Solo') + expect(page).to have_link('Chewbacca') + + click_link "Sign out" + login_as user + visit poll_path(poll) + click_link 'Han Solo' + + expect(page).to_not have_link('Han Solo') + expect(page).to have_link('Chewbacca') + + click_link "Sign out" + login_as user + visit poll_path(poll) + click_link 'Chewbacca' + + expect(page).to_not have_link('Chewbacca') + expect(page).to have_link('Han Solo') + end + end end From 81f65f1ac97517933bf32b0c116198d631dbd105 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Fri, 6 Oct 2017 19:57:52 +0200 Subject: [PATCH 2/3] Touch answer model to force the updated_at value update --- app/controllers/polls/questions_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 490b32fbe..7fecefe98 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -9,6 +9,7 @@ class Polls::QuestionsController < ApplicationController answer = @question.answers.find_or_initialize_by(author: current_user) answer.answer = params[:answer] + answer.touch if answer.persisted? answer.save! answer.record_voter_participation From bfef00d478e399160185044390a652aedc80c3fc Mon Sep 17 00:00:00 2001 From: Bertocq Date: Fri, 6 Oct 2017 20:07:57 +0200 Subject: [PATCH 3/3] Refactor voted_before_sign_in logic to avoid no-answer scenario error --- app/helpers/polls_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index c66db216f..b6c0518bb 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -42,7 +42,7 @@ module PollsHelper end def voted_before_sign_in(question) - current_user.current_sign_in_at >= question.answers.find_or_initialize_by(author: current_user).updated_at + question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at >= vote.updated_at } end end