forked from kaminari/kaminari
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/NoICE/kaminari
Conflicts: lib/kaminari/models/active_record_extension.rb lib/kaminari/railtie.rb
- Loading branch information
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Kaminari | ||
module DataMapperCollectionMethods | ||
extend ActiveSupport::Concern | ||
module InstanceMethods | ||
def limit_value #:nodoc: | ||
query.options[:limit] || 0 | ||
end | ||
|
||
def offset_value #:nodoc: | ||
query.options[:offset] || 0 | ||
end | ||
|
||
def total_count #:nodoc: | ||
return count if query.options.blank? | ||
opts = query.options.dup | ||
opts.delete(:limit) | ||
opts.delete(:offset) | ||
opts.delete(:order) | ||
model.all(opts).count | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'kaminari/models/data_mapper_collection_methods' | ||
|
||
module Kaminari | ||
module DataMapperExtension | ||
module Collection | ||
extend ActiveSupport::Concern | ||
included do | ||
include Kaminari::ConfigurationMethods::ClassMethods | ||
include Kaminari::DataMapperCollectionMethods | ||
include Kaminari::PageScopeMethods | ||
|
||
# Fetch the values at the specified page number | ||
# Model.all(:some => :conditions).page(5) | ||
def page(num) | ||
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1)) | ||
end | ||
end | ||
end | ||
|
||
module Model | ||
extend ActiveSupport::Concern | ||
included do | ||
# Fetch the values at the specified page number | ||
# Model.page(5) | ||
def page(*args) | ||
all.page(*args) | ||
end | ||
|
||
def per(*args) | ||
all.per(*args) | ||
end | ||
|
||
def limit(val) | ||
all(:limit => val) | ||
end | ||
|
||
def offset(val) | ||
all(:offset => val) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require File.expand_path('../spec_helper', File.dirname(__FILE__)) | ||
require 'dm-core' | ||
require 'kaminari/models/data_mapper_extension' | ||
|
||
describe Kaminari::DataMapperExtension do | ||
before :all do | ||
DataMapper.setup(:default, 'sqlite::memory:') | ||
class Developer | ||
include ::DataMapper::Resource | ||
property :id, Serial | ||
property :salary, Integer | ||
end | ||
end | ||
before do | ||
stub(subject).count { 300 } # in order to avoid DB access... | ||
end | ||
|
||
describe '#page' do | ||
context 'page 1' do | ||
subject { Developer.page(1) } | ||
it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 1 } | ||
its(:limit_value) { should == 25 } | ||
its(:num_pages) { should == 12 } | ||
it { should offset(0) } | ||
end | ||
|
||
context 'page 2' do | ||
subject { Developer.page 2 } | ||
it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 2 } | ||
its(:limit_value) { should == 25 } | ||
its(:num_pages) { should == 12 } | ||
it { should offset 25 } | ||
end | ||
|
||
context 'page "foobar"' do | ||
subject { Developer.page 'foobar' } | ||
it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 1 } | ||
its(:limit_value) { should == 25 } | ||
its(:num_pages) { should == 12 } | ||
it { should offset 0 } | ||
end | ||
|
||
context 'page 1 with another conditions' do | ||
subject { Developer.page(2) } | ||
it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 2 } | ||
its(:limit_value) { should == 25 } | ||
its(:num_pages) { should == 12 } | ||
it { should offset(25) } | ||
end | ||
end | ||
|
||
describe '#per' do | ||
subject { Developer.page(2).per(10) } | ||
it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 2 } | ||
its(:limit_value) { should == 10 } | ||
its(:num_pages) { should == 30 } | ||
it { should offset 10 } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters