diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 28ce9c447..9c83bc3e3 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -9,10 +9,14 @@ class PollsController < ApplicationController end def show - questions = @poll.questions.sort_for_list.for_render + @answerable_questions = @poll.questions.answerable_by(current_user).for_render.sort_for_list + @non_answerable_questions = @poll.questions.where.not(id: @answerable_questions.map(&:id)).for_render.sort_for_list - @answerable_questions = questions.answerable_by(current_user) - @non_answerable_questions = questions.where.not(id: @answerable_questions.pluck(:id)) + @answers_by_question_id = {} + poll_partial_results = Poll::PartialResult.by_question(@poll.question_ids).by_author(current_user.try(:id)) + poll_partial_results.each do |result| + @answers_by_question_id[result.question_id] = result.answer + end end end diff --git a/app/views/poll/questions/_answers.html.erb b/app/views/poll/questions/_answers.html.erb new file mode 100644 index 000000000..94eed7045 --- /dev/null +++ b/app/views/poll/questions/_answers.html.erb @@ -0,0 +1,23 @@ +
+ <% 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, question_id: question.id, answer: answer), + method: :post, + remote: true, + class: "button secondary hollow" %> + <% end %> + <% end %> +
+ <% else %> + <% question.valid_answers.each do |answer| %> + <%= answer %> + <% end %> + <% end %> +
diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb index a51f280fb..6282b112f 100644 --- a/app/views/polls/show.html.erb +++ b/app/views/polls/show.html.erb @@ -29,9 +29,9 @@
<%= question.title %> - <% question.valid_answers.each do |valid_answer| %> - <%= link_to valid_answer %> - <% end %> +
+ <%= render 'poll/questions/answers', question: question %> +
<% end %> @@ -46,8 +46,8 @@
<%= question.title %> - <% question.valid_answers.each do |valid_answer| %> - <%= valid_answer %> - <% end %> +
+ <%= render 'poll/questions/answers', question: question %> +
<% end %>