Merge pull request #3476 from consul/delete-poll
Allow delete polls with associated questions and answers
This commit is contained in:
@@ -58,6 +58,16 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
|
|||||||
@polls = Poll.current
|
@polls = Poll.current
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
if ::Poll::Voter.where(poll: @poll).any?
|
||||||
|
redirect_to admin_poll_path(@poll), alert: t("admin.polls.destroy.unable_notice")
|
||||||
|
else
|
||||||
|
@poll.destroy
|
||||||
|
|
||||||
|
redirect_to admin_polls_path, notice: t("admin.polls.destroy.success_notice")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def load_geozones
|
def load_geozones
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class Poll < ApplicationRecord
|
|||||||
has_many :voters
|
has_many :voters
|
||||||
has_many :officer_assignments, through: :booth_assignments
|
has_many :officer_assignments, through: :booth_assignments
|
||||||
has_many :officers, through: :officer_assignments
|
has_many :officers, through: :officer_assignments
|
||||||
has_many :questions, inverse_of: :poll
|
has_many :questions, inverse_of: :poll, dependent: :destroy
|
||||||
has_many :comments, as: :commentable
|
has_many :comments, as: :commentable
|
||||||
has_many :ballot_sheets
|
has_many :ballot_sheets
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class Poll::Question < ApplicationRecord
|
|||||||
|
|
||||||
has_many :comments, as: :commentable
|
has_many :comments, as: :commentable
|
||||||
has_many :answers, class_name: "Poll::Answer"
|
has_many :answers, class_name: "Poll::Answer"
|
||||||
has_many :question_answers, -> { order "given_order asc" }, class_name: "Poll::Question::Answer"
|
has_many :question_answers, -> { order "given_order asc" }, class_name: "Poll::Question::Answer", dependent: :destroy
|
||||||
has_many :partial_results
|
has_many :partial_results
|
||||||
belongs_to :proposal
|
belongs_to :proposal
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,12 @@
|
|||||||
edit_admin_poll_path(poll),
|
edit_admin_poll_path(poll),
|
||||||
class: "button hollow" %>
|
class: "button hollow" %>
|
||||||
|
|
||||||
|
<%= link_to t("admin.actions.delete"),
|
||||||
|
admin_poll_path(poll),
|
||||||
|
method: :delete,
|
||||||
|
"data-confirm": t("admin.polls.destroy.alert"),
|
||||||
|
class: "button hollow alert" %>
|
||||||
|
|
||||||
<%= link_to t("admin.actions.configure"),
|
<%= link_to t("admin.actions.configure"),
|
||||||
admin_poll_path(poll),
|
admin_poll_path(poll),
|
||||||
class: "button hollow " %>
|
class: "button hollow " %>
|
||||||
|
|||||||
@@ -1035,7 +1035,7 @@ en:
|
|||||||
polls:
|
polls:
|
||||||
index:
|
index:
|
||||||
title: "List of polls"
|
title: "List of polls"
|
||||||
no_polls: "There are no polls coming up."
|
no_polls: "There are no polls."
|
||||||
create: "Create poll"
|
create: "Create poll"
|
||||||
name: "Name"
|
name: "Name"
|
||||||
dates: "Dates"
|
dates: "Dates"
|
||||||
@@ -1066,6 +1066,10 @@ en:
|
|||||||
flash:
|
flash:
|
||||||
question_added: "Question added to this poll"
|
question_added: "Question added to this poll"
|
||||||
error_on_question_added: "Question could not be assigned to this poll"
|
error_on_question_added: "Question could not be assigned to this poll"
|
||||||
|
destroy:
|
||||||
|
alert: "This action will remove the poll and all its associated questions."
|
||||||
|
success_notice: "Poll deleted successfully"
|
||||||
|
unable_notice: "You cannot delete a poll that has votes"
|
||||||
questions:
|
questions:
|
||||||
index:
|
index:
|
||||||
title: "Questions"
|
title: "Questions"
|
||||||
|
|||||||
@@ -1034,7 +1034,7 @@ es:
|
|||||||
polls:
|
polls:
|
||||||
index:
|
index:
|
||||||
title: "Listado de votaciones"
|
title: "Listado de votaciones"
|
||||||
no_polls: "No hay ninguna votación próximamente."
|
no_polls: "No hay votaciones."
|
||||||
create: "Crear votación"
|
create: "Crear votación"
|
||||||
name: "Nombre"
|
name: "Nombre"
|
||||||
dates: "Fechas"
|
dates: "Fechas"
|
||||||
@@ -1065,6 +1065,10 @@ es:
|
|||||||
flash:
|
flash:
|
||||||
question_added: "Pregunta añadida a esta votación"
|
question_added: "Pregunta añadida a esta votación"
|
||||||
error_on_question_added: "No se pudo asignar la pregunta"
|
error_on_question_added: "No se pudo asignar la pregunta"
|
||||||
|
destroy:
|
||||||
|
alert: "Esta acción eliminará la votación y todas sus preguntas asociadas."
|
||||||
|
success_notice: "Votación eliminada correctamente"
|
||||||
|
unable_notice: "No se pueden eliminar votaciones con votos"
|
||||||
questions:
|
questions:
|
||||||
index:
|
index:
|
||||||
title: "Preguntas de votaciones"
|
title: "Preguntas de votaciones"
|
||||||
|
|||||||
@@ -109,6 +109,55 @@ feature "Admin polls" do
|
|||||||
expect(page).to have_current_path(edit_admin_poll_path(poll))
|
expect(page).to have_current_path(edit_admin_poll_path(poll))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "Destroy" do
|
||||||
|
|
||||||
|
scenario "Can destroy poll without questions", :js do
|
||||||
|
poll = create(:poll)
|
||||||
|
|
||||||
|
visit admin_polls_path
|
||||||
|
|
||||||
|
within("#poll_#{poll.id}") do
|
||||||
|
accept_confirm { click_link "Delete" }
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_content("Poll deleted successfully")
|
||||||
|
expect(page).to have_content("There are no polls.")
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Can destroy poll with questions and answers", :js do
|
||||||
|
poll = create(:poll)
|
||||||
|
question = create(:poll_question, poll: poll)
|
||||||
|
create(:poll_question_answer, question: question, title: "Yes")
|
||||||
|
create(:poll_question_answer, question: question, title: "No")
|
||||||
|
|
||||||
|
visit admin_polls_path
|
||||||
|
|
||||||
|
within("#poll_#{poll.id}") do
|
||||||
|
accept_confirm { click_link "Delete" }
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_content("Poll deleted successfully")
|
||||||
|
expect(page).not_to have_content(poll.name)
|
||||||
|
expect(Poll::Question.count).to eq(0)
|
||||||
|
expect(Poll::Question::Answer.count). to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Can't destroy poll with votes", :js do
|
||||||
|
poll = create(:poll)
|
||||||
|
create(:poll_question, poll: poll)
|
||||||
|
create(:poll_voter, :from_booth, :valid_document, poll: poll)
|
||||||
|
|
||||||
|
visit admin_polls_path
|
||||||
|
|
||||||
|
within("#poll_#{poll.id}") do
|
||||||
|
accept_confirm { click_link "Delete" }
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_content("You cannot delete a poll that has votes")
|
||||||
|
expect(page).to have_content(poll.name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "Booths" do
|
context "Booths" do
|
||||||
|
|
||||||
context "Poll show" do
|
context "Poll show" do
|
||||||
|
|||||||
Reference in New Issue
Block a user