2011-02-19 23:04:18 -05:00
|
|
|
module Kaminari
|
|
|
|
module PageScopeMethods
|
2011-12-11 10:14:52 -05:00
|
|
|
# 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
|
2012-08-28 03:54:45 -04:00
|
|
|
elsif max_per_page && max_per_page < n
|
2012-08-28 01:15:19 -04:00
|
|
|
limit(max_per_page).offset(offset_value / limit_value * max_per_page)
|
2011-12-11 10:14:52 -05:00
|
|
|
else
|
|
|
|
limit(n).offset(offset_value / limit_value * n)
|
2011-02-19 23:04:18 -05:00
|
|
|
end
|
2011-12-11 10:14:52 -05:00
|
|
|
end
|
2011-02-19 23:04:18 -05:00
|
|
|
|
2011-12-11 10:14:52 -05:00
|
|
|
def padding(num)
|
|
|
|
offset(offset_value + num.to_i)
|
|
|
|
end
|
2011-03-14 13:14:14 -04:00
|
|
|
|
2011-12-11 10:14:52 -05:00
|
|
|
# Total number of pages
|
2012-05-25 02:31:53 -04:00
|
|
|
def total_pages
|
2011-12-11 10:14:52 -05:00
|
|
|
(total_count.to_f / limit_value).ceil
|
|
|
|
end
|
2012-05-25 02:32:30 -04:00
|
|
|
#FIXME for compatibility. remove num_pages at some time in the future
|
|
|
|
alias num_pages total_pages
|
2011-02-19 23:04:18 -05:00
|
|
|
|
2011-12-11 10:14:52 -05:00
|
|
|
# Current page number
|
|
|
|
def current_page
|
|
|
|
(offset_value / limit_value) + 1
|
|
|
|
end
|
2011-03-05 13:32:15 -05:00
|
|
|
|
2011-12-11 10:14:52 -05:00
|
|
|
# First page of the collection ?
|
|
|
|
def first_page?
|
|
|
|
current_page == 1
|
|
|
|
end
|
2011-03-05 13:32:15 -05:00
|
|
|
|
2011-12-11 10:14:52 -05:00
|
|
|
# Last page of the collection?
|
|
|
|
def last_page?
|
2012-05-25 02:31:53 -04:00
|
|
|
current_page >= total_pages
|
2011-02-19 23:04:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|