Class: Shopify::Kaminari::Collection

Inherits:
ActiveResource::Collection
  • Object
show all
Defined in:
lib/shopify/kaminari/collection.rb

Overview

Shopify resources are paginated server-side. This custom ActiveResource::Collection provides pagination that is compatible with the “kaminari” gem. Requiring this gem in your project configures the ShopifyAPI so that every Shopify resource will use this collection by default.

Examples:

Paginating products.

products = Shopify::Product.all(params: { page: 2, limit: 25 })

Constant Summary collapse

DEFAULT_LIMIT_VALUE =

Shopify returns this many records by default if no limit is given.

50

Instance Method Summary collapse

Instance Method Details

#current_pageInteger

The page that was requested from the API.

Returns:

  • (Integer)

    The current page.



20
21
22
# File 'lib/shopify/kaminari/collection.rb', line 20

def current_page
  @current_page ||= original_params.fetch(:page, 1).to_i
end

#limit_valueInteger

The maximum amount of records each page will have.

Returns:

  • (Integer)

    Maximum amount of resources per page.



27
28
29
# File 'lib/shopify/kaminari/collection.rb', line 27

def limit_value
  @limit_value ||= original_params.fetch(:limit, DEFAULT_LIMIT_VALUE).to_i
end

#model_nameActiveModel::Name

Provides a Rails compatible model name for Kaminari’s view helpers.

Returns:

  • (ActiveModel::Name)

    The model’s name.



34
35
36
37
38
39
40
41
42
43
# File 'lib/shopify/kaminari/collection.rb', line 34

def model_name
  @model_name ||= begin
    # Find the name of the resource with the ShopifyAPI:: prefix.
    name = resource_class.name.rpartition('::').last

    # Providing the name prevents every name from being prefixed with
    # "shopify_api_".
    ActiveModel::Name.new(resource_class, nil, name)
  end
end

#next_pageInteger, NilClass

Gets the number of the next page.

Returns:

  • (Integer, NilClass)

    The next page number, or nil if on the last.



48
49
50
# File 'lib/shopify/kaminari/collection.rb', line 48

def next_page
  @next_page ||= current_page + 1 if current_page < total_pages
end

#prev_pageInteger, NilClass

Gets the number of the previous page.

Returns:

  • (Integer, NilClass)

    The previous page number or nil if on the first.



55
56
57
# File 'lib/shopify/kaminari/collection.rb', line 55

def prev_page
  @prev_page ||= current_page - 1 if current_page > 1
end

#total_pagesInteger

Calculates the total number of expected pages based on the number reported by the API.

Returns:

  • (Integer)

    Total number of pages.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shopify/kaminari/collection.rb', line 63

def total_pages
  @total_pages ||= begin
    # Get the parameters without the pagination parameters.
    params = original_params.with_indifferent_access.except(:page, :limit)

    options = params.empty? ? {} : params

    # Ask Shopify how many records there are for the given query.
    count = resource_class.count(options)

    # Calculate the number of pages.
    (count.to_f / limit_value.to_f).ceil
  end
end