Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created March 24, 2010 08:41
Show Gist options
  • Save patrick99e99/342102 to your computer and use it in GitHub Desktop.
Save patrick99e99/342102 to your computer and use it in GitHub Desktop.
def set_cover(should_be_cover, photo)
if should_be_cover
cover = photo
elsif cover.nil?
# set album cover to first photo album if no cover has been set.
cover = photos.first
end
end
require 'spec_helper'
describe PhotoAlbum do
before(:each) do
@photo_album = stub_model(PhotoAlbum, :id => 1)
@photo = stub_model(Photo, :photo_album_id => @photo_album.id)
end
describe 'saving a photo' do
it "should set the cover of the album to the curent photo when 'album cover' is checked" do
@photo_album.set_cover(true, @photo)
@photo_album.cover.should == @photo
end
it "should set the cover to the album's first photo when there are multiple photos, and there's currently no cover set and 'album cover' is not checked" do
5.times do
@photo_album << stub_model(Photo, :photo_album_id => @photo_album.id)
end
@photo_album.set_cover(false, @photo)
@photo_album.cover.should_not == @photo
end
it "should set the cover to the the photo if there are no other photos in the album" do
@photo_album.set_cover(false, @photo)
@photo_album.cover.should == @photo
end
end
end
def create
@photo = Photo.new(params[:id])
if @photo.save && @photo.photo_album.set_cover(params[:photo_album_cover], @photo)
redirect_to photos_path
else
render :new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment