From c6e4b2480fbe0e0584e91259c51949c1e64ef102 Mon Sep 17 00:00:00 2001 From: lalo Date: Wed, 29 May 2019 18:32:04 +0200 Subject: [PATCH] Add public changes to create and vote Poll:Questions with votation type --- app/assets/javascripts/sortable.js.coffee | 8 + app/assets/stylesheets/participation.scss | 96 +++++++++++- app/controllers/polls/answers_controller.rb | 70 +++++++++ app/controllers/polls/questions_controller.rb | 77 +++++++++- app/controllers/polls_controller.rb | 36 ++++- app/helpers/polls_helper.rb | 4 + app/models/abilities/common.rb | 10 +- app/views/polls/questions/_answers.html.erb | 51 +++---- .../polls/questions/_answers_couples.html.erb | 39 +++++ .../questions/_answers_multiple.html.erb | 36 +++++ .../_answers_positive_negative.html.erb | 55 +++++++ .../questions/_answers_prioritized.html.erb | 44 ++++++ .../polls/questions/_answers_set.html.erb | 37 +++++ .../polls/questions/_answers_unique.html.erb | 33 +++++ .../polls/questions/_like_dislike.html.erb | 19 +++ .../polls/questions/_new_answer.html.erb | 5 + app/views/polls/questions/_question.html.erb | 30 +++- app/views/polls/questions/answer.js.erb | 3 +- app/views/polls/results.html.erb | 38 +++-- app/views/polls/show.html.erb | 139 ++++++++++-------- config/routes/poll.rb | 8 +- spec/models/abilities/common_spec.rb | 12 ++ 22 files changed, 713 insertions(+), 137 deletions(-) create mode 100644 app/controllers/polls/answers_controller.rb create mode 100644 app/views/polls/questions/_answers_couples.html.erb create mode 100644 app/views/polls/questions/_answers_multiple.html.erb create mode 100644 app/views/polls/questions/_answers_positive_negative.html.erb create mode 100644 app/views/polls/questions/_answers_prioritized.html.erb create mode 100644 app/views/polls/questions/_answers_set.html.erb create mode 100644 app/views/polls/questions/_answers_unique.html.erb create mode 100644 app/views/polls/questions/_like_dislike.html.erb create mode 100644 app/views/polls/questions/_new_answer.html.erb diff --git a/app/assets/javascripts/sortable.js.coffee b/app/assets/javascripts/sortable.js.coffee index 61124264e40..ee98703f389 100644 --- a/app/assets/javascripts/sortable.js.coffee +++ b/app/assets/javascripts/sortable.js.coffee @@ -7,3 +7,11 @@ App.Sortable = url: $(".sortable").data("js-url"), data: { ordered_list: new_order }, type: "POST" + + $(".sortable-priotirized-votation").sortable + update: (event, ui) -> + new_order = $(this).sortable("toArray", { attribute: "data-answer-id" }) + $.ajax + url: $(this).data("js-url"), + data: { ordered_list: new_order }, + type: "POST" diff --git a/app/assets/stylesheets/participation.scss b/app/assets/stylesheets/participation.scss index 5a72a100b35..f003c46325e 100644 --- a/app/assets/stylesheets/participation.scss +++ b/app/assets/stylesheets/participation.scss @@ -1745,8 +1745,12 @@ background: #fafafa; border-bottom: 1px solid #eee; - .column:nth-child(odd) { - border-right: 2px solid $text; + .margin-bottom { + margin-bottom: 0; + } + + .orbit-bullets { + margin-bottom: 0; } .answer-divider { @@ -1756,14 +1760,35 @@ padding-bottom: $line-height; } + .answer-left-divider { + border-left: solid 1px $text; + padding-left: rem-calc(10); + } + + .margin-top { + margin-top: rem-calc(10); + } + + .margin-bottom { + margin-bottom: rem-calc(20); + } + .answer-description { - height: 100%; + max-height: rem-calc(1000); &.short { - height: rem-calc(300); + max-height: rem-calc(70); overflow: hidden; + max-width: rem-calc(700); } } + + .question-divider { + border-bottom: rgba(219, 219, 219, 0.62) solid 1px; + margin-bottom: 1rem; + padding: rem-calc(24); + } + } .orbit-bullets button { @@ -1991,6 +2016,69 @@ } } +.icon-like, +.icon-unlike { + background: #fff; + border: 2px solid $text-light; + border-radius: rem-calc(3); + color: $text-light; + display: inline-block; + font-size: rem-calc(30); + line-height: rem-calc(30); + padding: rem-calc(3) rem-calc(6); + position: relative; + + &:hover, + &:active { + color: #fff; + cursor: pointer; + opacity: 1 !important; + } +} + +.active-like { + color: #fff; + cursor: pointer; + opacity: 1 !important; + background: $like; + border: 2px solid $like; +} + +.active-unlike { + color: #fff; + cursor: pointer; + opacity: 1 !important; + background: $unlike; + border: 2px solid $unlike; +} + +.icon-like { + + &:hover, + &:active, + .picked { + background: $like; + border: 2px solid $like; + } +} + +.icon-unlike { + + &:hover, + &:active { + background: $unlike; + border: 2px solid $unlike; + } +} + +.vote-align { + float: right; +} + +.vote-divider { + border-bottom: 1px solid $text-light; +} + // 09. Polls results and stats // --------------------------- diff --git a/app/controllers/polls/answers_controller.rb b/app/controllers/polls/answers_controller.rb new file mode 100644 index 00000000000..49c5fcfc9e3 --- /dev/null +++ b/app/controllers/polls/answers_controller.rb @@ -0,0 +1,70 @@ +class Polls::AnswersController < ApplicationController + + load_and_authorize_resource :poll + load_and_authorize_resource :question, class: "Poll::Question" + authorize_resource :answer, class: "Poll::Answer" + + def create + @question = Poll::Question.find_by(id: params[:id]) + if @question.votation_type.open? && !check_question_answer_exist + @question.question_answers.create( + title: params[:answer], + given_order: @question.question_answers.count + 1, + hidden: false + ) + flash.now[:notice] = t("dashboard.polls.index.succesfull") + else + flash.now[:alert] = "Unfortunately failed to sent" + end + load_for_answers + if @question.enum_type&.include?("answer_couples") + last_pair ||= generate_and_store_new_pair(@question) + @last_pair_question_answers = {@question.id => last_pair} + end + render "polls/questions/answer", format: :js + end + + def delete + @question = Poll::Question.find_by(id: params[:id]) + !@question.answers.find_by(author: current_user, answer: params[:answer]).destroy + @question.question_answers.each do |question_answer| + question_answer.set_most_voted + end + question_answers + load_for_answers + if @question.enum_type&.include?("answer_couples") + last_pair ||= generate_and_store_new_pair(@question) + @last_pair_question_answers = {@question.id => last_pair} + end + render "polls/questions/answer", format: :js + end + + private + + def check_question_answer_exist + exist = false + @question.question_answers.each do |question_answer| + break if exist + exist = true if question_answer.title == params[:answer] + end + exist + end + + def load_for_answers + @page = params[:page].present? ? params[:page] : 1 + question_answers + @answers_by_question_id = {@question.id => @question.answers + .by_author(current_user) + .order(:order) + .pluck(:answer)} + end + + def question_answers + if @question.is_positive_negative? + @answers = @question.question_answers.visibles.page(params[:page]) + else + @answers = @question.question_answers.visibles + end + end + +end diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index eb054dd1ecd..b929e0fc5fa 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -3,18 +3,79 @@ class Polls::QuestionsController < ApplicationController load_and_authorize_resource :poll load_and_authorize_resource :question, class: "Poll::Question" - has_orders %w{most_voted newest oldest}, only: :show + has_orders %w[most_voted newest oldest], only: :show def answer - answer = @question.answers.find_or_initialize_by(author: current_user) - token = params[:token] + answer = store_answer + vote_stored(answer, params[:answer], params[:token]) if answer.present? + load_for_answers + if @question.enum_type&.include?("answer_couples") + last_pair ||= generate_and_store_new_pair(@question) + @last_pair_question_answers = {@question.id => last_pair} + end + end - answer.answer = params[:answer] - answer.touch if answer.persisted? - answer.save! - answer.record_voter_participation(token) + def load_answers + load_for_answers + render action: "answer.js.erb" + end - @answers_by_question_id = { @question.id => params[:answer] } + def prioritized_answers + unless params[:ordered_list].empty? + params[:ordered_list].each_with_index do |answer, i| + answer_obj = @question.votation_type.answer(current_user, + answer, + order: i + 1) + vote_stored(answer_obj, answer, params[:tooken]) if answer_obj.present? + end + @question.votation_type.update_priorized_values(current_user.id) + end + load_for_answers + render action: "answer.js.erb" end + private + + def load_for_answers + @page = params[:page].present? ? params[:page] : 1 + question_answers + @answers_by_question_id = {@question.id => @question.answers + .by_author(current_user) + .order(:order) + .pluck(:answer)} + end + + def vote_stored(answer, new_answer, token) + answer.answer = new_answer + answer.touch if answer.persisted? + answer.save! + answer.record_voter_participation(token) + @question.question_answers.visibles.where(question_id: @question).each do |question_answer| + question_answer.set_most_voted + end + end + + def store_answer + if @question.votation_type.nil? + answer = @question.answers.find_or_initialize_by(author: current_user) + else + answer = @question.votation_type.answer(current_user, + params[:answer], + positive: params[:positive]) + end + answer + end + + def generate_and_store_new_pair(question) + Poll::PairAnswer.generate_pair(question, current_user) + end + + def question_answers + if @question.is_positive_negative? + @answers = @question.question_answers.visibles.page(@page) + else + @answers = @question.question_answers.visibles + end + end + end diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index f322379ceba..b953033841d 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -20,13 +20,27 @@ def index def show @questions = @poll.questions.for_render.sort_for_list @token = poll_voter_token(@poll, current_user) - @poll_questions_answers = Poll::Question::Answer.where(question: @poll.questions) + @poll_questions_answers = Poll::Question::Answer.visibles + .where(question: @poll.questions) .where.not(description: "").order(:given_order) @answers_by_question_id = {} poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id)) - poll_answers.each do |answer| - @answers_by_question_id[answer.question_id] = answer.answer + + @last_pair_question_answers = {} + @questions.each do |question| + @answers_by_question_id[question.id] = question.answers.by_author(current_user).pluck(:answer) + + if question.enum_type&.include?("answer_couples") + last_pair = question.pair_answers.by_author(current_user).first + last_pair ||= generate_and_store_new_pair(question) + @last_pair_question_answers[question.id] = last_pair + end + + if question.enum_type&.include?("answer_set_closed") || + question.enum_type&.include?("answer_set_open") + votation_answer_sets(question) + end end @commentable = @poll @@ -42,6 +56,18 @@ def results private + def votation_answer_sets(question) + if question.votation_type.votation_set_answers.by_author(current_user).empty? + question.question_answers&.sample(question.max_groups_answers).each do |question_answer| + answer = VotationSetAnswer.new(answer: question_answer.title, + votation_type: question.votation_type, + author: current_user) + question.votation_type.votation_set_answers << answer + end + !question.save + end + end + def load_poll @poll = Poll.where(slug: params[:id]).first || Poll.where(id: params[:id]).first end @@ -50,4 +76,8 @@ def load_active_poll @active_poll = ActivePoll.first end + def generate_and_store_new_pair(question) + Poll::PairAnswer.generate_pair(question, current_user) + end + end diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index 2faf9b3f8db..963367a4fbc 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -78,4 +78,8 @@ def info_menu? def show_polls_description? @active_poll.present? && @current_filter == "current" end + + def stored_positive_negative_value(question, answer) + question.answers.find_by(author_id: current_user.id, answer: answer.title).positive + end end diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index b106ebef50e..d26a3868008 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -104,12 +104,18 @@ def initialize(user) can :create, DirectMessage can :show, DirectMessage, sender_id: user.id - can :answer, Poll do |poll| + + can [:load_answers], Poll::Question + can [:answer], Poll do |poll| poll.answerable_by?(user) end - can :answer, Poll::Question do |question| + can [:answer, :prioritized_answers], Poll::Question do |question| question.answerable_by?(user) end + + can [:create, :delete], Poll::Answer do |answer| + answer.question.answerable_by?(user) + end end can [:create, :show], ProposalNotification, proposal: { author_id: user.id } diff --git a/app/views/polls/questions/_answers.html.erb b/app/views/polls/questions/_answers.html.erb index 206896b0419..bfba35b9187 100644 --- a/app/views/polls/questions/_answers.html.erb +++ b/app/views/polls/questions/_answers.html.erb @@ -1,33 +1,22 @@ -
- <% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> - <% question.question_answers.each do |answer| %> - <% if @answers_by_question_id[question.id] == answer.title && - (!voted_before_sign_in(question) || - question.poll.voted_in_booth?(current_user)) %> - "> - <%= answer.title %> - - <% else %> - <%= link_to answer.title, - answer_question_path(question, answer: answer.title, token: token), - method: :post, - remote: true, - title: t("poll_questions.show.vote_answer", answer: answer.title), - 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 %> +<% if question.votation_type.nil? %> + <%= render "polls/questions/answers_unique", question: question, answers: answers, token: token %> +<% else %> + <% case question.votation_type.enum_type %> + <% when "unique" %> + <%= render "polls/questions/answers_unique", question: question, answers: answers, token: token %> + <% when "multiple", "positive_open" %> + <%= render "polls/questions/answers_multiple", question: question, answers: answers, token: token %> + <% when "positive_negative_open" %> + <%= render "polls/questions/answers_positive_negative", question: question, answers: answers, token: token, page: page %> + <% when "answer_couples_closed" %> + <%= render "polls/questions/answers_couples", answers_open: false, question: question, token: token %> + <% when "answer_couples_open" %> + <%= render "polls/questions/answers_couples", answers_open: true, question: question, token: token %> + <% when "answer_set_closed", "answer_set_open" %> + <%= render "polls/questions/answers_set", question: question, token: token, answers: question.votation_type.votation_set_answers.by_author(current_user) %> + <% when "prioritized" %> + <%= render "polls/questions/answers_prioritized", answers_open: false, question: question, answers: answers, token: token %> <% else %> - <% question.question_answers.order(id: :desc).each do |answer| %> - <%= answer.title %> - <% end %> + <%= render "polls/questions/answers_unique", question: question, answers: answers, token: token %> <% end %> -
+<% end %> diff --git a/app/views/polls/questions/_answers_couples.html.erb b/app/views/polls/questions/_answers_couples.html.erb new file mode 100644 index 00000000000..cab665ed804 --- /dev/null +++ b/app/views/polls/questions/_answers_couples.html.erb @@ -0,0 +1,39 @@ +
+ <% if can?(:answer, question) && question.user_can_vote(current_user) %> +
+ <% @last_pair_question_answers.dig(question.id)&.answers&.each do |answer| %> +
+ <%= link_to answer.title, + answer_question_path(question, answer: answer.title, token: token), + method: :post, + remote: true, + title: t("poll_questions.show.vote_answer", answer: answer.title), + class: "button secondary hollow js-question-answer" %> +
+ <% end %> +
+
+ <%= link_to "I can't decide", + answer_question_path(question, answer: "I can't decided", token: token), + method: :post, + remote: true, + title: "I can't decide", + class: "button secondary hollow js-question-answer" %> +
+ + <% if answers_open %> + <%= render "/polls/questions/new_answer", question: question, token: token %> + <% end %> + + <% elsif !user_signed_in? %> + <% question.question_answers.visibles.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.visibles.order(id: :desc).each do |answer| %> + <%= link_to answer.title, verification_path, class: "button secondary hollow" %> + <% end %> + <% else %> + <%= t('polls.index.max_votes_reached') %> + <% end %> +
diff --git a/app/views/polls/questions/_answers_multiple.html.erb b/app/views/polls/questions/_answers_multiple.html.erb new file mode 100644 index 00000000000..3770b0e3cd0 --- /dev/null +++ b/app/views/polls/questions/_answers_multiple.html.erb @@ -0,0 +1,36 @@ +
+ <% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> + <% answers&.each do |answer| %> + <% if @answers_by_question_id[question.id].include?(answer.title) %> + <%= link_to answer.title, + answer_question_path(question, answer: answer.title, token: token), + method: :delete, + remote: true, + title: t("poll_questions.show.voted", answer: answer.title), + class: "button answered expand" %> + <% else %> + <%= link_to answer.title, + answer_question_path(question, answer: answer.title, token: token), + method: :post, + remote: true, + title: t("poll_questions.show.vote_answer", answer: answer.title), + class: "button secondary hollow js-question-answer" %> + <% end %> + <% end %> + <% if question.enum_type == "positive_open" %> + <%= render "/polls/questions/new_answer", question: question, token: token %> + <% end %> + <% elsif !user_signed_in? %> + <% 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? %> + <% answers.order(id: :desc).each do |answer| %> + <%= link_to answer.title, verification_path, class: "button secondary hollow" %> + <% end %> + <% else %> + <% answers.order(id: :desc).each do |answer| %> + <%= answer.title %> + <% end %> + <% end %> +
diff --git a/app/views/polls/questions/_answers_positive_negative.html.erb b/app/views/polls/questions/_answers_positive_negative.html.erb new file mode 100644 index 00000000000..22959931c44 --- /dev/null +++ b/app/views/polls/questions/_answers_positive_negative.html.erb @@ -0,0 +1,55 @@ +
+ <% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> + + <% answers&.each do |answer| %> + + + + + <% end %> +
+ <%= answer.title %> + +
+ <% if @answers_by_question_id[question.id].include?(answer.title) && + stored_positive_negative_value(question, answer) %> + <%= render "polls/questions/like_dislike", question: question, + answer: answer, method: "delete", positive: true, + token: token, active: true, page: page %> + <% else %> + <%= render "polls/questions/like_dislike", question: question, + answer: answer, method: "post", positive: true, + token: token, active: false, page: page %> + <% end %> +
+
+ <% if @answers_by_question_id[question.id].include?(answer.title) && + !stored_positive_negative_value(question, answer) %> + <%= render "polls/questions/like_dislike", question: question, + answer: answer, method: "delete", positive: false, + token: token, active: true, page: page %> + <% else %> + <%= render "polls/questions/like_dislike", question: question, + answer: answer, method: "post", positive: false, + token: token, active: false, page: page %> + <% end %> +
+
+ <%= paginate answers, params: {controller: "polls/questions", action: :load_answers, id: question}, remote: true %> + + <%= render "/polls/questions/new_answer", question: question, token: token %> + + <% elsif !user_signed_in? %> + <% 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? %> + <% answers.visibles.order(id: :desc).each do |answer| %> + <%= link_to answer.title, verification_path, class: "button secondary hollow" %> + <% end %> + <% else %> + <% answers.order(id: :desc).each do |answer| %> + <%= answer.title %> + <% end %> + <% end %> +
diff --git a/app/views/polls/questions/_answers_prioritized.html.erb b/app/views/polls/questions/_answers_prioritized.html.erb new file mode 100644 index 00000000000..07500855028 --- /dev/null +++ b/app/views/polls/questions/_answers_prioritized.html.erb @@ -0,0 +1,44 @@ +
+ <% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> +
    + <% @answers_by_question_id[question.id].each do |answer| %> +
  1. + "> + <%= answer %> + +
  2. + <% end %> +
