Merge pull request #49 from medialab-prado/47-fix-bug-deleting-questions

Fix stack level too deep bug when deleting questions with answers
This commit is contained in:
Fernando Blat
2017-01-03 09:23:56 +01:00
committed by GitHub
3 changed files with 52 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
class Legislation::Answer < ActiveRecord::Base class Legislation::Answer < ActiveRecord::Base
belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :answers, counter_cache: true belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', inverse_of: :answers, counter_cache: true
belongs_to :question_option, class_name: 'Legislation::QuestionOption', foreign_key: 'legislation_question_option_id', dependent: :destroy, inverse_of: :answers, counter_cache: true belongs_to :question_option, class_name: 'Legislation::QuestionOption', foreign_key: 'legislation_question_option_id', inverse_of: :answers, counter_cache: true
belongs_to :user, dependent: :destroy, inverse_of: :legislation_answers belongs_to :user, dependent: :destroy, inverse_of: :legislation_answers
validates :question, presence: true, uniqueness: { scope: :user_id} validates :question, presence: true, uniqueness: { scope: :user_id}

View File

@@ -86,4 +86,22 @@ feature 'Admin legislation questions' do
expect(page).to have_content 'Question 2b' expect(page).to have_content 'Question 2b'
end end
end end
context 'Delete' do
scenario 'Legislation question', :js do
process = create(:legislation_process, title: 'An example legislation process')
create(:legislation_question, title: 'Question 1', process: process)
question = create(:legislation_question, title: 'Question 2', process: process)
question_option = create(:legislation_question_option, question: question, value: 'Yes')
create(:legislation_answer, question: question, question_option: question_option)
visit edit_admin_legislation_process_question_path(process, question)
click_link 'Delete'
expect(page).to have_content 'Questions'
expect(page).to have_content 'Question 1'
expect(page).to_not have_content 'Question 2'
end
end
end end

View File

@@ -6,4 +6,36 @@ RSpec.describe Legislation::Question, type: :model do
it "should be valid" do it "should be valid" do
expect(legislation_question).to be_valid expect(legislation_question).to be_valid
end end
context "can be deleted" do
example "when it has no options or answers" do
question = create(:legislation_question)
expect do
question.destroy
end.to change { Legislation::Question.count }.by(-1)
end
example "when it has options but no answers" do
question = create(:legislation_question)
create(:legislation_question_option, question: question, value: "Yes")
create(:legislation_question_option, question: question, value: "No")
expect do
question.destroy
end.to change { Legislation::Question.count }.by(-1)
end
example "when it has options and answers" do
question = create(:legislation_question)
option_1 = create(:legislation_question_option, question: question, value: "Yes")
option_2 = create(:legislation_question_option, question: question, value: "No")
create(:legislation_answer, question: question, question_option: option_1)
create(:legislation_answer, question: question, question_option: option_2)
expect do
question.destroy
end.to change { Legislation::Question.count }.by(-1)
end
end
end end