From f5fd0b011a9ef33ce0636113b9a51a6ac25597e2 Mon Sep 17 00:00:00 2001 From: maxwell Date: Fri, 9 Jul 2010 16:08:00 -0700 Subject: [PATCH] DG MS; upon friend deletion, all connected posts and comments are purged --- app/models/person.rb | 29 ++++++++++++++++++----------- app/models/user.rb | 15 ++++++++------- spec/models/person_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index 688e8fb8b..48999c664 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -17,32 +17,29 @@ class Person timestamps! - validates_presence_of :url + before_validation :clean_url + validates_presence_of :email, :url validates_format_of :url, :with => /^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix - #validates_uniqueness_of :url validates_true_for :url, :logic => lambda { self.url_unique?} - scope :friends, where(:_type => "Person", :active => true) - validates_presence_of :email - - before_validation :clean_url - + after_destroy :remove_all_traces + scope :friends, where(:_type => "Person", :active => true) + + def real_name "#{profile.first_name.to_s} #{profile.last_name.to_s}" end - - def key GPGME::Ctx.new.get_key key_fingerprint end + + protected - - def url_unique? same_url = Person.first(:url => self.url) return same_url.nil? || same_url.id == self.id @@ -55,4 +52,14 @@ class Person self.url = self.url + '/' if self.url[-1,1] != '/' end end + + private + + def remove_all_traces + self.posts.delete_all + Comment.delete_all(:person_id => self.id) + end + + + end diff --git a/app/models/user.rb b/app/models/user.rb index c8801b87f..a7bdcd294 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -36,11 +36,6 @@ class User < Person end end - - def do_bad_things - self.password_confirmation = self.password - end - def accept_friend_request(friend_request_id) request = Request.where(:id => friend_request_id).first request.activate_friend @@ -53,7 +48,6 @@ class User < Person def ignore_friend_request(friend_request_id) request = Request.where(:id => friend_request_id).first person = request.person - person.destroy unless person.active request.destroy end @@ -67,10 +61,17 @@ class User < Person end end + + ###Helpers############ def mine?(post) self == post.person end - + + def do_bad_things + self.password_confirmation = self.password + end + + protected def assign_key diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 9e9b40abe..69b5ff29a 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -34,4 +34,28 @@ describe Person do end + it 'should delete all of user upon user deletion' do + Factory.create(:user) + + f = Factory.create(:person) + p = Factory.create(:person) + Factory.create(:status_message, :person => f) + Factory.create(:blog, :person => f) + Factory.create(:bookmark, :person => f) + Factory.create(:status_message, :person => f) + s = Factory.create(:status_message, :person => p) + + Factory.create(:comment, :person_id => f.id, :text => "yes i do", :post => s) + Factory.create(:comment, :person_id => f.id, :text => "i love you", :post => s) + Factory.create(:comment, :person_id => f.id, :text => "hello", :post => s) + Factory.create(:comment, :person_id => p.id, :text => "you are creepy", :post => s) + + f.destroy + + Post.count.should == 1 + Comment.all.count.should == 1 + s.comments.count.should == 1 + end + + end