diff --git a/app/views/admin/poll/results/index.html.erb b/app/views/admin/poll/results/index.html.erb
index eaf6ad745..1358eaebb 100644
--- a/app/views/admin/poll/results/index.html.erb
+++ b/app/views/admin/poll/results/index.html.erb
@@ -11,10 +11,14 @@
<% end %>
- <% if @partial_results.present? %>
- <%= render "recount", resource: @poll %>
- <%= render "result" %>
- <%= render "results_by_booth" %>
+ <% if @poll.questions.any? { |question| question.votation_type.present? } %>
+ <%= render "votation_types_results" %>
+ <% else %>
+ <% if @partial_results.present? %>
+ <%= render "recount", resource: @poll %>
+ <%= render "result" %>
+ <%= render "results_by_booth" %>
+ <% end %>
<% end %>
<% if @poll.voters.any? %>
diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml
index a02f71928..510294c5f 100644
--- a/config/locales/en/general.yml
+++ b/config/locales/en/general.yml
@@ -556,6 +556,7 @@ en:
index:
title: Polls
create: Create poll
+ succesfull: Answer added succesfully
count:
one: You have created %{count} poll.
other: You have created %{count} polls.
@@ -648,6 +649,17 @@ en:
title: Help about voting
description: Citizens' polls are a participatory mechanism by which citizens with voting rights can make direct decisions
no_polls: "There are no open votings."
+ max_votes_reached: "You have already made the maximum number of votes. Thank you very much for participating"
+ descriptions:
+ unique: It's only possible to answer one time to the question.
+ multiple: Allows to choose multiple answers. It's possible to set the maximum number of answers.
+ prioritized: Allows to choose more than one answer and they will be prioritized. It's possible to set the maximum number of answers chosen and the answer count type.
+ positive_open: Allows to vote positively a maximum number of times to questions. It's possible to set the maximum number of answers and add other answers.
+ positive_negative_open: Allows to vote positively and negatively a maximum number of times to questions. It's possible to set the maximum number of answers and add other answers.
+ answer_couples_open: Allows to vote a maximum number of times to couples of possible answers. It's possible to set the maximum number of couples and add other answers.
+ answer_couples_closed: Allows to vote a maximum number of times to couples of possible answers. It's possible to set the maximum number of couples and it's not possible to add other answers.
+ answer_set_open: Allows to vote a maximum number of answers to a group of them. It's possible to set the maximum number of answers, the size of the group and add other answers.
+ answer_set_closed: Allows to vote a maximum number of answers to a group of them. It's possible to set the maximum number of answers and the size of the group.
show:
already_voted_in_booth: "You have already participated in a physical booth. You can not participate again."
already_voted_in_web: "You have already participated in this poll. If you vote again it will be overwritten."
@@ -689,6 +701,18 @@ en:
show:
vote_answer: "Vote %{answer}"
voted: "You have voted %{answer}"
+ add_answer: "Add answer"
+ description:
+ multiple: "You can select a maximum of %{maximum} answers."
+ positive_negative_open: "You can vote positive or negative a maximum of %{maximum} answers. And you can add your own answers."
+ answer_couples_open: "Choose an option from the following pair. You can choose a maximum of %{maximum} answers."
+ answer_couples_closed: "Choose an option from the following pair. You can choose a maximum of %{maximum} answers."
+ answer_set_open: "You can choose a maximum of %{maximum} answers."
+ answer_set_closed: "You can choose a maximum of %{maximum} answers."
+ prioritized: "You can select a maximum of %{maximum} answers. This question will use the %{system} system for count."
+ positive_open: "You can select a maximum of %{maximum} answers and add your own answers."
+ unique: ""
+ read_more_about: "Read more about:"
proposal_notifications:
new:
title: "Send message"
@@ -992,3 +1016,22 @@ en:
surveys: Surveys
poll:
take_part: Take part from %{from} to %{to}
+ question:
+ max_votes: Maximum number of votes
+ max_group_answers: Maximum number of answers in the set
+ votation_type: Votation type
+ enum_type:
+ title: Votation type
+ unique: Unique answer, closed
+ multiple: Multiple answers, closed
+ prioritized: Multiple prioritized answer, closed
+ positive_open: Votable positive, open
+ positive_negative_open: Votable positive and negative, open
+ answer_couples_open: Couples of answers, open
+ answer_couples_closed: Couples of answers, closed
+ answer_set_open: Set of answers, open
+ answer_set_closed: Set of answers, closed
+ prioritization_type:
+ title: Prioritization type
+ borda: Borda votation
+ dowdall: Dowdall votation
diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml
index 75931e379..1e9621506 100644
--- a/config/locales/es/general.yml
+++ b/config/locales/es/general.yml
@@ -556,6 +556,7 @@ es:
index:
title: Encuestas
create: Crear encuesta
+ succesfull: Respuesta añadida correctamente.
count:
one: Has creado %{count} encuesta.
other: Has creado %{count} encuestas.
@@ -646,6 +647,17 @@ es:
title: Ayuda sobre las votaciones
description: Las votaciones ciudadanas son un mecanismo de participación por el que la ciudadanía con derecho a voto puede tomar decisiones de forma directa.
no_polls: "No hay votaciones abiertas."
+ max_votes_reached: "Ya has realizado el número máximo de votos. Muchas gracias por participar"
+ descriptions:
+ unique: Solo se puede responder a la pregunta con una única respuesta.
+ multiple: Permite elegir más de una respuesta. Se puede elegir el número máximo de respuestas.
+ prioritized: Permite elegir mas de una respuesta, que a su vez se podran ordenar por prioridad. Se puede elegir el número máximo de respuestas y que tipo de recuento se utilizará en la priorización.
+ positive_open: Permite votar positivamente a un número máximo de respuestas. Se puede elegir el número máximo y los usuarios podran añadir resuestas.
+ positive_negative_open: Permite votar positiva y negativamente a un máximo de respuestas. Se puede elegir el número máximo y los usuarios podran añadir resuestas.
+ answer_couples_open: Permite votar a un número maximo de pares de respuestas. Se puede elegir el número maximo de pares y los usuarios podran añadir respuestas.
+ answer_couples_closed: Permite votar a un número maximo de pares de respuestas. Se puede elegir el número maximo de pares y los usuarios NO podran añadir respuestas.
+ answer_set_open: Permite votar un número máximo de respuestas en un conjunto de ellas. Se puede elegir el numero máximo de respuestas y el número de respuestas que hay en el conjunto. Los usuarios podran añadir respuestas.
+ answer_set_closed: Permite votar un número máximo de respuestas en un conjunto de ellas. Se puede elegir el numero máximo de respuestas y el número de respuestas que hay en el conjunto.
show:
already_voted_in_booth: "Ya has participado en esta votación en urnas presenciales, no puedes volver a participar."
already_voted_in_web: "Ya has participado en esta votación. Si vuelves a votar se sobreescribirá tu resultado anterior."
@@ -687,6 +699,17 @@ es:
show:
vote_answer: "Votar %{answer}"
voted: "Has votado %{answer}"
+ add_answer: "Añadir respuesta"
+ description:
+ multiple: "Puedes seleccionar un máximo de %{maximum} respuestas."
+ positive_negative_open: "Puedes votar, positiva o negativamente, un máximo de %{maximum} respuestas. Puedes añadir tus propias respuestas."
+ multiple: "Puedes seleccionar un máximo de %{maximum} respuestas"
+ answer_couples_open: "Elige una opción entre el siguiente par. Puedes elegir un máximo de %{maximum} respuestas"
+ answer_couples_closed: "Elige una opción entre el siguiente par. Puedes elegir un máximo de %{maximum} respuestas"
+ prioritized: "Puedes seleccionar un máximo de %{maximum} respuestas. Esta pregunta utilizara el método %{system} para el recuento."
+ positive_open: "Puedes seleccionar un máximo de %{maximum} respuestas y añadir tus propias respuestas."
+ unique: ""
+ read_more_about: "Leer más:"
proposal_notifications:
new:
title: "Enviar mensaje"
@@ -990,3 +1013,22 @@ es:
surveys: Encuestas
poll:
take_part: Participa del %{from} al %{to}
+ question:
+ max_votes: Número máximo de votos
+ max_group_answers: Número máximo de respuestas en el conjunto
+ votation_type: Tipo de votación
+ enum_type:
+ title: Tipo de votación
+ unique: Respuesta única, cerrada
+ multiple: Respuesta múltiple, cerrada
+ prioritized: Respuesta múltiple priorizada, cerrada
+ positive_open: Respuestas votables positivamente, abiertas
+ positive_negative_open: Respuestas votables positiva y negativamente, abiertas
+ answer_couples_open: Respuestas a pares, abiertas
+ answer_couples_closed: Respuestas a pares, cerradas
+ answer_set_open: Conjunto de respuestas, abiertas
+ answer_set_closed: Conjunto de respuestas, cerradas
+ prioritization_type:
+ title: Tipo de priorizacion
+ borda: Votación con recuento Borda
+ dowdall: Votación con recuento Dowdall
diff --git a/config/routes/admin.rb b/config/routes/admin.rb
index 294868b27..2b850a27d 100644
--- a/config/routes/admin.rb
+++ b/config/routes/admin.rb
@@ -163,6 +163,7 @@ namespace :admin do
end
resource :active_polls, only: [:create, :edit, :update]
+ get :get_options_traductions, controller: "questions"
end
resources :verifications, controller: :verifications, only: :index do
diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb
index 398ba4925..536e45858 100644
--- a/spec/features/admin/poll/polls_spec.rb
+++ b/spec/features/admin/poll/polls_spec.rb
@@ -248,14 +248,17 @@ describe "Admin polls" do
scenario "Question list", :js do
poll = create(:poll)
question = create(:poll_question, poll: poll)
+ votation_type_question = create(:poll_question_unique, poll: poll)
other_question = create(:poll_question)
visit admin_poll_path(poll)
- expect(page).to have_content "Questions (1)"
+ expect(page).to have_content "Questions (2)"
expect(page).to have_content question.title
+ expect(page).to have_content votation_type_question.title
expect(page).not_to have_content other_question.title
expect(page).not_to have_content "There are no questions assigned to this poll"
+
end
end
diff --git a/spec/features/admin/poll/questions_spec.rb b/spec/features/admin/poll/questions_spec.rb
index c315f904e..c1906e4dd 100644
--- a/spec/features/admin/poll/questions_spec.rb
+++ b/spec/features/admin/poll/questions_spec.rb
@@ -19,6 +19,7 @@ describe "Admin poll questions" do
question1 = create(:poll_question, poll: poll1)
question2 = create(:poll_question, poll: poll2)
question3 = create(:poll_question, poll: poll3, proposal: proposal)
+ question4 = create(:poll_question_unique, poll: poll1)
visit admin_poll_path(poll1)
expect(page).to have_content(poll1.name)
@@ -30,6 +31,13 @@ describe "Admin poll questions" do
expect(page).to have_content("Delete")
end
+ within("#poll_question_#{question4.id}") do
+ expect(page).to have_content(question4.title)
+ expect(page).to have_content("Edit answers")
+ expect(page).to have_content("Edit")
+ expect(page).to have_content("Delete")
+ end
+
visit admin_poll_path(poll2)
expect(page).to have_content(poll2.name)
@@ -52,16 +60,35 @@ describe "Admin poll questions" do
end
end
- scenario "Show" do
- geozone = create(:geozone)
- poll = create(:poll, geozone_restricted: true, geozone_ids: [geozone.id])
- question = create(:poll_question, poll: poll)
+ context "Show" do
+ scenario "Without Votation type" do
+ geozone = create(:geozone)
+ poll = create(:poll, geozone_restricted: true, geozone_ids: [geozone.id])
+ question = create(:poll_question, poll: poll)
- visit admin_poll_path(poll)
- click_link "#{question.title}"
+ visit admin_poll_path(poll)
+ click_link question.title
- expect(page).to have_content(question.title)
- expect(page).to have_content(question.author.name)
+ expect(page).to have_content(question.title)
+ expect(page).to have_content(question.author.name)
+ expect(page).not_to have_content("Votation type")
+ end
+
+ scenario "With Votation type" do
+ geozone = create(:geozone)
+ poll = create(:poll, geozone_restricted: true, geozone_ids: [geozone.id])
+ question = create(:poll_question_multiple, poll: poll)
+
+ visit admin_poll_path(poll)
+ click_link "#{question.title}"
+
+ expect(page).to have_content(question.title)
+ expect(page).to have_content(question.author.name)
+ expect(page).to have_content("Votation type")
+ expect(page).to have_content("Multiple")
+ expect(page).to have_content("Maximum number of votes")
+ expect(page).to have_content("5")
+ end
end
scenario "Create" do
@@ -123,6 +150,179 @@ describe "Admin poll questions" do
expect(page).to have_content(proposal.title)
end
+ context "create with votation type" do
+ before do
+ poll = create(:poll, name: "Movies")
+ visit admin_poll_path(poll)
+ click_link "Create question"
+ end
+
+ scenario "unique" do
+ title = "unique question"
+ fill_in "Question", with: title
+ select "Unique answer, closed", from: "votation_type_enum_type"
+
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Unique answer, closed")
+ end
+
+ scenario "multiple" do
+ title = "multiple question"
+ fill_in "Question", with: title
+ select "Multiple answers, closed", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Multiple answers, closed")
+ end
+
+ scenario "prioritized" do
+ title = "prioritized question"
+ fill_in "Question", with: title
+ select "Multiple prioritized answer, closed", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Multiple prioritized answer, closed")
+ end
+
+ scenario "positive_open" do
+ title = "positive open question"
+ fill_in "Question", with: title
+ select "Votable positive, open", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Votable positive, open")
+ end
+
+ scenario "positive_negative_open" do
+ title = "positive negative open question"
+ fill_in "Question", with: title
+ select "Votable positive and negative, open", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Votable positive and negative, open")
+ end
+
+ scenario "answer_couples_open" do
+ title = "answer couples open question"
+ fill_in "Question", with: title
+ select "Couples of answers, open", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Couples of answers, open")
+ end
+
+ scenario "answer_couples_closed" do
+ title = "answer couples closed question"
+ fill_in "Question", with: title
+ select "Couples of answers, closed", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Couples of answers, closed")
+ end
+
+ scenario "answer_set_open" do
+ title = "answer set open question"
+ fill_in "Question", with: title
+ select "Set of answers, open", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of answers in the set", with: 3
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Set of answers, open")
+ end
+
+ scenario "answer_set_closed" do
+ title = "answer set closed question"
+ fill_in "Question", with: title
+ select "Set of answers, closed", from: "votation_type_enum_type"
+
+ expect(page).to have_content("Maximum number of votes")
+
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of votes", with: 6
+ click_button "Save"
+
+ expect(page).to have_content("1 error prevented this Poll/Question from being saved.")
+
+ fill_in "Maximum number of answers in the set", with: 3
+ click_button "Save"
+
+ expect(page).to have_content(title)
+ expect(page).to have_content("Set of answers, closed")
+ end
+ end
+
scenario "Update" do
poll = create(:poll)
question1 = create(:poll_question, poll: poll)
diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb
index 1d1acb222..575a34744 100644
--- a/spec/models/abilities/administrator_spec.rb
+++ b/spec/models/abilities/administrator_spec.rb
@@ -88,6 +88,11 @@ describe Abilities::Administrator do
it { should be_able_to(:stats, poll) }
it { should be_able_to(:results, poll) }
+ it { should be_able_to(:read, Poll::Question)}
+ it { should be_able_to(:create, Poll::Question)}
+ it { should be_able_to(:update, Poll::Question)}
+ it { should be_able_to(:get_options_traductions, Poll::Question)}
+
it { is_expected.to be_able_to :manage, Dashboard::AdministratorTask }
it { is_expected.to be_able_to :manage, dashboard_administrator_task }
end
From c6e4b2480fbe0e0584e91259c51949c1e64ef102 Mon Sep 17 00:00:00 2001
From: lalo
Date: Wed, 29 May 2019 18:32:04 +0200
Subject: [PATCH 3/6] 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 | 81 +++++++++--
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 | 131 ++++++++++--------
config/routes/poll.rb | 8 +-
spec/models/abilities/common_spec.rb | 12 ++
22 files changed, 711 insertions(+), 135 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 61124264e..ee98703f3 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 5a72a100b..f003c4632 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 000000000..49c5fcfc9
--- /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 eb054dd1e..b929e0fc5 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.answer = params[:answer]
- answer.touch if answer.persisted?
- answer.save!
- answer.record_voter_participation(token)
-
- @answers_by_question_id = { @question.id => params[:answer] }
+ 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
+ def load_answers
+ load_for_answers
+ render action: "answer.js.erb"
+ end
+
+ 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 f322379ce..b95303384 100644
--- a/app/controllers/polls_controller.rb
+++ b/app/controllers/polls_controller.rb
@@ -20,13 +20,27 @@ class PollsController < ApplicationController
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 @@ class PollsController < ApplicationController
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 @@ class PollsController < ApplicationController
@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 2faf9b3f8..963367a4f 100644
--- a/app/helpers/polls_helper.rb
+++ b/app/helpers/polls_helper.rb
@@ -78,4 +78,8 @@ module PollsHelper
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 b106ebef5..d26a38680 100644
--- a/app/models/abilities/common.rb
+++ b/app/models/abilities/common.rb
@@ -104,12 +104,18 @@ module Abilities
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 206896b04..bfba35b91 100644
--- a/app/views/polls/questions/_answers.html.erb
+++ b/app/views/polls/questions/_answers.html.erb
@@ -1,33 +1,22 @@
-