Skip to content

Commit

Permalink
Merge branch 'concern_refactor' of https://github.com/l15n/kaminari i…
Browse files Browse the repository at this point in the history
…nto l15n-concern_refactor
  • Loading branch information
amatsuda committed Feb 20, 2011
2 parents 4809198 + 0132b30 commit 99b6627
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 76 deletions.
40 changes: 4 additions & 36 deletions lib/kaminari/active_record_extension.rb
@@ -1,9 +1,8 @@
require File.join(File.dirname(__FILE__), 'active_record_relation_methods')
module Kaminari
DEFAULT_PER_PAGE = 25 unless defined? ::Kaminari::DEFAULT_PER_PAGE

module ActiveRecordExtension
extend ActiveSupport::Concern

include Kaminari::ConfigurationMethods
included do
def self.inherited(kls) #:nodoc:
# TERRIBLE HORRIBLE NO GOOD VERY BAD HACK: inheritable_attributes is not yet set here on AR 3.0
Expand All @@ -20,39 +19,8 @@ def self.inherited(kls) #:nodoc:
scope :page, Proc.new {|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(offset_value / limit_value * n)
end
end

# Total number of pages
def num_pages
(except(:offset, :limit).count.to_f / limit_value).ceil
end

# Current page number
def current_page
(offset_value / limit_value) + 1
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::DEFAULT_PER_PAGE
include Kaminari::ActiveRecordRelationMethods
include Kaminari::PageScopeMethods
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/kaminari/active_record_relation_methods.rb
@@ -0,0 +1,10 @@
module Kaminari
module ActiveRecordRelationMethods
extend ActiveSupport::Concern
module InstanceMethods
def pagination_count
except(:offset, :limit).count
end
end
end
end
20 changes: 20 additions & 0 deletions lib/kaminari/configuration_methods.rb
@@ -0,0 +1,20 @@
module Kaminari
module ConfigurationMethods
extend ActiveSupport::Concern
module ClassMethods
# Overrides the default per_page value per model
# class Article < ActiveRecord::Base
# paginates_per 10
# end
def 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 default_per_page
@_default_per_page || Kaminari::DEFAULT_PER_PAGE
end
end
end
end
16 changes: 16 additions & 0 deletions lib/kaminari/mongoid_criteria_methods.rb
@@ -0,0 +1,16 @@
module Kaminari
module MongoidCriteriaMethods
extend ActiveSupport::Concern
module InstanceMethods
def limit_value
options[:limit]
end
def offset_value
options[:skip]
end
def pagination_count
count
end
end
end
end
45 changes: 5 additions & 40 deletions lib/kaminari/mongoid_extension.rb
@@ -1,61 +1,26 @@
require File.join(File.dirname(__FILE__), 'mongoid_criteria_methods')
module Kaminari
DEFAULT_PER_PAGE = 25 unless defined? ::Kaminari::DEFAULT_PER_PAGE

module MongoidExtension
module Criteria
extend ActiveSupport::Concern

included do
delegate :page, :per, :num_pages, :current_page, :limit_value, :to => '@klass'
delegate :page, :per, :num_pages, :current_page, :limit_value, :offset_value, :pagination_count, :to => '@klass'
end
end

module Document
extend ActiveSupport::Concern
include Kaminari::ConfigurationMethods

included do
# Fetch the values at the specified page number
# Model.page(5)
scope :page, Proc.new {|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::DEFAULT_PER_PAGE
include Kaminari::MongoidCriteriaMethods
include Kaminari::PageScopeMethods
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions lib/kaminari/page_scope_methods.rb
@@ -0,0 +1,26 @@
module Kaminari
module PageScopeMethods
extend ActiveSupport::Concern
module InstanceMethods
# 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(offset_value / limit_value * n)
end
end

# Total number of pages
def num_pages
(pagination_count.to_f / limit_value).ceil
end

# Current page number
def current_page
(offset_value / limit_value) + 1
end
end
end
end
3 changes: 3 additions & 0 deletions lib/kaminari/railtie.rb
Expand Up @@ -3,8 +3,11 @@
begin; require 'mongoid'; rescue LoadError; end

require File.join(File.dirname(__FILE__), 'helpers')
require File.join(File.dirname(__FILE__), 'page_scope_methods')
require File.join(File.dirname(__FILE__), 'configuration_methods')

module Kaminari
DEFAULT_PER_PAGE = 25 unless defined? ::Kaminari::DEFAULT_PER_PAGE
class Railtie < ::Rails::Railtie #:nodoc:
initializer 'kaminari' do |app|
ActiveSupport.on_load(:active_record) do
Expand Down

0 comments on commit 99b6627

Please sign in to comment.