+ + <% answers&.each do |answer| %> + <% if @answers_by_question_id[question.id].include?(answer.title) %> + <%= link_to answer.title, + answer_question_path(question, answer: answer.title, token: token), + method: :delete, + remote: true, + title: t("poll_questions.show.voted", answer: answer.title), + class: "button answered expand" %> + <% else %> + <%= link_to answer.title, + answer_question_path(question, answer: answer.title, token: token), + method: :post, + remote: true, + title: t("poll_questions.show.vote_answer", answer: answer.title), + class: "button secondary hollow js-question-answer" %> + <% end %> + <% end %> + <% elsif !user_signed_in? %> + <% 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? %> + <% answers.order(id: :desc).each do |answer| %> + <%= link_to answer.title, verification_path, class: "button secondary hollow" %> + <% end %> + <% else %> + <% answers.order(id: :desc).each do |answer| %> + <%= answer.title %> + <% end %> + <% end %> +
diff --git a/app/views/polls/questions/_answers_set.html.erb b/app/views/polls/questions/_answers_set.html.erb new file mode 100644 index 00000000000..f8870c0d448 --- /dev/null +++ b/app/views/polls/questions/_answers_set.html.erb @@ -0,0 +1,37 @@ +
+ <% if can?(:answer, question) %> + <% answers.each do |answer| %> + <% if @answers_by_question_id[question.id].include?(answer.answer) %> + <%= link_to answer.answer, + answer_question_path(question, answer: answer.answer, token: token), + method: :delete, + remote: true, + title: t("poll_questions.show.voted", answer: answer.answer), + class: "button answered" %> + <% else %> + <%= link_to answer.answer, + answer_question_path(question, answer: answer.answer, token: token), + method: :post, + remote: true, + title: t("poll_questions.show.vote_answer", answer: answer.answer), + class: "button secondary hollow js-question-answer" %> + <% end %> + <% end %> + <% if question.enum_type == "answer_set_open" %> + <%= render "/polls/questions/new_answer", question: question, token: token %> + <%= flash[:notice] %> + <% end %> + <% elsif !user_signed_in? %> + <% answers.order(id: :desc).each do |answer| %> + <%= link_to answer.answer, new_user_session_path, class: "button secondary hollow" %> + <% end %> + <% elsif !current_user.level_two_or_three_verified? %> + <% answers.order(id: :desc).each do |answer| %> + <%= link_to answer.answer, verification_path, class: "button secondary hollow" %> + <% end %> + <% else %> + <% answers.order(id: :desc).each do |answer| %> + <%= answer.answer %> + <% end %> + <% end %> +
diff --git a/app/views/polls/questions/_answers_unique.html.erb b/app/views/polls/questions/_answers_unique.html.erb new file mode 100644 index 00000000000..8029624ee57 --- /dev/null +++ b/app/views/polls/questions/_answers_unique.html.erb @@ -0,0 +1,33 @@ +
+ <% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> + <% answers&.each do |answer| %> + <% if @answers_by_question_id[question.id].include?(answer.title) && + (!voted_before_sign_in(question) || + question.poll.voted_in_booth?(current_user)) %> + "> + <%= answer.title %> + + <% else %> + <%= link_to answer.title, + answer_question_path(question, answer: answer.title, token: token), + method: :post, + remote: true, + title: t("poll_questions.show.vote_answer", answer: answer.title), + class: "button secondary hollow js-question-answer" %> + <% end %> + <% end %> + <% elsif !user_signed_in? %> + <% 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? %> + <% answers.order(id: :desc).each do |answer| %> + <%= link_to answer.title, verification_path, class: "button secondary hollow" %> + <% end %> + <% else %> + <% answers.order(id: :desc).each do |answer| %> + <%= answer.title %> + <% end %> + <% end %> +
diff --git a/app/views/polls/questions/_like_dislike.html.erb b/app/views/polls/questions/_like_dislike.html.erb new file mode 100644 index 00000000000..e34476e9a55 --- /dev/null +++ b/app/views/polls/questions/_like_dislike.html.erb @@ -0,0 +1,19 @@ +<%= link_to answer_question_path(question, answer: answer.title, positive: positive, token: token, page: page), + method: method.to_sym, + remote: true, + title: t("poll_questions.show.vote_answer", answer: answer.title), + class: "expand js-question-answer" do %> + <% if positive %> + <% if active %> + + <% else %> + + <% end %> + <% else %> + <% if active %> + + <% else %> + + <% end %> + <% end %> +<% end %> diff --git a/app/views/polls/questions/_new_answer.html.erb b/app/views/polls/questions/_new_answer.html.erb new file mode 100644 index 00000000000..2f056bafa14 --- /dev/null +++ b/app/views/polls/questions/_new_answer.html.erb @@ -0,0 +1,5 @@ +<%= form_tag(create_answer_question_path(question, token: token), method: :post, remote: true) do %> + + + "> +<% end %> diff --git a/app/views/polls/questions/_question.html.erb b/app/views/polls/questions/_question.html.erb index 016c0fa9562..bb50da3bc5f 100644 --- a/app/views/polls/questions/_question.html.erb +++ b/app/views/polls/questions/_question.html.erb @@ -3,7 +3,35 @@ <%= question.title %> + <% unless question.votation_type.nil? %> + + <%= t("poll_questions.description.#{question.enum_type}", + maximum: question.votation_type.max_votes, + system: question.votation_type.prioritization_type) %> + + <% end %> +
- <%= render "polls/questions/answers", question: question, token: token %> + <% answers = question.is_positive_negative? ? question.question_answers.visibles.page(1) : question.question_answers.visibles %> + <%= render "polls/questions/answers", question: question, token: token, answers: answers, page: 1 %>
+ + <% if question.answers_with_read_more? %> +
+

