Created
March 24, 2010 08:41
-
-
Save patrick99e99/342102 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
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 | |
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 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 |
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
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