diff --git a/app/controllers/dashboard/polls_controller.rb b/app/controllers/dashboard/polls_controller.rb index f97f91281..89dff143b 100644 --- a/app/controllers/dashboard/polls_controller.rb +++ b/app/controllers/dashboard/polls_controller.rb @@ -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 diff --git a/app/views/dashboard/polls/_form.html.erb b/app/views/dashboard/polls/_form.html.erb index ab4f32dcf..9839401ed 100644 --- a/app/views/dashboard/polls/_form.html.erb +++ b/app/views/dashboard/polls/_form.html.erb @@ -1,7 +1,7 @@ <%= form_for [proposal, :dashboard, poll] do |f| %>
- <%= f.text_field :name %> + <%= f.text_field :name, label: t("dashboard.polls.form.name") %>
diff --git a/app/views/dashboard/polls/_poll.html.erb b/app/views/dashboard/polls/_poll.html.erb index d41fa54e1..8396c0ad7 100644 --- a/app/views/dashboard/polls/_poll.html.erb +++ b/app/views/dashboard/polls/_poll.html.erb @@ -1,8 +1,8 @@

- <%= link_to poll.title, - proposal_poll_path(proposal, poll), + <%= link_to poll.title, + proposal_poll_path(proposal, poll), target: "_blank" %>

@@ -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 %>
@@ -31,5 +31,11 @@ class: "js-submit-on-change" %> <% end %>

<%= t("dashboard.polls.poll.show_results_help") %>

+ + <%= 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" %>
diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 8fa3e0b2e..1cc83c602 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -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 diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index a15374549..b7a527c6a 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -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 diff --git a/config/routes/proposal.rb b/config/routes/proposal.rb index 131c1f402..eed13dc2d 100644 --- a/config/routes/proposal.rb +++ b/config/routes/proposal.rb @@ -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 diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index c1278e74e..398ba4925 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -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 diff --git a/spec/features/dashboard/polls_spec.rb b/spec/features/dashboard/polls_spec.rb index d9e57a589..5b5c6c3a3 100644 --- a/spec/features/dashboard/polls_spec.rb +++ b/spec/features/dashboard/polls_spec.rb @@ -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)