mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
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
2 changed files with 26 additions and 7 deletions
|
@ -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
|
||||
|
|
|
@ -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' }
|
||||
|
|
Loading…
Add table
Reference in a new issue