diff --git a/app/models/photo.rb b/app/models/photo.rb index f52dc0d1c..3498a2e3b 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -26,9 +26,13 @@ class Photo < Post after_save :log_save_inspection validates_true_for :album_id, :logic => lambda {self.validate_album_person} + before_destroy :ensure_user_picture + + def validate_album_person album.person_id == person_id end + def remote_photo @remote_photo ||= User.owner.url.chop + image.url(:scaled_full) end @@ -40,4 +44,11 @@ class Photo < Post image.store! Rails.logger.info("Setting remote photo with id #{id}") end + + def ensure_user_picture + user = User.owner + if user.profile.image_url == image.url(:thumb_medium) + user.profile.update_attributes!(:image_url => nil) + end + end end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 5eb9ea805..3646d0626 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -25,8 +25,7 @@ describe Photo do binary.should == fixture_binary end - it 'must have an album' do - + it 'must have an album' do photo = Photo.new(:person => @user) photo.image = File.open(@fixture_name) photo.save @@ -36,6 +35,18 @@ describe Photo do Photo.first.album.name.should == 'foo' end + it 'should remove its reference in user profile if it is referred' do + @photo.image.store! File.open(@fixture_name) + @photo.save + + @user.profile.image_url = @photo.image.url(:thumb_medium) + @user.save + + User.first.profile.image_url.should == @photo.image.url(:thumb_medium) + @photo.destroy + User.first.profile.image_url.should be nil + end + describe 'non-image files' do it 'should not store' do file = File.open(@fail_fixture_name) @@ -52,9 +63,6 @@ describe Photo do @photo.image = file @photo.save.should == false end - - - end describe 'with encryption' do @@ -80,12 +88,12 @@ describe Photo do @photo.image = File.open(@fixture_name) @photo.image.store! @photo.save - xml = @photo.to_xml.to_s xml.include?("bp.jpeg").should be true end + it 'should have an album id on serialization' do @photo.image = File.open(@fixture_name)