Class: Spree::Api::Paginate

Inherits:
Object
  • Object
show all
Includes:
Pagy::Method
Defined in:
app/paginators/spree/api/paginate.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, params) ⇒ Paginate

Returns a new instance of Paginate.



8
9
10
11
12
13
14
15
16
17
18
# File 'app/paginators/spree/api/paginate.rb', line 8

def initialize(collection, params)
  @collection = collection
  @params = params

  per_page_limit = Spree::Api::Config[:api_v2_per_page_limit]
  @per_page = if params[:per_page].to_i.between?(1, per_page_limit)
                params[:per_page].to_i
              else
                25
              end
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/paginators/spree/api/paginate.rb', line 20

def call
  # Pass params as a hash-based request object for Pagy
  # Pagy::Request accepts a hash with :params key
  # Use to_unsafe_h for ActionController::Parameters, fallback to to_h for regular hashes
  params_hash = @params.respond_to?(:to_unsafe_h) ? @params.to_unsafe_h : @params.to_h
  request_hash = { params: params_hash }

  # Uses countish paginator which is faster as it avoids COUNT queries
  pagy, records = pagy(:countish, @collection, limit: @per_page, request: request_hash)

  PagyCollection.new(records, pagy)
end