Created
May 13, 2014 15:55
-
-
Save hbin/dedb1a14f50b42e436f4 to your computer and use it in GitHub Desktop.
This would be helpful when handling an Array object for the kaminari gem.
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
if defined?(Kaminari) | |
class Array | |
# This would be helpful when handling an Array object that | |
# has a different count value from actual count. | |
# | |
# Example: | |
# arr = ['a', 'b', 'c', 'd', 'e'] | |
# | |
# paged = arr.paginate(total_count: 200).page(2) #-> ['a', 'b', 'c', 'd', 'e'] | |
# paged.total_count #-> 200 | |
# paged.current_page #-> 2 | |
# | |
# paged = arr.paginate.page(2).per(2) #-> ['c', 'd'] | |
# paged.total_count #-> 5 | |
# paged.current_page #-> 2 | |
# | |
def paginate(options = {}) | |
page = options[:page] || 1 | |
per_page = options[:per_page] || Kaminari.config.default_per_page | |
total = options[:total_count] || self.length | |
if options[:total_count] | |
Kaminari.paginate_array(self, total_count: total).page(page).per(per_page) | |
else | |
Kaminari.paginate_array(self).page(page).per(per_page) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment