diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 4ab83332b..a48c1e194 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -11,8 +11,8 @@ class Polls::QuestionsController < ApplicationController @comment_tree = CommentTree.new(@commentable, params[:page], @current_order) set_comment_flags(@comment_tree.comments) - #@question_answer = @question.answers.where(author_id: current_user.try(:id)).first - @answers_by_question_id = {@question.id => params[:answer]} + question_answer = @question.partial_results.where(author_id: current_user.try(:id)).first + @answers_by_question_id = {@question.id => question_answer.try(:answer)} end def answer diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 350f19eae..7964727be 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -47,8 +47,7 @@ class Poll::Question < ActiveRecord::Base end def answerable_by?(user) - true - #poll.answerable_by?(user) && (self.all_geozones || self.geozone_ids.include?(user.geozone_id)) + poll.answerable_by?(user) && (self.all_geozones || self.geozone_ids.include?(user.geozone_id)) end def self.answerable_by(user) diff --git a/app/views/polls/questions/_answers.html.erb b/app/views/polls/questions/_answers.html.erb index ee76706fc..16d3dcb01 100644 --- a/app/views/polls/questions/_answers.html.erb +++ b/app/views/polls/questions/_answers.html.erb @@ -1,23 +1,46 @@ -
+
<% if can? :answer, question %>
<% question.valid_answers.each do |answer| %> <% if @answers_by_question_id[question.id] == answer %> - + "> <%= answer %> <% else %> <%= link_to answer, - answer_poll_question_path(poll_id: question.poll_id, id: question.id, answer: answer), - method: :post, - remote: true, - class: "button secondary hollow" %> + answer_question_path(question, answer: answer), + method: :post, + remote: true, + title: t("poll_questions.show.vote_answer", answer: answer), + class: "button secondary hollow" %> <% end %> <% end %>
<% else %> - <% question.valid_answers.each do |answer| %> - <%= answer %> - <% end %> +
+ <% if current_user.nil? %> +
+ <%= t("poll_questions.show.not_logged_in", + signin: link_to(t("poll_questions.show.signin"), new_user_session_path, class: "probe-message"), + signup: link_to(t("poll_questions.show.signup"), new_user_registration_path, class: "probe-message")).html_safe %> +
+ <% elsif current_user.unverified? %> +
+ <%= t('poll_questions.show.cant_answer_verify_html', + verify_link: link_to(t('poll_questions.show.verify_link'), verification_path)) %> +
+ <% else %> +
+ <%= t('poll_questions.show.cant_answer_wrong_geozone') %> +
+ <% end %> +
+
+ <% question.valid_answers.each do |answer| %> + <%= answer %> + <% end %> +
+
+
<% end %> -
+
\ No newline at end of file diff --git a/spec/features/admin/poll/questions_spec.rb b/spec/features/admin/poll/questions_spec.rb index 0fa57003e..ffb128edd 100644 --- a/spec/features/admin/poll/questions_spec.rb +++ b/spec/features/admin/poll/questions_spec.rb @@ -52,7 +52,7 @@ feature 'Admin enquiries' do expect(page).to have_content(summary) end - scenario 'Create from successful proposal', :focus do + scenario 'Create from successful proposal' do geozones = create_list(:geozone, 3) proposal = create(:proposal, :successful) diff --git a/spec/features/polls_spec.rb b/spec/features/polls/polls_spec.rb similarity index 97% rename from spec/features/polls_spec.rb rename to spec/features/polls/polls_spec.rb index 9270d5696..fceb382da 100644 --- a/spec/features/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 require 'rails_helper' feature 'Polls' do @@ -95,7 +94,7 @@ feature 'Polls' do expect(page).to_not have_link('Chewbacca') end - scenario 'Level 2 users in an incoming question' do + scenario 'Level 2 users in an incoming poll' do incoming_poll = create(:poll, :incoming) create(:poll_question, poll: incoming_poll, geozone_ids: [geozone.id], valid_answers: 'Rey, Finn') login_as(create(:user, :level_two, geozone: geozone)) @@ -110,7 +109,7 @@ feature 'Polls' do expect(page).to have_content('This poll has not yet started') end - scenario 'Level 2 users in an expired question' do + scenario 'Level 2 users in an expired poll' do expired_poll = create(:poll, :expired) create(:poll_question, poll: expired_poll, geozone_ids: [geozone.id], valid_answers: 'Luke, Leia') login_as(create(:user, :level_two, geozone: geozone)) @@ -161,6 +160,7 @@ feature 'Polls' do question = create(:poll_question, poll: poll, geozone_ids:[geozone.id], valid_answers: 'Han Solo, Chewbacca') user = create(:user, :level_two, geozone: geozone) create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca') + login_as user visit poll_path(poll) @@ -182,6 +182,4 @@ feature 'Polls' do end end - -end - +end \ No newline at end of file diff --git a/spec/features/polls/questions_spec.rb b/spec/features/polls/questions_spec.rb new file mode 100644 index 000000000..e55ffb05d --- /dev/null +++ b/spec/features/polls/questions_spec.rb @@ -0,0 +1,95 @@ +require 'rails_helper' + +feature 'Poll Questions' do + + scenario 'Lists enquiries from proposals before regular enquiries' do + poll = create(:poll) + normal_question = create(:poll_question, poll: poll) + proposal_question = create(:poll_question, proposal: create(:proposal), poll: poll) + + visit poll_path(poll) + + expect(proposal_question.title).to appear_before(normal_question.title) + end + + context 'Answering' do + let(:geozone) { create(:geozone) } + + scenario 'Non-logged in users' do + question = create(:poll_question, valid_answers: 'Han Solo, Chewbacca') + + visit question_path(question) + + 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).to_not have_link('Han Solo') + expect(page).to_not have_link('Chewbacca') + end + + scenario 'Level 1 users' do + question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca') + + login_as(create(:user, geozone: geozone)) + visit question_path(question) + + expect(page).to have_content('Han Solo') + expect(page).to have_content('Chewbacca') + expect(page).to have_content('You must verify your account in order to answer') + + expect(page).to_not have_link('Han Solo') + expect(page).to_not have_link('Chewbacca') + end + + scenario 'Level 2 users in an enquiry for a geozone which is not theirs' do + question = create(:poll_question, geozone_ids: [], valid_answers: 'Vader, Palpatine') + + login_as(create(:user, :level_two)) + visit question_path(question) + + expect(page).to have_content('Vader') + expect(page).to have_content('Palpatine') + expect(page).to_not have_link('Vader') + expect(page).to_not have_link('Palpatine') + end + + scenario 'Level 2 users who can answer' do + question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca') + + login_as(create(:user, :level_two, geozone: geozone)) + visit question_path(question) + + expect(page).to have_link('Han Solo') + expect(page).to have_link('Chewbacca') + end + + scenario 'Level 2 users who have already answered' do + question = create(:poll_question, geozone_ids:[geozone.id], valid_answers: 'Han Solo, Chewbacca') + + user = create(:user, :level_two, geozone: geozone) + create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca') + + login_as user + visit question_path(question) + + expect(page).to have_link('Han Solo') + expect(page).to_not have_link('Chewbacca') + expect(page).to have_content('Chewbacca') + end + + scenario 'Level 2 users answering', :js do + question = create(:poll_question, geozone_ids: [geozone.id], 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') + expect(page).to have_link('Chewbacca') + end + + end +end \ No newline at end of file