Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created March 23, 2010 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save patrick99e99/341447 to your computer and use it in GitHub Desktop.
Save patrick99e99/341447 to your computer and use it in GitHub Desktop.
class Photo
belongs_to :photo_album
def set_cover(cover)
if cover
photo_album.cover = self
elsif photo_album.cover.nil?
# set album cover to first photo album if no cover has been set.
photo_album.cover = photo_album.photos.first
end
end
end
require 'spec_helper'
describe Photo do
describe 'creating a new photo' do
before(:each) do
@photo_album = stub_model(PhotoAlbum, :name => 'a photo album', :id => 1)
@photo = stub_model(Photo, :name => 'a photo', :photo_album_id => @photo_album)
end
it 'should set the first photo to be the cover of the album if no cover is set' do
@photo.stub!(:set_cover).with(false)
@photo.photo_album.cover.should == @photo_album.photos.first
end
it 'should set the album cover to the current photo' do
@photo.stub!(:set_cover).with(true)
@photo.photo_album.cover.should == @photo_album.cover
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment