Allow to delete answers if the poll has not started yet

Deleting answers was not even possible. But it was possible to delete
questions. So we implemented the same behavior.
This commit is contained in:
Julian Herrero
2022-02-26 17:41:37 +07:00
committed by Javi Martín
parent 3a6e99cb8c
commit 14542df0de
10 changed files with 52 additions and 5 deletions

View File

@@ -9,6 +9,6 @@ class Admin::Poll::Questions::Answers::TableActionsComponent < ApplicationCompon
private private
def actions def actions
[:edit].select { |action| can?(action, answer) } [:edit, :destroy].select { |action| can?(action, answer) }
end end
end end

View File

@@ -30,6 +30,11 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end end
end end
def destroy
@answer.destroy!
redirect_to admin_question_path(@question), notice: t("admin.answers.destroy.success_notice")
end
def order_answers def order_answers
::Poll::Question::Answer.order_answers(params[:ordered_list]) ::Poll::Question::Answer.order_answers(params[:ordered_list])
head :ok head :ok

View File

@@ -96,7 +96,7 @@ module Abilities
!question.poll.started? !question.poll.started?
end end
can [:read, :order_answers], Poll::Question::Answer 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) can?(:update, answer.question)
end end
can :manage, Poll::Question::Answer::Video can :manage, Poll::Question::Answer::Video

View File

@@ -1159,6 +1159,8 @@ en:
title: New answer title: New answer
edit: edit:
title: Edit answer title: Edit answer
destroy:
success_notice: "Answer deleted successfully"
videos: videos:
index: index:
title: Videos title: Videos

View File

@@ -1158,6 +1158,8 @@ es:
title: Nueva respuesta title: Nueva respuesta
edit: edit:
title: Editar respuesta title: Editar respuesta
destroy:
success_notice: "Respuesta eliminada correctamente"
videos: videos:
index: index:
title: Vídeos title: Vídeos

View File

@@ -169,7 +169,7 @@ namespace :admin do
end end
resources :questions, shallow: true do 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 :answers, only: [], controller: "questions/answers" do
resources :images, controller: "questions/answers/images" resources :images, controller: "questions/answers/images"
resources :videos, controller: "questions/answers/videos", shallow: false resources :videos, controller: "questions/answers/videos", shallow: false

View File

@@ -3,19 +3,21 @@ require "rails_helper"
describe Admin::Poll::Questions::Answers::TableActionsComponent, controller: Admin::BaseController do describe Admin::Poll::Questions::Answers::TableActionsComponent, controller: Admin::BaseController do
before { sign_in(create(:administrator).user) } 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)) answer = create(:poll_question_answer, poll: create(:poll, :future))
render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer) render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer)
expect(page).to have_link "Edit" expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end 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)) answer = create(:poll_question_answer, poll: create(:poll))
render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer) render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer)
expect(page).not_to have_link "Edit" expect(page).not_to have_link "Edit"
expect(page).not_to have_button "Delete"
end end
end end

View File

@@ -85,4 +85,23 @@ describe Admin::Poll::Questions::AnswersController, :admin do
expect(future_answer.reload.title).to eq "New title" expect(future_answer.reload.title).to eq "New title"
end end
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 end

View File

@@ -127,8 +127,10 @@ describe Abilities::Administrator do
it { should be_able_to(:order_answers, Poll::Question::Answer) } 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(:create, future_poll_question_answer) }
it { should be_able_to(:update, 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(:create, current_poll_question_answer) }
it { should_not be_able_to(:update, 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) } it { should be_able_to(:manage, Poll::Question::Answer::Video) }

View File

@@ -71,6 +71,21 @@ describe "Answers", :admin do
expect("Another title").to appear_before("New title") expect("Another title").to appear_before("New title")
end 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 scenario "Reorder" do
question = create(:poll_question) question = create(:poll_question)
create(:poll_question_answer, question: question, title: "First", given_order: 1) create(:poll_question_answer, question: question, title: "First", given_order: 1)