2011-02-19 23:04:18 -05:00
|
|
|
module Kaminari
|
|
|
|
module PageScopeMethods
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
module InstanceMethods
|
|
|
|
# Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
|
|
|
|
# Model.page(3).per(10)
|
|
|
|
def per(num)
|
|
|
|
if (n = num.to_i) <= 0
|
|
|
|
self
|
|
|
|
else
|
|
|
|
limit(n).offset(offset_value / limit_value * n)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Total number of pages
|
|
|
|
def num_pages
|
2011-02-20 13:58:35 -05:00
|
|
|
(total_count.to_f / limit_value).ceil
|
2011-02-19 23:04:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Current page number
|
|
|
|
def current_page
|
|
|
|
(offset_value / limit_value) + 1
|
|
|
|
end
|
2011-03-05 13:32:15 -05:00
|
|
|
|
|
|
|
# First page of the collection ?
|
|
|
|
def first_page?
|
|
|
|
current_page == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# Last page of the collection?
|
|
|
|
def last_page?
|
2011-04-06 11:59:13 -04:00
|
|
|
current_page >= num_pages
|
2011-03-05 13:32:15 -05:00
|
|
|
end
|
2011-02-19 23:04:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|