From 4eba0595f90080d958c6ee26802f90caa475a166 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 4 Nov 2010 18:46:05 -0700 Subject: [PATCH] Fix 328 profile images dying, thanks to theBernd --- app/models/photo.rb | 12 +++++++++--- app/models/profile.rb | 4 ++-- spec/models/photo_spec.rb | 39 ++++++++++++++++++------------------- spec/models/profile_spec.rb | 3 --- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/app/models/photo.rb b/app/models/photo.rb index b52851af0..28cedf8ea 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -50,9 +50,9 @@ class Photo < Post end def ensure_user_picture - users = Person.all('profile.image_url' => image.url(:thumb_medium) ) - users.each{ |user| - user.profile.update_attributes!(:image_url => nil) + people = Person.all('profile.image_url' => absolute_url(:thumb_medium) ) + people.each{ |person| + person.profile.update_attributes(:image_url => nil) } end @@ -64,6 +64,12 @@ class Photo < Post true end + def absolute_url *args + pod_url = APP_CONFIG[:pod_url].dup + pod_url.chop! if APP_CONFIG[:pod_url][-1,1] == '/' + "#{pod_url}#{url(*args)}" + end + def self.gen_random_string(len) chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a string = "" diff --git a/app/models/profile.rb b/app/models/profile.rb index 698b858d3..ce874c728 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -42,8 +42,8 @@ class Profile end def image_url= url - return if url.nil? || url.empty? - if url.match(/^https?:\/\//) + return image_url if url == '' + if url.nil? || url.match(/^https?:\/\//) super(url) else super(absolutify_local_url(url)) diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 275b2b40d..95057758e 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -76,29 +76,28 @@ describe Photo do binary.should == fixture_binary end - it 'should have a caption' do - @photo.image.store! File.open(@fixture_name) - @photo.caption = "cool story, bro" - @photo.save.should be_true - end + context 'with a saved photo' do + before do + @photo.image.store! File.open(@fixture_name) + end + it 'should have a caption' do + @photo.caption = "cool story, bro" + @photo.save.should be_true + end - it 'should remove its reference in user profile if it is referred' do - @photo.image.store! File.open(@fixture_name) - @photo.save + it 'should remove its reference in user profile if it is referred' do + @photo.save - @user.profile.image_url = @photo.image.url(:thumb_medium) - @user.save - @user.person.save + @user.profile.image_url = @photo.image.url(:thumb_medium) + @user.person.save + @photo.destroy + Person.find(@user.person.id).profile.image_url.should be_nil + end - @user.profile.image_url.should == @photo.image.url(:thumb_medium) - @photo.destroy - @user.reload.profile.image_url.should be nil - end - - it 'should not use the imported filename as the url' do - @photo.image.store! File.open(@fixture_name) - @photo.image.url.include?(@fixture_filename).should be false - @photo.image.url(:thumb_medium).include?("/" + @fixture_filename).should be false + it 'should not use the imported filename as the url' do + @photo.image.url.include?(@fixture_filename).should be false + @photo.image.url(:thumb_medium).include?("/" + @fixture_filename).should be false + end end describe 'non-image files' do diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index 7c9bfa5a1..8ce848a85 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -54,9 +54,6 @@ describe Profile do it 'ignores an empty string' do lambda {@profile.image_url = ""}.should_not change(@profile, :image_url) end - it 'ignores nil' do - lambda {@profile.image_url = nil}.should_not change(@profile, :image_url) - end it 'makes relative urls absolute' do @profile.image_url = @photo.url(:thumb_medium) @profile.image_url.should == "#{@pod_url}#{@photo.url(:thumb_medium)}"