Commit 6fcf788e02ff6731733cb3bc6abb5dba63fa0ac9
1 parent
0484736d
Exists in
master
and in
29 other branches
[custom_forms] destroy answers when remove field
Also: - fix to avoid crash if there is some answer without a related field
Showing
3 changed files
with
14 additions
and
2 deletions
Show diff stats
plugins/custom_forms/lib/custom_forms_plugin/field.rb
@@ -6,7 +6,7 @@ class CustomFormsPlugin::Field < ActiveRecord::Base | @@ -6,7 +6,7 @@ class CustomFormsPlugin::Field < ActiveRecord::Base | ||
6 | attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :select_field_type, :alternatives_attributes | 6 | attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :select_field_type, :alternatives_attributes |
7 | 7 | ||
8 | belongs_to :form, :class_name => 'CustomFormsPlugin::Form' | 8 | belongs_to :form, :class_name => 'CustomFormsPlugin::Form' |
9 | - has_many :answers, :class_name => 'CustomFormsPlugin::Answer' | 9 | + has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy |
10 | 10 | ||
11 | has_many :alternatives, :order => 'position', :class_name => 'CustomFormsPlugin::Alternative' | 11 | has_many :alternatives, :order => 'position', :class_name => 'CustomFormsPlugin::Alternative' |
12 | accepts_nested_attributes_for :alternatives, :allow_destroy => true | 12 | accepts_nested_attributes_for :alternatives, :allow_destroy => true |
plugins/custom_forms/lib/custom_forms_plugin/submission.rb
@@ -52,7 +52,7 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord | @@ -52,7 +52,7 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord | ||
52 | self.answers.each do |answer| | 52 | self.answers.each do |answer| |
53 | answer.valid? | 53 | answer.valid? |
54 | answer.errors.each do |attribute, msg| | 54 | answer.errors.each do |attribute, msg| |
55 | - self.errors.add answer.field.id.to_s.to_sym, msg | 55 | + self.errors.add answer.field.id.to_s.to_sym, msg if answer.field.present? |
56 | end | 56 | end |
57 | end | 57 | end |
58 | end | 58 | end |
plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb
@@ -33,6 +33,18 @@ class CustomFormsPlugin::FieldTest < ActiveSupport::TestCase | @@ -33,6 +33,18 @@ class CustomFormsPlugin::FieldTest < ActiveSupport::TestCase | ||
33 | assert_equal form.fields, [license_field] | 33 | assert_equal form.fields, [license_field] |
34 | end | 34 | end |
35 | 35 | ||
36 | + should 'destroy its answers after removing a field' do | ||
37 | + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) | ||
38 | + field = CustomFormsPlugin::Field.create!(:name => 'Project name', :form => form) | ||
39 | + | ||
40 | + CustomFormsPlugin::Answer.create(:field => field, :value => 'My Project') | ||
41 | + CustomFormsPlugin::Answer.create(:field => field, :value => 'Other Project') | ||
42 | + | ||
43 | + assert_difference 'CustomFormsPlugin::Answer.count', -2 do | ||
44 | + field.destroy | ||
45 | + end | ||
46 | + end | ||
47 | + | ||
36 | should 'have alternative if type is SelectField' do | 48 | should 'have alternative if type is SelectField' do |
37 | select = CustomFormsPlugin::Field.new(:name => 'select_field001', :type => 'CustomFormsPlugin::SelectField') | 49 | select = CustomFormsPlugin::Field.new(:name => 'select_field001', :type => 'CustomFormsPlugin::SelectField') |
38 | assert !select.save | 50 | assert !select.save |