From 6fcf788e02ff6731733cb3bc6abb5dba63fa0ac9 Mon Sep 17 00:00:00 2001 From: Daniela Feitosa Date: Thu, 12 Feb 2015 23:28:08 -0300 Subject: [PATCH] [custom_forms] destroy answers when remove field --- plugins/custom_forms/lib/custom_forms_plugin/field.rb | 2 +- plugins/custom_forms/lib/custom_forms_plugin/submission.rb | 2 +- plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/plugins/custom_forms/lib/custom_forms_plugin/field.rb b/plugins/custom_forms/lib/custom_forms_plugin/field.rb index 7d53b75..911555b 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/field.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/field.rb @@ -6,7 +6,7 @@ class CustomFormsPlugin::Field < ActiveRecord::Base attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :select_field_type, :alternatives_attributes belongs_to :form, :class_name => 'CustomFormsPlugin::Form' - has_many :answers, :class_name => 'CustomFormsPlugin::Answer' + has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy has_many :alternatives, :order => 'position', :class_name => 'CustomFormsPlugin::Alternative' accepts_nested_attributes_for :alternatives, :allow_destroy => true diff --git a/plugins/custom_forms/lib/custom_forms_plugin/submission.rb b/plugins/custom_forms/lib/custom_forms_plugin/submission.rb index 154a736..b0d91c8 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/submission.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/submission.rb @@ -52,7 +52,7 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord self.answers.each do |answer| answer.valid? answer.errors.each do |attribute, msg| - self.errors.add answer.field.id.to_s.to_sym, msg + self.errors.add answer.field.id.to_s.to_sym, msg if answer.field.present? end end end diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb index a1468fa..c1849f1 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb @@ -33,6 +33,18 @@ class CustomFormsPlugin::FieldTest < ActiveSupport::TestCase assert_equal form.fields, [license_field] end + should 'destroy its answers after removing a field' do + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) + field = CustomFormsPlugin::Field.create!(:name => 'Project name', :form => form) + + CustomFormsPlugin::Answer.create(:field => field, :value => 'My Project') + CustomFormsPlugin::Answer.create(:field => field, :value => 'Other Project') + + assert_difference 'CustomFormsPlugin::Answer.count', -2 do + field.destroy + end + end + should 'have alternative if type is SelectField' do select = CustomFormsPlugin::Field.new(:name => 'select_field001', :type => 'CustomFormsPlugin::SelectField') assert !select.save -- libgit2 0.21.2