From 36c2effa1b1cb70267ab53906f97e5cbcc102383 Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Thu, 25 Sep 2008 19:15:39 -0300 Subject: [PATCH] ActionItem732: destroy tasks when requestor or target is destroyed --- app/models/person.rb | 2 ++ app/models/profile.rb | 2 +- test/unit/person_test.rb | 9 +++++++++ test/unit/profile_test.rb | 9 +++++++++ test/unit/task_test.rb | 8 ++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/models/person.rb b/app/models/person.rb index cff5598..27e86e0 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -6,6 +6,8 @@ class Person < Profile has_many :friendships has_many :friends, :class_name => 'Person', :through => :friendships + has_many :requested_tasks, :class_name => 'Task', :foreign_key => :requestor_id, :dependent => :destroy + def suggested_friend_groups (friend_groups + [ _('friends'), _('work'), _('school'), _('family') ]).map {|i| i if !i.empty?}.compact.uniq end diff --git a/app/models/profile.rb b/app/models/profile.rb index b3e2e48..bab7c8c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -98,7 +98,7 @@ class Profile < ActiveRecord::Base has_many :consumptions has_many :consumed_product_categories, :through => :consumptions, :source => :product_category - has_many :tasks, :foreign_key => :target_id + has_many :tasks, :foreign_key => :target_id, :dependent => :destroy has_many :profile_categorizations, :conditions => [ 'categories_profiles.virtual = ?', false ] has_many :categories, :through => :profile_categorizations diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 4a21b92..0609413 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -306,4 +306,13 @@ class PersonTest < Test::Unit::TestCase assert_kind_of Person, p.template end + should 'destroy all task that it requested when destroyed' do + p = create_user('test_profile').person + + assert_no_difference Task, :count do + Task.create(:requestor => p) + p.destroy + end + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index eecb58d..e8d18df 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -929,6 +929,15 @@ class ProfileTest < Test::Unit::TestCase assert_equal 6, p.boxes_limit end + should 'destroy tasks requested to it when destroyed' do + p = Profile.create!(:name => 'test_profile', :identifier => 'test_profile') + + assert_no_difference Task, :count do + Task.create(:target => p) + p.destroy + end + end + private def assert_invalid_identifier(id) diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 601c886..817ee0d 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -182,6 +182,14 @@ class TaskTest < Test::Unit::TestCase t = Task.new assert_equal :perform_task, t.permission end + + should 'be destroyed when requestor destroyed' do + user = create_user('test_user').person + assert_no_difference Task, :count do + Task.create(:requestor => user) + user.destroy + end + end protected -- libgit2 0.21.2