Sha256: cd9f13b1e1587d64b42696a810db6d44d376808e0066a895cee45c247094e10e

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'pagy_mongoid_cursor/pagy/cursor'

class Pagy
  DEFAULT[:cursor_last_page_link] = true

  module CursorExtra
    private

    def pagy_cursor(collection, vars={})
      pagy = Pagy::Cursor.new(pagy_cursor_get_vars(collection, vars))
      [pagy, pagy_cursor_get_items(collection, pagy)]
    end

    def pagy_cursor_get_vars(_collection, vars)
      pagy_set_items_from_params(vars) if defined?(ItemsExtra)
      vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
      vars
    end

    def pagy_cursor_get_items(collection, pagy)
      items = collection.order(id: :asc)

      if pagy.page.nil?
        items = items.limit(pagy.items + 1).to_a
      else
        items = items.where(:id.gte => pagy.page).limit(pagy.items + 1).to_a

        prev_items = collection.where(:id.lt => pagy.page).
                                order(id: :desc).
                                limit(pagy.items).
                                to_a.reverse
        prev_id = prev_items.first.id if prev_items.size == pagy.items
      end
      next_id = items.pop&.id if items.size > pagy.items

      if pagy.vars[:cursor_last_page_link]
        last_items = collection.order(id: :desc).
                                limit(pagy.items).
                                only(:id).to_a
        last_id = last_items.last.id if last_items.size == pagy.items
      end

      pagy.finalize(prev_id, next_id, last_id)
      items
    end

  end
  Backend.prepend CursorExtra
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pagy_mongoid_cursor-0.1.0 lib/pagy_mongoid_cursor/pagy/extras/cursor.rb