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/active_record_extension.rb
Leonard Chin 6e614149d1 Use Proc.new instead of lambda for scope_options.
lambda is strict about block parameters in 1.9.2,
so we might as well just use Proc.new.
2011-02-20 06:49:55 +09:00

61 lines
2 KiB
Ruby

module Kaminari
DEFAULT_PER_PAGE = 25 unless defined? ::Kaminari::DEFAULT_PER_PAGE
module ActiveRecordExtension
extend ActiveSupport::Concern
included do
def self.inherited(kls) #:nodoc:
# TERRIBLE HORRIBLE NO GOOD VERY BAD HACK: inheritable_attributes is not yet set here on AR 3.0
unless kls.default_scoping
new_inheritable_attributes = Hash[inheritable_attributes.map do |key, value|
[key, value.duplicable? ? value.dup : value]
end]
kls.instance_variable_set('@inheritable_attributes', new_inheritable_attributes)
end
kls.class_eval do
# Fetch the values at the specified page number
# Model.page(5)
scope :page, Proc.new {|num|
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
} do
# 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
(except(:offset, :limit).count.to_f / limit_value).ceil
end
# Current page number
def current_page
(offset_value / limit_value) + 1
end
end
# Overrides the default per_page value per model
# class Article < ActiveRecord::Base
# paginates_per 10
# end
def self.paginates_per(val)
@_default_per_page = val
end
# This model's default per_page value
# returns 25 unless explicitly overridden via <tt>paginates_per</tt>
def self.default_per_page
@_default_per_page || Kaminari::DEFAULT_PER_PAGE
end
end
end
end
end
end