Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoidサポートを追加しました #9

Merged
5 commits merged into from Feb 17, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -20,4 +20,6 @@ group :development do
gem 'rspec-rails', '>= 2.5.0'
gem 'sqlite3', '>= 1.3.3'
gem 'ruby-debug'
gem 'mongoid', '2.0.0.rc.7'
gem 'bson_ext', '~> 1.2'
end
12 changes: 12 additions & 0 deletions Gemfile.lock
Expand Up @@ -61,6 +61,8 @@ GEM
specs:
abstract (1.0.0)
bcrypt-ruby (2.1.4)
bson (1.2.2)
bson_ext (1.2.2)
builder (3.0.0)
capybara (0.4.1.2)
celerity (>= 0.7.9)
Expand Down Expand Up @@ -96,6 +98,13 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mongo (1.2.2)
bson (>= 1.2.2)
mongoid (2.0.0.rc.7)
activemodel (~> 3.0)
mongo (~> 1.2)
tzinfo (~> 0.3.22)
will_paginate (~> 3.0.pre)
nokogiri (1.4.4)
polyglot (0.3.1)
rack-cache (1.0)
Expand Down Expand Up @@ -138,6 +147,7 @@ GEM
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.24)
will_paginate (3.0.pre2)
xpath (0.1.3)
nokogiri (~> 1.3)

Expand All @@ -146,9 +156,11 @@ PLATFORMS

DEPENDENCIES
arel!
bson_ext (~> 1.2)
bundler (>= 1.0.0)
capybara (>= 0.4.1.1)
jeweler (>= 1.5.2)
mongoid (= 2.0.0.rc.7)
rack!
rails!
rcov
Expand Down
5 changes: 5 additions & 0 deletions lib/kaminari/mongoid.rb
@@ -0,0 +1,5 @@
module Kaminari
module Mongoid
DEFAULT_PER_PAGE = 25
end
end
39 changes: 39 additions & 0 deletions lib/kaminari/mongoid/criteria.rb
@@ -0,0 +1,39 @@
module Kaminari
module Mongoid
module Criteria
extend ActiveSupport::Concern

included do
# Fetch the values at the specified page number
# Model.order_by(:name.asc).page(5)
def page(num)
limit(@klass.default_per_page).offset(@klass.default_per_page * ([num.to_i, 1].max - 1))
end

# Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
# Model.order_by(:name.asc).page(3).per(10)
def per(num)
if (n = num.to_i) <= 0
self
else
limit(n).offset(options[:skip] / options[:limit] * n)
end
end

# Total number of pages
def num_pages
(count.to_f / options[:limit]).ceil
end

# Current page number
def current_page
(options[:skip] / options[:limit]) + 1
end

def limit_value
options[:limit]
end
end
end
end
end
53 changes: 53 additions & 0 deletions lib/kaminari/mongoid/document.rb
@@ -0,0 +1,53 @@
module Kaminari
module Mongoid
module Document
extend ActiveSupport::Concern

included do
# Fetch the values at the specified page number
# Model.page(5)
scope :page, lambda {|num|
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
} do
# Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
# Model.page(3).per(10)
def per(num)
if (n = num.to_i) <= 0
self
else
limit(n).offset(options[:skip] / options[:limit] * n)
end
end

# Total number of pages
def num_pages
(count.to_f / options[:limit]).ceil
end

# Current page number
def current_page
(options[:skip] / options[:limit]) + 1
end

def limit_value
options[:limit]
end
end

# Overrides the default per_page value per model
# class Article < ActiveRecord::Base
# paginates_per 10
# end
def self.paginates_per(val)
@_default_per_page = val
end

# This model's default per_page value
# returns 25 unless explicitly overridden via <tt>paginates_per</tt>
def self.default_per_page
@_default_per_page || Kaminari::Mongoid::DEFAULT_PER_PAGE
end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/kaminari/railtie.rb
@@ -1,13 +1,18 @@
require 'rails'
require 'action_view'
require File.join(File.dirname(__FILE__), 'active_record') if defined? ::ActiveRecord
require File.join(File.dirname(__FILE__), 'mongoid') if defined? ::Mongoid
require File.join(File.dirname(__FILE__), 'mongoid/criteria') if defined? ::Mongoid::Criteria
require File.join(File.dirname(__FILE__), 'mongoid/document') if defined? ::Mongoid::Document
require File.join(File.dirname(__FILE__), 'helpers')

module Kaminari
class Railtie < ::Rails::Railtie #:nodoc:
initializer 'paginatablize' do |app|
::ActiveRecord::Base.send :include, Kaminari::ActiveRecord if defined? ::ActiveRecord::Base
::ActionView::Base.send :include, Kaminari::Helpers
::Mongoid::Criteria.send :include, Kaminari::Mongoid::Criteria if defined? ::Mongoid::Criteria
::Mongoid::Document.send :include, Kaminari::Mongoid::Document if defined? ::Mongoid::Document
end
end
end