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 e3b8db5876 Fixes Ruby 1.9.2 compatibility of page scope.
In Ruby 1.9.2, syntax for optional block parameters
is available, but behaviour for missing block parameters
also changed. When no arguments are provided to Model.page,
"ArgumentError: wrong number of arguments (1 for 0)" is raised.

This fix is an ugly workaround that is compatible with both
1.8 and 1.9.
2011-02-19 20:57:32 +09:00

63 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, lambda {|*num|
raise ArgumentError if num.size > 1
num = num.first || 1
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