From 89962ba61a898fd3be86f807d400feb3325122d8 Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Thu, 25 Jun 2020 11:33:40 +0700 Subject: [PATCH] Allow deleting polls with answers including videos If a poll has a question with an answer containing a related video, an error was raised because the poll ID was referenced in another table. --- app/models/poll/question/answer.rb | 2 +- spec/system/admin/poll/polls_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 4b5d3e7f3..1141cfc4b 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -9,7 +9,7 @@ class Poll::Question::Answer < ApplicationRecord accepts_nested_attributes_for :documents, allow_destroy: true belongs_to :question, class_name: "Poll::Question" - has_many :videos, class_name: "Poll::Question::Answer::Video" + has_many :videos, class_name: "Poll::Question::Answer::Video", dependent: :destroy validates_translation :title, presence: true validates :given_order, presence: true, uniqueness: { scope: :question_id } diff --git a/spec/system/admin/poll/polls_spec.rb b/spec/system/admin/poll/polls_spec.rb index f30d246ad..6465f9e10 100644 --- a/spec/system/admin/poll/polls_spec.rb +++ b/spec/system/admin/poll/polls_spec.rb @@ -145,6 +145,19 @@ describe "Admin polls" do expect(Poll::Question::Answer.count).to eq(0) end + scenario "Can destroy polls with answers including videos", :js do + poll = create(:poll, name: "Do you support CONSUL?") + create(:poll_answer_video, poll: poll) + + visit admin_polls_path + + within(".poll", text: "Do you support CONSUL?") do + accept_confirm { click_link "Delete" } + end + + expect(page).to have_content "Poll deleted successfully" + end + scenario "Can't destroy poll with votes", :js do poll = create(:poll) create(:poll_question, poll: poll)