Sha256: 162a2bb42302fa6d66ac0c23bc4daa0ea7406943ad367abbd6dccd2438d5bbc2

Contents?: true

Size: 988 Bytes

Versions: 1

Compression:

Stored size: 988 Bytes

Contents

module CursorPaginator
  module Paginator
    class ActiveRecord < Base
      def paginate(scope)
        records = scope.reorder(paginator_options.fetch(:order_key) => order_direction)
        records = filter_by_cursor(records) if options_parser.filter_required?

        PaginationResult.new(records, self)
      end

      def take_records(records, limit)
        return super if records.limit_value.blank?

        super(records, [ records.limit_value, limit ].min)
      end

      private

      def filter_by_cursor(collection)
        arel_table      = collection.arel_table
        order_column    = arel_table[paginator_options.fetch(:order_key)]
        primary_column  = arel_table[paginator_options.fetch(:primary_key)]
        cursor_select   = arel_table.project(order_column).where(primary_column.eq(options_parser.cursor))
        where_clause = order_column.public_send(query_operator, cursor_select)

        collection.where(where_clause)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cursor_paginator-0.2.1 lib/cursor_paginator/paginator/active_record.rb