Sha256: 6f6ad1fdce3e6d534b2bbb181129bc983b92b4ad7b208b0af05a1062005c6e24

Contents?: true

Size: 930 Bytes

Versions: 2

Compression:

Stored size: 930 Bytes

Contents

module Pagers
  module ActiveRecord
    module Relation
      extend ActiveSupport::Concern

      def total_pages
        @total_pages ||= [(total_count.to_f / page_length).ceil, 1].max
      end

      def previous_page
        @previous_page ||= current_page > 1 ? (current_page - 1) : nil
      end

      def next_page
        @next_page ||= current_page < total_pages ? (current_page + 1) : nil
      end

      def first_page
        1
      end

      def last_page
        total_pages
      end

      def out_of_bounds?
        @out_of_bounds ||= (current_page > total_pages || current_page < first_page)
      end
 
      def total_count
        @total_count ||= begin
          r = except(:limit, :offset, :order, :reorder)
          r = r.except(:includes) unless references_eager_loaded_tables?
          r = r.count
          (r.respond_to?(:count) ? r.count : r) - padding
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pagers-3.0.2 lib/pagers/active_record/relation.rb
pagers-3.0.1 lib/pagers/active_record/relation.rb