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:
parent
4ebf14081c
commit
8c0b9d5d1a
|
@ -6,17 +6,17 @@ module Kaminari
|
||||||
|
|
||||||
included do
|
included do
|
||||||
self.send(:include, Kaminari::ConfigurationMethods)
|
self.send(:include, Kaminari::ConfigurationMethods)
|
||||||
end
|
|
||||||
|
|
||||||
module ClassMethods
|
|
||||||
# Fetch the values at the specified page number
|
# Fetch the values at the specified page number
|
||||||
# Model.page(5)
|
# Model.page(5)
|
||||||
define_method(Kaminari.config.page_method_name) do |num = nil|
|
eval <<-RUBY
|
||||||
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1)).extending do
|
def self.#{Kaminari.config.page_method_name}(num = nil)
|
||||||
include Kaminari::ActiveRecordRelationMethods
|
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1)).extending do
|
||||||
include Kaminari::PageScopeMethods
|
include Kaminari::ActiveRecordRelationMethods
|
||||||
|
include Kaminari::PageScopeMethods
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
RUBY
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,25 @@ require 'spec_helper'
|
||||||
|
|
||||||
if defined? ActiveRecord
|
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
|
shared_examples_for 'the first page' do
|
||||||
it { should have(25).users }
|
it { should have(25).users }
|
||||||
its('first.name') { should == 'user001' }
|
its('first.name') { should == 'user001' }
|
||||||
|
|
Loading…
Reference in New Issue