<%= t("poll_questions.read_more_about") %>

+

+ <% first = true %> + <% question.question_answers&.visibles&.each do |answer| %> + <% if answer.description.present? || answer.images.any? || + answer.documents.present? || answer.videos.present? %> + <% unless first %> + + <% end %> + <% first = false if first %> + <%= link_to answer.title, "#answer_description_#{answer.id}" %> + <% end %> + <% end %> +

+
+ <% end %> diff --git a/app/views/polls/questions/answer.js.erb b/app/views/polls/questions/answer.js.erb index 79978e2c13d..c35c55bfcee 100644 --- a/app/views/polls/questions/answer.js.erb +++ b/app/views/polls/questions/answer.js.erb @@ -1,2 +1,3 @@ <% token = poll_voter_token(@question.poll, current_user) %> -$("#<%= dom_id(@question) %>_answers").html("<%= j render("polls/questions/answers", question: @question, token: token) %>"); +$("#<%= dom_id(@question) %>_answers").html("<%= j render("polls/questions/answers", question: @question, answers: @answers, token: token, page: @page) %>"); +App.Sortable.initialize(); diff --git a/app/views/polls/results.html.erb b/app/views/polls/results.html.erb index 7627fe01886..c64362b95a0 100644 --- a/app/views/polls/results.html.erb +++ b/app/views/polls/results.html.erb @@ -1,4 +1,5 @@ -<% provide :title do %><%= @poll.name %><% end %> +<% provide :title do %><%= @poll.name %> +<% end %>
<%= render "poll_header" %> @@ -10,37 +11,32 @@

