1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00
kaminari--kaminari/lib/kaminari/models/page_scope_methods.rb

42 lines
991 B
Ruby
Raw Normal View History

module Kaminari
module PageScopeMethods
# 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-27 22:15:19 -07:00
elsif max_per_page != 0 && max_per_page < n
limit(max_per_page).offset(offset_value / limit_value * max_per_page)
else
limit(n).offset(offset_value / limit_value * n)
end
end
def padding(num)
offset(offset_value + num.to_i)
end
# Total number of pages
2012-05-25 15:31:53 +09:00
def total_pages
(total_count.to_f / limit_value).ceil
end
#FIXME for compatibility. remove num_pages at some time in the future
alias num_pages total_pages
# Current page number
def current_page
(offset_value / limit_value) + 1
end
2011-03-05 19:32:15 +01:00
# First page of the collection ?
def first_page?
current_page == 1
end
2011-03-05 19:32:15 +01:00
# Last page of the collection?
def last_page?
2012-05-25 15:31:53 +09:00
current_page >= total_pages
end
end
end