Sha256: 260aed652dc21de4b29ed15e8b80a02f989053685fdc64ce790e5d56cbec3cf1

Contents?: true

Size: 1011 Bytes

Versions: 3

Compression:

Stored size: 1011 Bytes

Contents

module Pagers
  module Extensions
    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)
            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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pagers-5.1.0 lib/pagers/extensions/active_record/relation.rb
pagers-4.0.0.1 lib/pagers/extensions/active_record/relation.rb
pagers-4.0.0.0 lib/pagers/extensions/active_record/relation.rb