Sha256: 85144e024df8190c81e0fb287ea308042d10ccc6bf788ca5ea7eff0e4cd4fea1
Contents?: true
Size: 1.37 KB
Versions: 5
Compression:
Stored size: 1.37 KB
Contents
module SlackRubyBotServer module Api module Helpers module CursorHelpers extend ActiveSupport::Concern # apply cursor-based pagination to a collection # returns a hash: # results: (paginated collection subset) # next: (cursor to the next page) def paginate_by_cursor(coll, &_block) raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor) results = { results: [], next: nil } size = (params[:size] || 10).to_i if params.key?(:offset) skip = params[:offset].to_i coll = coll.skip(skip) end # some items may be skipped with a block query = block_given? ? coll : coll.limit(size) query.scroll(params[:cursor]) do |record, next_cursor| record = yield(record) if block_given? results[:results] << record if record results[:next] = next_cursor.to_s break if results[:results].count >= size end results[:total_count] = coll.count if params[:total_count] && coll.respond_to?(:count) results end def paginate_and_sort_by_cursor(coll, options = {}, &block) Hashie::Mash.new(paginate_by_cursor(sort(coll, options), &block)) end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems