mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
Refactor to extract common code from extensions.
Uses ActiveSupport::Concern to control the addition of methods. The Kaminari public API (scope DSL, class methods for config) was extracted to PageScopeMethods and ConfigurationMethods. Shims to abstract the difference between Mongoid and ActiveRecord were extracted to MongoidCriteriaMethods and ActiveRecordRelationMethods respectively. Also moved definition of common constants up to the railtie.
This commit is contained in:
parent
2659e0b59b
commit
0132b30a73
7 changed files with 84 additions and 76 deletions
|
@ -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
|
||||
|
@ -20,39 +19,8 @@ module Kaminari
|
|||
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
|
||||
|
|
10
lib/kaminari/active_record_relation_methods.rb
Normal file
10
lib/kaminari/active_record_relation_methods.rb
Normal file
|
@ -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
lib/kaminari/configuration_methods.rb
Normal file
20
lib/kaminari/configuration_methods.rb
Normal file
|
@ -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
lib/kaminari/mongoid_criteria_methods.rb
Normal file
16
lib/kaminari/mongoid_criteria_methods.rb
Normal file
|
@ -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
|
|
@ -1,17 +1,17 @@
|
|||
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
|
||||
|
@ -19,43 +19,8 @@ module Kaminari
|
|||
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
|
||||
|
|
26
lib/kaminari/page_scope_methods.rb
Normal file
26
lib/kaminari/page_scope_methods.rb
Normal file
|
@ -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
|
|
@ -5,8 +5,11 @@ begin; require 'active_record'; rescue LoadError; end
|
|||
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|
|
||||
if defined? ::ActiveRecord
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue