Define page method when a AR class is defined

Commit 92052eedf0 looked good at the beginning. However, that causes a bug that kaminari's page_method_name config has no effect since the pagination method will be defined when `lib/kaminari/models/active_record_model_extension.rb` is loaded.

Define that 'page' method when an AR class is actually defined so we will be able to change the name of the method properly. Fixes #481
This commit is contained in:
Yuki Nishijima 2013-12-02 05:21:49 +09:00
parent 4ebf14081c
commit 8c0b9d5d1a
2 changed files with 26 additions and 7 deletions

View File

@ -6,17 +6,17 @@ module Kaminari
included do
self.send(:include, Kaminari::ConfigurationMethods)
end
module ClassMethods
# Fetch the values at the specified page number
# Model.page(5)
define_method(Kaminari.config.page_method_name) do |num = nil|
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1)).extending do
include Kaminari::ActiveRecordRelationMethods
include Kaminari::PageScopeMethods
eval <<-RUBY
def self.#{Kaminari.config.page_method_name}(num = nil)
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1)).extending do
include Kaminari::ActiveRecordRelationMethods
include Kaminari::PageScopeMethods
end
end
end
RUBY
end
end
end

View File

@ -2,6 +2,25 @@ require 'spec_helper'
if defined? ActiveRecord
describe Kaminari::ActiveRecordModelExtension do
before do
Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari
end
class Comment < ActiveRecord::Base; end
end
subject { Comment }
it { should respond_to(:per_page_kaminari) }
it { should_not respond_to(:page) }
after do
Kaminari.configure do |config|
config.page_method_name = :page
end
end
end
shared_examples_for 'the first page' do
it { should have(25).users }
its('first.name') { should == 'user001' }