Created
November 23, 2008 21:23
-
-
Save mislav/28211 to your computer and use it in GitHub Desktop.
add pagination to YouTube Model
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
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