Fix problem adding photos from photosets.

When we fetched photos from a photoset, the response object didn't
respond to to_a, breaking our controller code. This turned out to be due
to weirdness in FlickRaw's API:
https://github.com/hanklords/flickraw/issues/58
This commit is contained in:
Miles Gould
2013-07-18 14:16:22 +01:00
parent c977709235
commit 2b863cd62e
3 changed files with 27 additions and 7 deletions

View File

@@ -33,11 +33,10 @@ class PhotosController < ApplicationController
@flickr_auth = current_member.auth('flickr')
if @flickr_auth
@sets = current_member.flickr_sets
photos = current_member.flickr_photos(page, params[:set])
total = photos.instance_of?(FlickRaw::ResponseList) ? photos.total : 0
photos, total = current_member.flickr_photos(page, params[:set])
@photos = WillPaginate::Collection.create(page, 30, total) do |pager|
pager.replace photos.to_a
pager.replace photos
end
end

View File

@@ -150,19 +150,37 @@ class Member < ActiveRecord::Base
end
# Fetches a collection of photos from Flickr
# Returns a [[page of photos], total] pair.
# Total is needed for pagination.
def flickr_photos(page_num=1, set=nil)
# FlickRaw returns a ResponseList when asked for a user's photos, but a
# Response when asked for photos in a set. Only ResponseList supports
# to_a(). Both support total(). Hence the following.
#
# We think this is a bug in FlickRaw, and have reported it here:
# https://github.com/hanklords/flickraw/issues/58
if set
return flickr.photosets.getPhotos(
result = flickr.photosets.getPhotos(
:photoset_id => set,
:page => page_num,
:per_page => 30
)
if result
return [result.photo, result.total]
else
return [[], 0]
end
else
return flickr.people.getPhotos(
result_list = flickr.people.getPhotos(
:user_id => 'me',
:page => page_num,
:per_page => 30
)
if result_list
return [result_list.to_a, result_list.total]
else
return [[], 0]
end
end
end

View File

@@ -23,12 +23,15 @@ describe PhotosController do
end
describe "GET new" do
it "assigns the flickr auth as @flickr_auth" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
@member.stub(:flickr_photos) { [] }
@member.stub(:flickr_photos) { [[], 0] }
@member.stub(:flickr_sets) { [] }
controller.stub(:current_member) { @member }
end
it "assigns the flickr auth as @flickr_auth" do
@auth = FactoryGirl.create(:flickr_authentication, :member => @member)
get :new, {}
assigns(:flickr_auth).should be_an_instance_of(Authentication)