diff --git a/app/components/admin/poll/questions/answers/table_actions_component.rb b/app/components/admin/poll/questions/answers/table_actions_component.rb index fcec11fce..577e099b1 100644 --- a/app/components/admin/poll/questions/answers/table_actions_component.rb +++ b/app/components/admin/poll/questions/answers/table_actions_component.rb @@ -9,6 +9,6 @@ class Admin::Poll::Questions::Answers::TableActionsComponent < ApplicationCompon private def actions - [:edit].select { |action| can?(action, answer) } + [:edit, :destroy].select { |action| can?(action, answer) } end end diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index c015f1ebe..844f3d494 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -30,6 +30,11 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController end end + def destroy + @answer.destroy! + redirect_to admin_question_path(@question), notice: t("admin.answers.destroy.success_notice") + end + def order_answers ::Poll::Question::Answer.order_answers(params[:ordered_list]) head :ok diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 96f4b581a..07f26a649 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -96,7 +96,7 @@ module Abilities !question.poll.started? end can [:read, :order_answers], Poll::Question::Answer - can [:create, :update], Poll::Question::Answer do |answer| + can [:create, :update, :destroy], Poll::Question::Answer do |answer| can?(:update, answer.question) end can :manage, Poll::Question::Answer::Video diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index e1a1bd5cf..3084abb6f 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -1159,6 +1159,8 @@ en: title: New answer edit: title: Edit answer + destroy: + success_notice: "Answer deleted successfully" videos: index: title: Videos diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index acae33b58..3ea4905cc 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -1158,6 +1158,8 @@ es: title: Nueva respuesta edit: title: Editar respuesta + destroy: + success_notice: "Respuesta eliminada correctamente" videos: index: title: VĂ­deos diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 84d3e6b68..56a2cf77e 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -169,7 +169,7 @@ namespace :admin do end resources :questions, shallow: true do - resources :answers, except: [:index, :show, :destroy], controller: "questions/answers", shallow: false + resources :answers, except: [:index, :show], controller: "questions/answers", shallow: false resources :answers, only: [], controller: "questions/answers" do resources :images, controller: "questions/answers/images" resources :videos, controller: "questions/answers/videos", shallow: false diff --git a/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb b/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb index 3ed96b8da..4e6080a11 100644 --- a/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb +++ b/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb @@ -3,19 +3,21 @@ require "rails_helper" describe Admin::Poll::Questions::Answers::TableActionsComponent, controller: Admin::BaseController do before { sign_in(create(:administrator).user) } - it "displays the edit action when the poll has not started" do + it "displays the edit and destroy actions when the poll has not started" do answer = create(:poll_question_answer, poll: create(:poll, :future)) render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer) expect(page).to have_link "Edit" + expect(page).to have_button "Delete" end - it "does not display the edit action when the poll has started" do + it "does not display the edit and destroy actions when the poll has started" do answer = create(:poll_question_answer, poll: create(:poll)) render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer) expect(page).not_to have_link "Edit" + expect(page).not_to have_button "Delete" end end diff --git a/spec/controllers/admin/poll/questions/answers_controller_spec.rb b/spec/controllers/admin/poll/questions/answers_controller_spec.rb index 6ecccf37b..bdee4c59a 100644 --- a/spec/controllers/admin/poll/questions/answers_controller_spec.rb +++ b/spec/controllers/admin/poll/questions/answers_controller_spec.rb @@ -85,4 +85,23 @@ describe Admin::Poll::Questions::AnswersController, :admin do expect(future_answer.reload.title).to eq "New title" end end + + describe "DELETE destroy" do + it "is not possible for an already started poll" do + current_answer = create(:poll_question_answer, question: current_question) + delete :destroy, params: { question_id: current_question, id: current_answer } + + expect(flash[:alert]).to eq "You do not have permission to carry out the action 'destroy' on Answer." + expect(Poll::Question::Answer.count).to eq 1 + end + + it "is possible for a not started poll" do + future_answer = create(:poll_question_answer, question: future_question) + delete :destroy, params: { question_id: future_question, id: future_answer } + + expect(response).to redirect_to admin_question_path(future_question) + expect(flash[:notice]).to eq "Answer deleted successfully" + expect(Poll::Question::Answer.count).to eq 0 + end + end end diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb index dfd1a76f0..853fcee82 100644 --- a/spec/models/abilities/administrator_spec.rb +++ b/spec/models/abilities/administrator_spec.rb @@ -127,8 +127,10 @@ describe Abilities::Administrator do it { should be_able_to(:order_answers, Poll::Question::Answer) } it { should be_able_to(:create, future_poll_question_answer) } it { should be_able_to(:update, future_poll_question_answer) } + it { should be_able_to(:destroy, future_poll_question_answer) } it { should_not be_able_to(:create, current_poll_question_answer) } it { should_not be_able_to(:update, current_poll_question_answer) } + it { should_not be_able_to(:destroy, current_poll_question_answer) } it { should be_able_to(:manage, Poll::Question::Answer::Video) } diff --git a/spec/system/admin/poll/questions/answers/answers_spec.rb b/spec/system/admin/poll/questions/answers/answers_spec.rb index 9e3ac1a4f..21e53e2b1 100644 --- a/spec/system/admin/poll/questions/answers/answers_spec.rb +++ b/spec/system/admin/poll/questions/answers/answers_spec.rb @@ -71,6 +71,21 @@ describe "Answers", :admin do expect("Another title").to appear_before("New title") end + scenario "Destroy" do + answer = create(:poll_question_answer, poll: future_poll, title: "I'm not useful") + + visit admin_question_path(answer.question) + + within("tr", text: "I'm not useful") do + accept_confirm("Are you sure? This action will delete \"I'm not useful\" and can't be undone.") do + click_button "Delete" + end + end + + expect(page).to have_content "Answer deleted successfully" + expect(page).not_to have_content "I'm not useful" + end + scenario "Reorder" do question = create(:poll_question) create(:poll_question_answer, question: question, title: "First", given_order: 1)