Merge pull request #3574 from consul/remove-dashboard-polls

Allow users to delete dashboard polls
This commit is contained in:
Alberto
2019-06-01 11:06:34 +02:00
committed by GitHub
8 changed files with 65 additions and 13 deletions

View File

@@ -1,20 +1,16 @@
class Dashboard::PollsController < Dashboard::BaseController class Dashboard::PollsController < Dashboard::BaseController
helper_method :poll helper_method :poll
before_action :authorize_manage_polls
def index def index
authorize! :manage_polls, proposal
@polls = Poll.for(proposal) @polls = Poll.for(proposal)
end end
def new def new
authorize! :manage_polls, proposal
@poll = Poll.new @poll = Poll.new
end end
def create def create
authorize! :manage_polls, proposal
@poll = Poll.new(poll_params.merge(author: current_user, related: proposal)) @poll = Poll.new(poll_params.merge(author: current_user, related: proposal))
if @poll.save if @poll.save
redirect_to proposal_dashboard_polls_path(proposal), notice: t("flash.actions.create.poll") redirect_to proposal_dashboard_polls_path(proposal), notice: t("flash.actions.create.poll")
@@ -24,12 +20,9 @@ class Dashboard::PollsController < Dashboard::BaseController
end end
def edit def edit
authorize! :manage_polls, proposal
end end
def update def update
authorize! :manage_polls, proposal
respond_to do |format| respond_to do |format|
if poll.update(poll_params) if poll.update(poll_params)
format.html { redirect_to proposal_dashboard_polls_path(proposal), format.html { redirect_to proposal_dashboard_polls_path(proposal),
@@ -42,6 +35,16 @@ class Dashboard::PollsController < Dashboard::BaseController
end end
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 private
def poll def poll
@@ -70,4 +73,8 @@ class Dashboard::PollsController < Dashboard::BaseController
def documents_attributes def documents_attributes
[:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
end end
def authorize_manage_polls
authorize! :manage_polls, proposal
end
end end

View File

@@ -1,7 +1,7 @@
<%= form_for [proposal, :dashboard, poll] do |f| %> <%= form_for [proposal, :dashboard, poll] do |f| %>
<div class="row expanded"> <div class="row expanded">
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= f.text_field :name %> <%= f.text_field :name, label: t("dashboard.polls.form.name") %>
</div> </div>
</div> </div>

View File

@@ -31,5 +31,11 @@
class: "js-submit-on-change" %> class: "js-submit-on-change" %>
<% end %> <% end %>
<p class="help-text"><%= t("dashboard.polls.poll.show_results_help") %></p> <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>
</div> </div>

View File

@@ -573,7 +573,12 @@ en:
edit_poll: Edit survey edit_poll: Edit survey
show_results: Show results 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 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: form:
name: Survey title
add_question: Add question add_question: Add question
question_fields: question_fields:
remove_question: Remove question remove_question: Remove question

View File

@@ -573,7 +573,12 @@ es:
edit_poll: Editar encuesta edit_poll: Editar encuesta
show_results: Mostrar resultados show_results: Mostrar resultados
show_results_help: Si marcas esta casilla los resultados serán públicos y todos los usuarios podrán verlos 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: form:
name: Título de la encuesta
add_question: Añadir pregunta add_question: Añadir pregunta
question_fields: question_fields:
remove_question: Borrar pregunta remove_question: Borrar pregunta

View File

@@ -12,7 +12,7 @@ resources :proposals do
resources :achievements, only: [:index], controller: "dashboard/achievements" resources :achievements, only: [:index], controller: "dashboard/achievements"
resources :successful_supports, only: [:index], controller: "dashboard/successful_supports" resources :successful_supports, only: [:index], controller: "dashboard/successful_supports"
resources :supports, only: [:index], controller: "dashboard/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 :mailing, only: [:index, :new, :create], controller: "dashboard/mailing"
resources :poster, only: [:index, :new], controller: "dashboard/poster" resources :poster, only: [:index, :new], controller: "dashboard/poster"
resources :actions, only: [], controller: "dashboard/actions" do resources :actions, only: [], controller: "dashboard/actions" do

View File

@@ -150,6 +150,7 @@ describe "Admin polls" do
expect(page).to have_content("Poll deleted successfully") expect(page).to have_content("Poll deleted successfully")
expect(page).not_to have_content(poll.name) expect(page).not_to have_content(poll.name)
expect(Poll::Question.count).to eq(0) expect(Poll::Question.count).to eq(0)
expect(Poll::Question::Answer.count). to eq(0) expect(Poll::Question::Answer.count). to eq(0)
end end

View File

@@ -153,6 +153,34 @@ describe "Polls" do
end end
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 scenario "View results not available for upcoming polls" do
poll = create(:poll, related: proposal, starts_at: 1.week.from_now) poll = create(:poll, related: proposal, starts_at: 1.week.from_now)