Merge pull request #3574 from consul/remove-dashboard-polls
Allow users to delete dashboard polls
This commit is contained in:
@@ -1,20 +1,16 @@
|
||||
class Dashboard::PollsController < Dashboard::BaseController
|
||||
helper_method :poll
|
||||
before_action :authorize_manage_polls
|
||||
|
||||
def index
|
||||
authorize! :manage_polls, proposal
|
||||
|
||||
@polls = Poll.for(proposal)
|
||||
end
|
||||
|
||||
def new
|
||||
authorize! :manage_polls, proposal
|
||||
@poll = Poll.new
|
||||
end
|
||||
|
||||
def create
|
||||
authorize! :manage_polls, proposal
|
||||
|
||||
@poll = Poll.new(poll_params.merge(author: current_user, related: proposal))
|
||||
if @poll.save
|
||||
redirect_to proposal_dashboard_polls_path(proposal), notice: t("flash.actions.create.poll")
|
||||
@@ -24,12 +20,9 @@ class Dashboard::PollsController < Dashboard::BaseController
|
||||
end
|
||||
|
||||
def edit
|
||||
authorize! :manage_polls, proposal
|
||||
end
|
||||
|
||||
def update
|
||||
authorize! :manage_polls, proposal
|
||||
|
||||
respond_to do |format|
|
||||
if poll.update(poll_params)
|
||||
format.html { redirect_to proposal_dashboard_polls_path(proposal),
|
||||
@@ -42,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
|
||||
@@ -70,4 +73,8 @@ class Dashboard::PollsController < Dashboard::BaseController
|
||||
def documents_attributes
|
||||
[:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
|
||||
end
|
||||
|
||||
def authorize_manage_polls
|
||||
authorize! :manage_polls, proposal
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<%= form_for [proposal, :dashboard, poll] do |f| %>
|
||||
<div class="row expanded">
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= f.text_field :name %>
|
||||
<%= f.text_field :name, label: t("dashboard.polls.form.name") %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -573,7 +573,12 @@ 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
|
||||
question_fields:
|
||||
remove_question: Remove question
|
||||
|
||||
@@ -573,7 +573,12 @@ 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
|
||||
question_fields:
|
||||
remove_question: Borrar pregunta
|
||||
|
||||
@@ -12,7 +12,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
|
||||
|
||||
@@ -150,6 +150,7 @@ describe "Admin polls" do
|
||||
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user