From 6bd42874e8f2ab4c394b018c804e510d17051f29 Mon Sep 17 00:00:00 2001 From: maxwell Date: Tue, 14 Dec 2010 17:36:14 -0800 Subject: [PATCH] adding a task to fix person object with spaces in their name --- lib/rake_helpers.rb | 49 ++++++++++++++++++++++++++++++++++++ lib/tasks/migrations.rake | 26 +++++++++++++------ spec/lib/rake_helper_spec.rb | 30 ++++++++++++++++++++++ 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/lib/rake_helpers.rb b/lib/rake_helpers.rb index 818875cb3..68b82e48c 100644 --- a/lib/rake_helpers.rb +++ b/lib/rake_helpers.rb @@ -28,4 +28,53 @@ module RakeHelpers end churn_through end + + def fix_diaspora_handle_spaces(test = true) + offenders = {} + space_people = Person.all(:diaspora_handle => / /, :url => APP_CONFIG[:pod_url]) # this is every person with a space.... + + + + #these people dont even have users.... they are totally messed up + totally_messed_up_people = space_people.find_all{|x| x.owner.nil?} + totally_messed_up_people.each{|x| x.delete} + + + space_people = Person.all(:diaspora_handle => / /, :owner_id.ne => nil, :url => APP_CONFIG[:pod_url]) # this is every person with a space.... + + + space_people.each do |person| + user = person.owner + new_diaspora_handle = "#{user.username}@#{APP_CONFIG[:pod_uri].host}" + + user.my_posts.all.each do |post| + post.diaspora_handle = new_diaspora_handle + if test + (puts "TEST: saving post w/id #{post.id}") + else + post.save(:safe => true) + end + end + + person.diaspora_handle = new_diaspora_handle + if test + (puts "TEST:saving person w/handle #{person.diaspora_handle}") + else + person.save(:safe => true) + end + +mail = < false) + end + p = Factory(:person) + p.diaspora_handle = "bubblegoose @#{APP_CONFIG[:pod_uri].host}" + p.url = APP_CONFIG[:pod_url] + p.save(:validate => false) + end + + it 'should fix diaspora handles' do + + + RakeHelpers::fix_diaspora_handle_spaces(false) + + Person.all.all?{|x| !x.diaspora_handle.include?(" ")}.should == true + end + + it 'should delete broken space people with no users' do + expect{ + RakeHelpers::fix_diaspora_handle_spaces(false) + }.to change(Person, :count).by(-1) + end end end