Commit dc7ddee65ec19736b054bf0f76d3433581cbc1f0

Authored by Daniela Feitosa
1 parent aceb3bee

ActionItem758: removing friendships when person is destroyed

Showing 2 changed files with 17 additions and 1 deletions   Show diff stats
app/models/person.rb
... ... @@ -3,11 +3,15 @@ class Person < Profile
3 3  
4 4 acts_as_accessor
5 5  
6   - has_many :friendships
  6 + has_many :friendships, :dependent => :destroy
7 7 has_many :friends, :class_name => 'Person', :through => :friendships
8 8  
9 9 has_many :requested_tasks, :class_name => 'Task', :foreign_key => :requestor_id, :dependent => :destroy
10 10  
  11 + after_destroy do |person|
  12 + Friendship.find(:all, :conditions => { :friend_id => person.id}).each { |friendship| friendship.destroy }
  13 + end
  14 +
11 15 def suggested_friend_groups
12 16 (friend_groups + [ _('friends'), _('work'), _('school'), _('family') ]).map {|i| i if !i.empty?}.compact.uniq
13 17 end
... ...
test/unit/person_test.rb
... ... @@ -217,6 +217,18 @@ class PersonTest < Test::Unit::TestCase
217 217 assert_not_includes p1.friends(true), p2
218 218 end
219 219  
  220 + should 'destroy friendships when person is destroyed' do
  221 + p1 = create_user('testuser1').person
  222 + p2 = create_user('testuser2').person
  223 + p1.add_friend(p2, 'friends')
  224 + p2.add_friend(p1, 'friends')
  225 +
  226 + assert_difference Friendship, :count, -2 do
  227 + p1.destroy
  228 + end
  229 + assert_not_includes p2.friends(true), p1
  230 + end
  231 +
220 232 should 'return info name instead of name when info is setted' do
221 233 p = create_user('ze_maria').person
222 234 assert_equal 'ze_maria', p.name
... ...