noosfero | [custom_forms] destroy answers when remove field

Daniela Feitosa gitlab at gitlab.com
Fri Feb 13 00:34:29 BRST 2015


Daniela Feitosa pushed to refs/heads/master at <a href="https://gitlab.com/noosfero/noosfero">Noosfero / noosfero</a>

Commits:
<a href="https://gitlab.com/noosfero/noosfero/commit/6fcf788e02ff6731733cb3bc6abb5dba63fa0ac9">6fcf788e</a> by Daniela Feitosa
[custom_forms] destroy answers when remove field

Also:
- fix to avoid crash if there is some answer without a related field

- - - - -


Changes:

=====================================
plugins/custom_forms/lib/custom_forms_plugin/field.rb
=====================================
--- 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

=====================================
plugins/custom_forms/lib/custom_forms_plugin/submission.rb
=====================================
--- 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

=====================================
plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb
=====================================
--- 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150213/702bd35e/attachment-0001.html>


More information about the Noosfero-dev mailing list