Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/NoICE/kaminari
Browse files Browse the repository at this point in the history
Conflicts:
	lib/kaminari/models/active_record_extension.rb
	lib/kaminari/railtie.rb
  • Loading branch information
amatsuda committed Aug 29, 2011
2 parents a62388f + c03cd8d commit 27160db
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.rdoc
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions kaminari.gemspec
Expand Up @@ -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']
Expand Down
23 changes: 23 additions & 0 deletions lib/kaminari/models/data_mapper_collection_methods.rb
@@ -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
43 changes: 43 additions & 0 deletions lib/kaminari/models/data_mapper_extension.rb
@@ -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
7 changes: 7 additions & 0 deletions lib/kaminari/railtie.rb
@@ -1,6 +1,8 @@
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'
Expand Down Expand Up @@ -28,6 +30,11 @@ class Railtie < ::Rails::Railtie #:nodoc:
::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
Expand Down
64 changes: 64 additions & 0 deletions spec/models/data_mapper_spec.rb
@@ -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
6 changes: 6 additions & 0 deletions spec/support/matchers.rb
Expand Up @@ -44,3 +44,9 @@ def instance_of(klass)
criteria.instance_variable_get('@options')[:skip] == num
end
end

RSpec::Matchers.define :offset do |num|
match do |collection|
collection.offset_value == num
end
end

0 comments on commit 27160db

Please sign in to comment.