<%= t("polls.show.results.title") %>

<%- @poll.questions.each do |question| %> - <% most_voted_answer_id = question.most_voted_answer_id %>

<%= question.title %>

- - - <%- question.question_answers.each do |answer| %> - - <% end %> - - - - <%- question.question_answers.each do |answer| %> - > + + - <% end %> + <% end %> + + <% end %>
> - <% if answer.id == most_voted_answer_id %> - <%= t("polls.show.results.most_voted_answer") %> - <% end %> - <%= answer.title %> -
> - <%= answer.total_votes %> + <%- question.question_answers.visibles.each do |answer| %> +
+ <% if answer.most_voted %> + <%= t("polls.show.results.most_voted_answer") %> + <% end %> + <%= answer.title %> + > + <%= answer.total_votes %> + <% unless question.enum_type == "positive_negative_open" || question.enum_type == "prioritized" %> (<%= answer.total_votes_percentage.round(2) %>%) -
<% end %> diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb index 5fc0d67226d..04521882275 100644 --- a/app/views/polls/show.html.erb +++ b/app/views/polls/show.html.erb @@ -1,4 +1,4 @@ -<%= provide :title, t("social_share.polls_show.title_#{@poll.id}", default: @poll.title) %> +<%= provide :title, t("social_share.polls_show.title_#{@poll.id}", default: @poll.title) %> <%= provide :meta_description, t("social_share.polls_show.description_#{@poll.id}", default: @poll.title) %> <%= provide :social_media_meta_tags do %> <%= render "shared/social_media_meta_tags", @@ -55,71 +55,80 @@
- - <% @poll_questions_answers.each do |answer| %> -
" id="answer_<%= answer.id %>"> - - <% if answer.description.present? %> -

