module Pading::PageScopeMethods

Public Instance Methods

current_page() click to toggle source

获取当前page

# File lib/pading/activerecord/page_scope_menthods.rb, line 16
def current_page
  # offset_value  limit_value 元编程自动生成的查询
  # limit_value 每一页多少个
  # offset_value 从第几个开始查询
 (offset_value / limit_value) + 1
end
first_page?() click to toggle source
# File lib/pading/activerecord/page_scope_menthods.rb, line 53
def first_page?
  current_page == 1
end
last_page?() click to toggle source
# File lib/pading/activerecord/page_scope_menthods.rb, line 57
def last_page?
  current_page == total_pages
end
next_page() click to toggle source
# File lib/pading/activerecord/page_scope_menthods.rb, line 45
def next_page
  current_page + 1 unless last_page?
end
per(num, max_per_page: nil) click to toggle source
# File lib/pading/activerecord/page_scope_menthods.rb, line 4
def per(num, max_per_page: nil)
  if (n = num.to_i ) < 0 ||  !(/^\d/ =~ num.to_s)
    self
  elsif n.zero? # zero 是否为0
    limit(n)
  else
    limit(n).offset(offset_value / limit_value * n)
  end
end
prev_page() click to toggle source
# File lib/pading/activerecord/page_scope_menthods.rb, line 49
def prev_page
  current_page - 1 unless first_page?
end
total_count(column_name = :all, _options = nil) click to toggle source
# File lib/pading/activerecord/page_scope_menthods.rb, line 24
def total_count(column_name = :all, _options = nil)
  return @total_count if defined?(@total_count) && @total_count
  c = except(:offset, :limit, :order)
  # TODO references_eager_loaded_tables? API 没有这个方法的描述
  c = c.except(:includes) unless references_eager_loaded_tables?
  c = c.count(column_name)
  @total_count = if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash)
                   c.count
                 elsif c.respond_to? :count
                   c.count(column_name)
                 else
                   c
                 end

end
total_pages() click to toggle source
# File lib/pading/activerecord/page_scope_menthods.rb, line 40
def total_pages
  (total_count.to_f / limit_value).ceil
end