1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

fixes #180 avoid the unnecessary "yo dawg, I heard you like include, so I put a module that includes your module when it is included" approach when building extensions

using InstanceMethods is deprecated since 401393b656
This commit is contained in:
Akira Matsuda 2011-12-12 00:14:52 +09:00
parent 0ffe09de68
commit 27a4e6076b
6 changed files with 113 additions and 131 deletions

View file

@ -1,49 +1,46 @@
module Kaminari
# = Helpers
module ActionViewExtension
extend ::ActiveSupport::Concern
module InstanceMethods
# A helper that renders the pagination links.
#
# <%= paginate @articles %>
#
# ==== Options
# * <tt>:window</tt> - The "inner window" size (4 by default).
# * <tt>:outer_window</tt> - The "outer window" size (0 by default).
# * <tt>:left</tt> - The "left outer window" size (0 by default).
# * <tt>:right</tt> - The "right outer window" size (0 by default).
# * <tt>:params</tt> - url_for parameters for the links (:controller, :action, etc.)
# * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
# * <tt>:remote</tt> - Ajax? (false by default)
# * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
def paginate(scope, options = {}, &block)
paginator = Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :num_pages => scope.num_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
paginator.to_s
end
# A helper that renders the pagination links.
#
# <%= paginate @articles %>
#
# ==== Options
# * <tt>:window</tt> - The "inner window" size (4 by default).
# * <tt>:outer_window</tt> - The "outer window" size (0 by default).
# * <tt>:left</tt> - The "left outer window" size (0 by default).
# * <tt>:right</tt> - The "right outer window" size (0 by default).
# * <tt>:params</tt> - url_for parameters for the links (:controller, :action, etc.)
# * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
# * <tt>:remote</tt> - Ajax? (false by default)
# * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
def paginate(scope, options = {}, &block)
paginator = Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :num_pages => scope.num_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
paginator.to_s
end
# A simple "Twitter like" pagination link that creates a link to the next page.
#
# ==== Examples
# Basic usage:
#
# <%= link_to_next_page @items, 'Next Page' %>
#
# Ajax:
#
# <%= link_to_next_page @items, 'Next Page', :remote => true %>
#
# By default, it renders nothing if there are no more results on the next page.
# You can customize this output by passing a block.
#
# <%= link_to_next_page @users, 'Next Page' do %>
# <span>No More Pages</span>
# <% end %>
def link_to_next_page(scope, name, options = {}, &block)
params = options.delete(:params) || {}
param_name = options.delete(:param_name) || Kaminari.config.param_name
link_to_unless scope.last_page?, name, params.merge(param_name => (scope.current_page + 1)), options.reverse_merge(:rel => 'next') do
block.call if block
end
# A simple "Twitter like" pagination link that creates a link to the next page.
#
# ==== Examples
# Basic usage:
#
# <%= link_to_next_page @items, 'Next Page' %>
#
# Ajax:
#
# <%= link_to_next_page @items, 'Next Page', :remote => true %>
#
# By default, it renders nothing if there are no more results on the next page.
# You can customize this output by passing a block.
#
# <%= link_to_next_page @users, 'Next Page' do %>
# <span>No More Pages</span>
# <% end %>
def link_to_next_page(scope, name, options = {}, &block)
params = options.delete(:params) || {}
param_name = options.delete(:param_name) || Kaminari.config.param_name
link_to_unless scope.last_page?, name, params.merge(param_name => (scope.current_page + 1)), options.reverse_merge(:rel => 'next') do
block.call if block
end
end
end

View file

@ -1,27 +1,24 @@
module Kaminari
module ActiveRecordRelationMethods
extend ActiveSupport::Concern
module InstanceMethods
# a workaround for AR 3.0.x that returns 0 for #count when page > 1
# if +limit_value+ is specified, load all the records and count them
if ActiveRecord::VERSION::STRING < '3.1'
def count #:nodoc:
limit_value ? length : super
end
# a workaround for AR 3.0.x that returns 0 for #count when page > 1
# if +limit_value+ is specified, load all the records and count them
if ActiveRecord::VERSION::STRING < '3.1'
def count #:nodoc:
limit_value ? length : super
end
end
def total_count #:nodoc:
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
@total_count ||= begin
c = except(:offset, :limit, :order)
# a workaround for 3.1.beta1 bug. see: https://github.com/rails/rails/issues/406
c = c.reorder nil
# Remove includes only if they are irrelevant
c = c.except(:includes) unless references_eager_loaded_tables?
# .group returns an OrderdHash that responds to #count
c = c.count
c.respond_to?(:count) ? c.count : c
end
def total_count #:nodoc:
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
@total_count ||= begin
c = except(:offset, :limit, :order)
# a workaround for 3.1.beta1 bug. see: https://github.com/rails/rails/issues/406
c = c.reorder nil
# Remove includes only if they are irrelevant
c = c.except(:includes) unless references_eager_loaded_tables?
# .group returns an OrderdHash that responds to #count
c = c.count
c.respond_to?(:count) ? c.count : c
end
end
end

View file

@ -1,18 +1,15 @@
module Kaminari
module DataMapperCollectionMethods
extend ActiveSupport::Concern
module InstanceMethods
def limit_value #:nodoc:
query.options[:limit] || 0
end
def limit_value #:nodoc:
query.options[:limit] || 0
end
def offset_value #:nodoc:
query.options[:offset] || 0
end
def offset_value #:nodoc:
query.options[:offset] || 0
end
def total_count #:nodoc:
model.count(query.options.except(:limit, :offset, :order))
end
def total_count #:nodoc:
model.count(query.options.except(:limit, :offset, :order))
end
end
end

View file

@ -1,25 +1,22 @@
module Kaminari
module MongoidCriteriaMethods
extend ActiveSupport::Concern
module InstanceMethods
def limit_value #:nodoc:
options[:limit]
end
def limit_value #:nodoc:
options[:limit]
end
def offset_value #:nodoc:
options[:skip]
end
def offset_value #:nodoc:
options[:skip]
end
def total_count #:nodoc:
embedded? ? unpage.count : count
end
def total_count #:nodoc:
embedded? ? unpage.count : count
end
private
def unpage
clone.tap do |crit|
crit.options.delete :limit
crit.options.delete :skip
end
private
def unpage
clone.tap do |crit|
crit.options.delete :limit
crit.options.delete :skip
end
end
end

View file

@ -1,40 +1,37 @@
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
# 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
def padding(num)
offset(offset_value + num.to_i)
end
def padding(num)
offset(offset_value + num.to_i)
end
# Total number of pages
def num_pages
(total_count.to_f / limit_value).ceil
end
# Total number of pages
def num_pages
(total_count.to_f / limit_value).ceil
end
# Current page number
def current_page
(offset_value / limit_value) + 1
end
# Current page number
def current_page
(offset_value / limit_value) + 1
end
# First page of the collection ?
def first_page?
current_page == 1
end
# First page of the collection ?
def first_page?
current_page == 1
end
# Last page of the collection?
def last_page?
current_page >= num_pages
end
# Last page of the collection?
def last_page?
current_page >= num_pages
end
end
end

View file

@ -1,18 +1,15 @@
module Kaminari
module PluckyCriteriaMethods
extend ActiveSupport::Concern
module InstanceMethods
def limit_value #:nodoc:
options[:limit]
end
def limit_value #:nodoc:
options[:limit]
end
def offset_value #:nodoc:
options[:skip]
end
def offset_value #:nodoc:
options[:skip]
end
def total_count #:nodoc:
count
end
def total_count #:nodoc:
count
end
end
end
end