Allow users to delete dashboard polls

This commit is contained in:
decabeza
2019-05-29 10:45:55 +02:00
parent 6b7c6a2f1f
commit 3855bf26ad
6 changed files with 56 additions and 4 deletions

View File

@@ -35,6 +35,16 @@ class Dashboard::PollsController < Dashboard::BaseController
end
end
def destroy
if ::Poll::Voter.where(poll: poll).any?
redirect_to proposal_dashboard_polls_path(proposal), alert: t("dashboard.polls.poll.unable_notice")
else
poll.destroy
redirect_to proposal_dashboard_polls_path(proposal), notice: t("dashboard.polls.poll.success_notice")
end
end
private
def poll

View File

@@ -1,8 +1,8 @@
<div id="<%= dom_id(poll) %>" class="small-12 medium-6 large-4 column end">
<div class="poll-card" data-equalizer-watch="poll-cards">
<h4>
<%= link_to poll.title,
proposal_poll_path(proposal, poll),
<%= link_to poll.title,
proposal_poll_path(proposal, poll),
target: "_blank" %>
</h4>
<span class="small">
@@ -19,7 +19,7 @@
edit_proposal_dashboard_poll_path(proposal, poll), class: "button hollow" %>
<% else %>
<%= link_to t("dashboard.polls.poll.view_results"),
results_proposal_poll_path(proposal, poll),
results_proposal_poll_path(proposal, poll),
class: "button", target: "_blank" %>
<% end %>
</div>
@@ -31,5 +31,11 @@
class: "js-submit-on-change" %>
<% end %>
<p class="help-text"><%= t("dashboard.polls.poll.show_results_help") %></p>
<%= link_to t("dashboard.polls.poll.delete"),
proposal_dashboard_poll_path(proposal, poll),
method: :delete,
"data-confirm": t("dashboard.polls.poll.alert_notice"),
class: "delete" %>
</div>
</div>

View File

@@ -558,6 +558,10 @@ en:
edit_poll: Edit survey
show_results: Show results
show_results_help: If you check this box the results will be public and all users will be able to see them
delete: Delete survey
alert_notice: This action will remove the survey and all its associated questions.
success_notice: Survey deleted successfully
unable_notice: You cannot destroy a survey that has responses
form:
name: Survey title
add_question: Add question

View File

@@ -558,6 +558,10 @@ es:
edit_poll: Editar encuesta
show_results: Mostrar resultados
show_results_help: Si marcas esta casilla los resultados serán públicos y todos los usuarios podrán verlos
delete: Eliminar encuesta
alert_notice: Esta acción eliminará la encuesta y todas sus preguntas asociadas.
success_notice: Encuesta eliminada correctamente
unable_notice: No se pueden eliminar encuestas con respuestas
form:
name: Título de la encuesta
add_question: Añadir pregunta

View File

@@ -11,7 +11,7 @@ resources :proposals do
resources :achievements, only: [:index], controller: "dashboard/achievements"
resources :successful_supports, only: [:index], controller: "dashboard/successful_supports"
resources :supports, only: [:index], controller: "dashboard/supports"
resources :polls, except: [:show, :destroy], controller: "dashboard/polls"
resources :polls, except: [:show], controller: "dashboard/polls"
resources :mailing, only: [:index, :new, :create], controller: "dashboard/mailing"
resources :poster, only: [:index, :new], controller: "dashboard/poster"
resources :actions, only: [], controller: "dashboard/actions" do

View File

@@ -153,6 +153,34 @@ describe "Polls" do
end
end
scenario "Can destroy poll without responses", :js do
poll = create(:poll, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within("#poll_#{poll.id}") do
accept_confirm { click_link "Delete survey" }
end
expect(page).to have_content("Survey deleted successfully")
expect(page).not_to have_content(poll.name)
end
scenario "Can't destroy poll with responses", :js do
poll = create(:poll, related: proposal)
create(:poll_question, poll: poll)
create(:poll_voter, poll: poll)
visit proposal_dashboard_polls_path(proposal)
within("#poll_#{poll.id}") do
accept_confirm { click_link "Delete survey" }
end
expect(page).to have_content("You cannot destroy a survey that has responses")
expect(page).to have_content(poll.name)
end
scenario "View results not available for upcoming polls" do
poll = create(:poll, related: proposal, starts_at: 1.week.from_now)