From 65a1626f730d21f8fd32551196ed1ccb2a527a4f Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 18 Feb 2011 08:07:23 +0900 Subject: [PATCH] merge mongoid/document and mongoid/criteria into one file --- lib/kaminari/mongoid.rb | 58 ++++++++++++++++++++++++++++++++ lib/kaminari/mongoid/criteria.rb | 11 ------ lib/kaminari/mongoid/document.rb | 53 ----------------------------- lib/kaminari/railtie.rb | 4 +-- 4 files changed, 59 insertions(+), 67 deletions(-) delete mode 100644 lib/kaminari/mongoid/criteria.rb delete mode 100644 lib/kaminari/mongoid/document.rb diff --git a/lib/kaminari/mongoid.rb b/lib/kaminari/mongoid.rb index 48af975..dd5b677 100644 --- a/lib/kaminari/mongoid.rb +++ b/lib/kaminari/mongoid.rb @@ -1,5 +1,63 @@ module Kaminari module Mongoid DEFAULT_PER_PAGE = 25 + + module Criteria + extend ActiveSupport::Concern + + included do + delegate :page, :per, :num_pages, :current_page, :limit_value, :to => '@klass' + end + end + + 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 per_page value for the preceding page 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 paginates_per + def self.default_per_page + @_default_per_page || Kaminari::Mongoid::DEFAULT_PER_PAGE + end + end + end end end diff --git a/lib/kaminari/mongoid/criteria.rb b/lib/kaminari/mongoid/criteria.rb deleted file mode 100644 index cd3e952..0000000 --- a/lib/kaminari/mongoid/criteria.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Kaminari - module Mongoid - module Criteria - extend ActiveSupport::Concern - - included do - delegate :page, :per, :num_pages, :current_page, :limit_value, :to => '@klass' - end - end - end -end diff --git a/lib/kaminari/mongoid/document.rb b/lib/kaminari/mongoid/document.rb deleted file mode 100644 index 622585f..0000000 --- a/lib/kaminari/mongoid/document.rb +++ /dev/null @@ -1,53 +0,0 @@ -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 per_page value for the preceding page 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 paginates_per - def self.default_per_page - @_default_per_page || Kaminari::Mongoid::DEFAULT_PER_PAGE - end - end - end - end -end diff --git a/lib/kaminari/railtie.rb b/lib/kaminari/railtie.rb index a8f9cc5..16b3ecc 100644 --- a/lib/kaminari/railtie.rb +++ b/lib/kaminari/railtie.rb @@ -11,10 +11,8 @@ module Kaminari end if defined? ::Mongoid require File.join(File.dirname(__FILE__), 'mongoid') - require File.join(File.dirname(__FILE__), 'mongoid/criteria') - require File.join(File.dirname(__FILE__), 'mongoid/document') - ::Mongoid::Criteria.send :include, Kaminari::Mongoid::Criteria ::Mongoid::Document.send :include, Kaminari::Mongoid::Document + ::Mongoid::Criteria.send :include, Kaminari::Mongoid::Criteria end ::ActionView::Base.send :include, Kaminari::Helpers end