From 27a4e6076bc3a0969b9dfa1c8bc4dbe66bbd5b75 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 12 Dec 2011 00:14:52 +0900 Subject: [PATCH] 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 https://github.com/rails/rails/commit/401393b6561adc1ce7101945163c9601257c057a --- lib/kaminari/helpers/action_view_extension.rb | 81 +++++++++---------- .../models/active_record_relation_methods.rb | 37 ++++----- .../models/data_mapper_collection_methods.rb | 19 ++--- .../models/mongoid_criteria_methods.rb | 31 ++++--- lib/kaminari/models/page_scope_methods.rb | 55 ++++++------- .../models/plucky_criteria_methods.rb | 21 +++-- 6 files changed, 113 insertions(+), 131 deletions(-) diff --git a/lib/kaminari/helpers/action_view_extension.rb b/lib/kaminari/helpers/action_view_extension.rb index fbf5de0..6c86ee9 100644 --- a/lib/kaminari/helpers/action_view_extension.rb +++ b/lib/kaminari/helpers/action_view_extension.rb @@ -1,49 +1,46 @@ module Kaminari # = Helpers module ActionViewExtension - extend ::ActiveSupport::Concern - module InstanceMethods - # A helper that renders the pagination links. - # - # <%= paginate @articles %> - # - # ==== Options - # * :window - The "inner window" size (4 by default). - # * :outer_window - The "outer window" size (0 by default). - # * :left - The "left outer window" size (0 by default). - # * :right - The "right outer window" size (0 by default). - # * :params - url_for parameters for the links (:controller, :action, etc.) - # * :param_name - parameter name for page number in the links (:page by default) - # * :remote - Ajax? (false by default) - # * :ANY_OTHER_VALUES - 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 + # * :window - The "inner window" size (4 by default). + # * :outer_window - The "outer window" size (0 by default). + # * :left - The "left outer window" size (0 by default). + # * :right - The "right outer window" size (0 by default). + # * :params - url_for parameters for the links (:controller, :action, etc.) + # * :param_name - parameter name for page number in the links (:page by default) + # * :remote - Ajax? (false by default) + # * :ANY_OTHER_VALUES - 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 %> - # No More Pages - # <% 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 %> + # No More Pages + # <% 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 diff --git a/lib/kaminari/models/active_record_relation_methods.rb b/lib/kaminari/models/active_record_relation_methods.rb index 5d014d7..566a4db 100644 --- a/lib/kaminari/models/active_record_relation_methods.rb +++ b/lib/kaminari/models/active_record_relation_methods.rb @@ -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 diff --git a/lib/kaminari/models/data_mapper_collection_methods.rb b/lib/kaminari/models/data_mapper_collection_methods.rb index 8ef87aa..7400b38 100644 --- a/lib/kaminari/models/data_mapper_collection_methods.rb +++ b/lib/kaminari/models/data_mapper_collection_methods.rb @@ -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 diff --git a/lib/kaminari/models/mongoid_criteria_methods.rb b/lib/kaminari/models/mongoid_criteria_methods.rb index 1d94fbc..d401a59 100644 --- a/lib/kaminari/models/mongoid_criteria_methods.rb +++ b/lib/kaminari/models/mongoid_criteria_methods.rb @@ -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 diff --git a/lib/kaminari/models/page_scope_methods.rb b/lib/kaminari/models/page_scope_methods.rb index d3d1588..273f983 100644 --- a/lib/kaminari/models/page_scope_methods.rb +++ b/lib/kaminari/models/page_scope_methods.rb @@ -1,40 +1,37 @@ module Kaminari module PageScopeMethods - extend ActiveSupport::Concern - module InstanceMethods - # 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(offset_value / limit_value * n) - end + # 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(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 diff --git a/lib/kaminari/models/plucky_criteria_methods.rb b/lib/kaminari/models/plucky_criteria_methods.rb index 3c5f3c5..039f6d9 100644 --- a/lib/kaminari/models/plucky_criteria_methods.rb +++ b/lib/kaminari/models/plucky_criteria_methods.rb @@ -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 \ No newline at end of file +end