Commit 3133df278c62e533376db76ca88020606bb28089

Authored by Daniela Feitosa
1 parent 4c91f481

Destroying forms when its profile is destroyed

Also added migration to remove existent forms without profile

Signed-off-by: Ana Losnak <analosnak@gmail.com>

(ActionItem3144)
plugins/custom_forms/db/migrate/20140619213950_remove_forms_without_profile.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +class RemoveFormsWithoutProfile < ActiveRecord::Migration
  2 + def self.up
  3 + CustomFormsPlugin::Form.find_each do |form|
  4 + form.destroy if form.profile.nil?
  5 + end
  6 + end
  7 +
  8 + def self.down
  9 + say "This migration is irreversible."
  10 + end
  11 +end
plugins/custom_forms/lib/custom_forms_plugin/form.rb
@@ -19,7 +19,7 @@ class CustomFormsPlugin::Form &lt; Noosfero::Plugin::ActiveRecord @@ -19,7 +19,7 @@ class CustomFormsPlugin::Form &lt; Noosfero::Plugin::ActiveRecord
19 end 19 end
20 20
21 after_destroy do |form| 21 after_destroy do |form|
22 - tasks = CustomFormsPlugin::MembershipSurvey.from(form.profile).opened.select { |t| t.form_id == form.id } 22 + tasks = CustomFormsPlugin::MembershipSurvey.opened.select { |t| t.form_id == form.id }
23 tasks.each {|task| task.cancel} 23 tasks.each {|task| task.cancel}
24 end 24 end
25 25
plugins/custom_forms/lib/ext/profile.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +require_dependency 'profile'
  2 +
  3 +class Profile
  4 +
  5 + has_many :forms, :class_name => 'CustomFormsPlugin::Form', :dependent => :destroy
  6 +
  7 +end
plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb
@@ -274,4 +274,15 @@ class CustomFormsPlugin::FormTest &lt; ActiveSupport::TestCase @@ -274,4 +274,15 @@ class CustomFormsPlugin::FormTest &lt; ActiveSupport::TestCase
274 s2.reload 274 s2.reload
275 end 275 end
276 end 276 end
  277 +
  278 + should 'destroy forms after profile is destroyed' do
  279 + profile = fast_create(Profile)
  280 + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software')
  281 + profile.destroy
  282 +
  283 + assert_raise ActiveRecord::RecordNotFound do
  284 + CustomFormsPlugin::Form.find(form.id)
  285 + end
  286 + end
  287 +
277 end 288 end