From 54fbae6339db6aa2f671e4332fe53b6d006d062f Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Wed, 8 Feb 2017 13:49:14 +0100 Subject: [PATCH 1/2] adds links to login or verification on question answers --- app/views/polls/questions/_answers.html.erb | 10 +++++++++- spec/features/polls/polls_spec.rb | 14 ++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/views/polls/questions/_answers.html.erb b/app/views/polls/questions/_answers.html.erb index 7d9bc4cac..206896b04 100644 --- a/app/views/polls/questions/_answers.html.erb +++ b/app/views/polls/questions/_answers.html.erb @@ -17,8 +17,16 @@ class: "button secondary hollow js-question-answer" %> <% end %> <% end %> + <% elsif !user_signed_in? %> + <% question.question_answers.order(id: :desc).each do |answer| %> + <%= link_to answer.title, new_user_session_path, class: "button secondary hollow" %> + <% end %> + <% elsif !current_user.level_two_or_three_verified? %> + <% question.question_answers.order(id: :desc).each do |answer| %> + <%= link_to answer.title, verification_path, class: "button secondary hollow" %> + <% end %> <% else %> - <% question.question_answers.each do |answer| %> + <% question.question_answers.order(id: :desc).each do |answer| %> <%= answer.title %> <% end %> <% end %> diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index b95bb8129..a75afb8a5 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -179,12 +179,9 @@ feature 'Polls' do visit poll_path(poll) - expect(page).to have_content('Han Solo') - expect(page).to have_content('Chewbacca') expect(page).to have_content('You must Sign in or Sign up to participate') - - expect(page).not_to have_link('Han Solo') - expect(page).not_to have_link('Chewbacca') + expect(page).to have_link('Han Solo', href: new_user_session_path) + expect(page).to have_link('Chewbacca', href: new_user_session_path) end scenario 'Level 1 users' do @@ -203,11 +200,8 @@ feature 'Polls' do expect(page).to have_content('You must verify your account in order to answer') - expect(page).to have_content('Han Solo') - expect(page).to have_content('Chewbacca') - - expect(page).not_to have_link('Han Solo') - expect(page).not_to have_link('Chewbacca') + expect(page).to have_link('Han Solo', href: verification_path) + expect(page).to have_link('Chewbacca', href: verification_path) end scenario 'Level 2 users in an incoming poll' do From b601f6c33f6ec86045e93e476f2fc96ea4b90664 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 8 Feb 2017 20:35:33 +0100 Subject: [PATCH 2/2] adds method voted_by?(user) to polls --- app/models/poll.rb | 4 ++++ spec/models/poll/poll_spec.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/poll.rb b/app/models/poll.rb index 38c1a7d56..35b888c90 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -90,6 +90,10 @@ class Poll < ActiveRecord::Base Poll::Voter.where(poll: self, user: user, origin: "web").exists? end + def voted_by?(user) + Poll::Voter.where(poll: self, user: user).exists? + end + def date_range unless starts_at.present? && ends_at.present? && starts_at <= ends_at errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range')) diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index d9ebaa4cd..f3c114155 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -175,6 +175,24 @@ describe Poll do end end + describe "#voted_by?" do + it "return false if the user has not voted for this poll" do + user = create(:user, :level_two) + poll = create(:poll) + + expect(poll.voted_by?(user)).to eq(false) + end + + it "returns true if the user has voted for this poll" do + user = create(:user, :level_two) + poll = create(:poll) + + voter = create(:poll_voter, user: user, poll: poll) + + expect(poll.voted_by?(user)).to eq(true) + end + end + describe "#voted_in_booth?" do it "returns true if the user has already voted in booth" do