mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
implement AR scopes
This commit is contained in:
parent
b59b1e3132
commit
bfa1449f16
4 changed files with 124 additions and 0 deletions
|
@ -0,0 +1 @@
|
|||
require File.join(File.dirname(__FILE__), 'kaminari/railtie')
|
30
lib/kaminari/active_record.rb
Normal file
30
lib/kaminari/active_record.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Kaminari
|
||||
module ActiveRecord
|
||||
extend ActiveSupport::Concern
|
||||
PER_PAGE = 10
|
||||
|
||||
included do
|
||||
def self.inherited(kls)
|
||||
kls.class_eval do
|
||||
# page(5)
|
||||
scope :page, lambda {|num|
|
||||
offset(PER_PAGE * ([num.to_i, 1].max - 1)).limit(PER_PAGE)
|
||||
} do
|
||||
# page(3).per(20)
|
||||
def per(num)
|
||||
offset(offset_value / limit_value * num).limit(num)
|
||||
end
|
||||
|
||||
def num_pages
|
||||
(except(:offset, :limit).count.to_f / limit_value).ceil
|
||||
end
|
||||
|
||||
def current_page
|
||||
(offset_value / limit_value) + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
11
lib/kaminari/railtie.rb
Normal file
11
lib/kaminari/railtie.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'rails'
|
||||
require 'active_record'
|
||||
require File.join(File.dirname(__FILE__), 'active_record')
|
||||
|
||||
module Kaminari
|
||||
class Railtie < ::Rails::Railtie
|
||||
initializer 'paginatablize' do |app|
|
||||
::ActiveRecord::Base.send :include, Kaminari::ActiveRecord
|
||||
end
|
||||
end
|
||||
end
|
82
spec/models/scopes_spec.rb
Normal file
82
spec/models/scopes_spec.rb
Normal file
|
@ -0,0 +1,82 @@
|
|||
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
||||
|
||||
describe Kaminari::ActiveRecord do
|
||||
before :all do
|
||||
User.delete_all
|
||||
1.upto(20) {|i| User.create! :name => "user#{'%02d' % i}" }
|
||||
end
|
||||
|
||||
describe '#page' do
|
||||
shared_examples_for 'the first page' do
|
||||
it { should have(10).users }
|
||||
its('first.name') { should == 'user01' }
|
||||
end
|
||||
|
||||
shared_examples_for 'blank page' do
|
||||
it { should have(0).users }
|
||||
end
|
||||
|
||||
context 'page 1' do
|
||||
subject { User.page 1 }
|
||||
it_should_behave_like 'the first page'
|
||||
end
|
||||
|
||||
context 'page 2' do
|
||||
subject { User.page 2 }
|
||||
it { should have(10).users }
|
||||
its('first.name') { should == 'user11' }
|
||||
end
|
||||
|
||||
context 'page without an argument' do
|
||||
subject { User.page }
|
||||
it_should_behave_like 'the first page'
|
||||
end
|
||||
|
||||
context 'page < 1' do
|
||||
subject { User.page 0 }
|
||||
it_should_behave_like 'the first page'
|
||||
end
|
||||
|
||||
context 'page > max page' do
|
||||
subject { User.page 3 }
|
||||
it_should_behave_like 'blank page'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#per' do
|
||||
context 'page 1 per 5' do
|
||||
subject { User.page(1).per(5) }
|
||||
it { should have(5).users }
|
||||
its('first.name') { should == 'user01' }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#num_pages' do
|
||||
context 'per 10' do
|
||||
subject { User.page.num_pages }
|
||||
it { should == 2 }
|
||||
end
|
||||
|
||||
context 'per 7' do
|
||||
subject { User.page(2).per(7).num_pages }
|
||||
it { should == 3 }
|
||||
end
|
||||
|
||||
context 'per 65536' do
|
||||
subject { User.page(50).per(65536).num_pages }
|
||||
it { should == 1 }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#current_page' do
|
||||
context 'page 1' do
|
||||
subject { User.page.current_page }
|
||||
it { should == 1 }
|
||||
end
|
||||
|
||||
context 'page 2' do
|
||||
subject { User.page(2).per(3).current_page }
|
||||
it { should == 2 }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue