DG MS all photos of a album are destroyed on album deletion

This commit is contained in:
maxwell 2010-07-21 17:05:21 -07:00
parent 21bd875a3f
commit 0d268905e7
3 changed files with 26 additions and 4 deletions

View file

@ -10,6 +10,8 @@ class Album
validates_presence_of :name
before_destroy :destroy_photos
def prev_photo(photo)
n_photo = self.photos.where(:created_at.lt => photo.created_at).sort(:created_at.desc).first
n_photo ? n_photo : self.photos.sort(:created_at.desc).first
@ -20,4 +22,10 @@ class Album
p_photo ? p_photo : self.photos.sort(:created_at.desc).last
end
protected
def destroy_photos
photos.each{|p| p.destroy}
end
end

View file

@ -30,12 +30,24 @@ describe Album do
album.photos.count.should == 1
end
it 'should remove all photos on album delete' do
photo_one = Photo.create(:person => @user, :album => @album, :created_at => Time.now)
photo_two = Photo.create(:person => @user, :album => @album, :created_at => Time.now-1)
photo_three = Photo.create(:person => @user, :album => @album, :created_at => Time.now-2)
@album.photos += [photo_one, photo_two, photo_three]
Photo.all.count.should == 3
@album.destroy
Photo.all.count.should == 0
end
describe 'traversing' do
before do
@album = Album.create(:name => "test collection")
@photo_one = Photo.create(:person => @user, :created_at => Time.now)
@photo_two = Photo.create(:person => @user, :created_at => Time.now-1)
@photo_three = Photo.create(:person => @user, :created_at => Time.now-2)
@photo_one = Photo.create(:person => @user, :album => @album, :created_at => Time.now)
@photo_two = Photo.create(:person => @user, :album => @album, :created_at => Time.now+1)
@photo_three = Photo.create(:person => @user, :album => @album, :created_at => Time.now+2)
@album.photos += [@photo_one, @photo_two, @photo_three]
end

View file

@ -34,8 +34,10 @@ describe Photo do
it 'must have an album' do
photo = Photo.create(:person => @user)
photo = Photo.new(:person => @user)
photo.valid?.should be false
photo.album = Album.new(:name => "foo")
photo.save
Photo.first.album.name.should == 'foo'
end
end