diff --git a/plugins/custom_forms/db/migrate/20140619213950_remove_forms_without_profile.rb b/plugins/custom_forms/db/migrate/20140619213950_remove_forms_without_profile.rb new file mode 100644 index 0000000..549ab76 --- /dev/null +++ b/plugins/custom_forms/db/migrate/20140619213950_remove_forms_without_profile.rb @@ -0,0 +1,11 @@ +class RemoveFormsWithoutProfile < ActiveRecord::Migration + def self.up + CustomFormsPlugin::Form.find_each do |form| + form.destroy if form.profile.nil? + end + end + + def self.down + say "This migration is irreversible." + end +end diff --git a/plugins/custom_forms/lib/custom_forms_plugin/form.rb b/plugins/custom_forms/lib/custom_forms_plugin/form.rb index 9b67478..22802cf 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/form.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/form.rb @@ -19,7 +19,7 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord end after_destroy do |form| - tasks = CustomFormsPlugin::MembershipSurvey.from(form.profile).opened.select { |t| t.form_id == form.id } + tasks = CustomFormsPlugin::MembershipSurvey.opened.select { |t| t.form_id == form.id } tasks.each {|task| task.cancel} end diff --git a/plugins/custom_forms/lib/ext/profile.rb b/plugins/custom_forms/lib/ext/profile.rb new file mode 100644 index 0000000..bc15ccf --- /dev/null +++ b/plugins/custom_forms/lib/ext/profile.rb @@ -0,0 +1,7 @@ +require_dependency 'profile' + +class Profile + + has_many :forms, :class_name => 'CustomFormsPlugin::Form', :dependent => :destroy + +end diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb index 1b23413..34e584a 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb @@ -274,4 +274,15 @@ class CustomFormsPlugin::FormTest < ActiveSupport::TestCase s2.reload end end + + should 'destroy forms after profile is destroyed' do + profile = fast_create(Profile) + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software') + profile.destroy + + assert_raise ActiveRecord::RecordNotFound do + CustomFormsPlugin::Form.find(form.id) + end + end + end -- libgit2 0.21.2