Created
March 23, 2010 17:36
-
-
Save patrick99e99/341447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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