<%= answer.title %>

- <% end %> - - <% if answer.images.any? %> - <%= render "gallery", answer: answer %> - <% end %> - - <% if answer.description.present? %> - - <% end %> - - <% if answer.documents.present? %> - - <% end %> - - <% if answer.videos.present? %> - + <% end %> +
+ <% end %> <% end %>
diff --git a/config/routes/poll.rb b/config/routes/poll.rb index 96c43f8567e..8836fa762ad 100644 --- a/config/routes/poll.rb +++ b/config/routes/poll.rb @@ -5,6 +5,12 @@ end resources :questions, controller: "polls/questions", shallow: true do - post :answer, on: :member + member do + post :answer + post :prioritized_answers + delete :answer, to: "polls/answers#delete" + post :create_answer, to: "polls/answers#create" + get :load_answers + end end end diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb index f9fd8fbeffe..d30c7ddfcad 100644 --- a/spec/models/abilities/common_spec.rb +++ b/spec/models/abilities/common_spec.rb @@ -213,6 +213,18 @@ it { should_not be_able_to(:answer, expired_poll_question_from_all_geozones) } it { should_not be_able_to(:answer, expired_poll_question_from_other_geozone) } + it { should be_able_to(:prioritized_answers, poll_question_from_own_geozone) } + it { should be_able_to(:prioritized_answers, poll_question_from_all_geozones) } + it { should_not be_able_to(:prioritized_answers, poll_question_from_other_geozone) } + + it { should_not be_able_to(:prioritized_answers, expired_poll_question_from_own_geozone) } + it { should_not be_able_to(:prioritized_answers, expired_poll_question_from_all_geozones) } + it { should_not be_able_to(:prioritized_answers, expired_poll_question_from_other_geozone) } + + context "Poll::Question" do + it { should be_able_to(:load_answers, Poll::Question) } + end + context "without geozone" do before { user.geozone = nil }