1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Added support for DataMapper v1.1.0. Including tests.

This commit is contained in:
noice 2011-08-14 04:25:59 +02:00
parent beeb13bbde
commit 7f71f2c07b
7 changed files with 146 additions and 0 deletions

View file

@ -37,6 +37,8 @@ The pagination helper outputs the HTML5 <nav> tag by default. Plus, the helper s
* MongoMapper 0.9
* DataMapper 1.1.0
== Install
Put this line in your Gemfile:

View file

@ -26,6 +26,8 @@ Gem::Specification.new do |s|
s.add_development_dependency 'sqlite3', ['>= 0']
s.add_development_dependency 'mongoid', ['>= 2']
s.add_development_dependency 'mongo_mapper', ['>= 0.9']
s.add_development_dependency 'dm-core', ['>= 1.1.0']
s.add_development_dependency 'dm-sqlite-adapter', ['>= 1.1.0']
s.add_development_dependency 'rspec', ['>= 0']
s.add_development_dependency 'rspec-rails', ['>= 0']
s.add_development_dependency 'rr', ['>= 0']

View file

@ -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

View file

@ -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

View file

@ -2,6 +2,7 @@ require 'rails'
# ensure ORMs are loaded *before* initializing Kaminari
begin; require 'mongoid'; rescue LoadError; end
begin; require 'mongo_mapper'; rescue LoadError; end
begin; require 'dm-core'; rescue LoadError; end
require 'kaminari/config'
require 'kaminari/helpers/action_view_extension'
@ -27,6 +28,11 @@ module Kaminari
::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
::Plucky::Query.send :include, Kaminari::PageScopeMethods
end
if defined? ::DataMapper
require 'kaminari/models/data_mapper_extension'
::DataMapper::Model.send :include, Kaminari::DataMapperExtension::Model
::DataMapper::Collection.send :include, Kaminari::DataMapperExtension::Collection
end
require 'kaminari/models/array_extension'
ActiveSupport.on_load(:action_view) do
::ActionView::Base.send :include, Kaminari::ActionViewExtension

View file

@ -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

View file

@ -44,3 +44,9 @@ RSpec::Matchers.define :skip do |num|
criteria.instance_variable_get('@options')[:skip] == num
end
end
RSpec::Matchers.define :offset do |num|
match do |collection|
collection.offset_value == num
end
end