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)
|
2013-06-18 09:30:05 -04:00
|
|
|
if (n = num.to_i) <= 0
|
2011-12-11 10:14:52 -05:00
|
|
|
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)
|
2013-02-21 16:51:58 -05:00
|
|
|
@_padding = num
|
2011-12-11 10:14:52 -05:00
|
|
|
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
|
2013-02-21 16:51:58 -05:00
|
|
|
count_without_padding = total_count
|
2013-04-23 09:20:21 -04:00
|
|
|
count_without_padding -= @_padding if defined?(@_padding) && @_padding
|
2013-02-21 16:51:58 -05:00
|
|
|
count_without_padding = 0 if count_without_padding < 0
|
|
|
|
|
|
|
|
total_pages_count = (count_without_padding.to_f / limit_value).ceil
|
2012-10-19 13:35:26 -04:00
|
|
|
if max_pages.present? && max_pages < total_pages_count
|
|
|
|
max_pages
|
|
|
|
else
|
|
|
|
total_pages_count
|
|
|
|
end
|
2011-12-11 10:14:52 -05:00
|
|
|
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
|
2013-02-21 16:51:58 -05:00
|
|
|
offset_without_padding = offset_value
|
2013-04-23 09:20:21 -04:00
|
|
|
offset_without_padding -= @_padding if defined?(@_padding) && @_padding
|
2013-02-21 16:51:58 -05:00
|
|
|
offset_without_padding = 0 if offset_without_padding < 0
|
|
|
|
|
|
|
|
(offset_without_padding / limit_value) + 1
|
2011-12-11 10:14:52 -05:00
|
|
|
end
|
2011-03-05 13:32:15 -05:00
|
|
|
|
2012-12-04 05:16:48 -05:00
|
|
|
# Next page number in the collection
|
|
|
|
def next_page
|
|
|
|
current_page + 1 unless last_page?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Previous page number in the collection
|
|
|
|
def prev_page
|
|
|
|
current_page - 1 unless first_page?
|
|
|
|
end
|
|
|
|
|
2013-06-18 09:10:43 -04:00
|
|
|
# First page of the collection?
|
2011-12-11 10:14:52 -05:00
|
|
|
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
|
2013-06-18 09:10:43 -04:00
|
|
|
|
|
|
|
# Out of range of the collection?
|
|
|
|
def out_of_range?
|
|
|
|
current_page > total_pages
|
|
|
|
end
|
2011-02-19 23:04:18 -05:00
|
|
|
end
|
|
|
|
end
|