Skip to content

Instantly share code, notes, and snippets.

@mislav
Created November 23, 2008 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mislav/28211 to your computer and use it in GitHub Desktop.
Save mislav/28211 to your computer and use it in GitHub Desktop.
add pagination to YouTube Model
YouTubeModel::SingletonMethods.class_eval do
include WillPaginate::Finders::Base
protected
def wp_query(options, pager, args)
# dup options and set limit
args << options.merge(:max_results => pager.per_page)
# carefully add start_index (YouTube goes nuts if it's zero)
args.last[:start_index] = pager.offset if pager.offset > 0
# perform the search
youtube = send(:find, *args)
# populate the collection with videos
pager.replace youtube.videos
# keep track of total results
unless pager.total_entries
pager.total_entries = youtube.totalResults.to_i
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment