Class: Shopify::Kaminari::Collection
- Inherits:
-
ActiveResource::Collection
- Object
- ActiveResource::Collection
- Shopify::Kaminari::Collection
- 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.
Constant Summary collapse
- DEFAULT_LIMIT_VALUE =
Shopify returns this many records by default if no limit is given.
50
Instance Method Summary collapse
-
#current_page ⇒ Integer
The page that was requested from the API.
-
#limit_value ⇒ Integer
The maximum amount of records each page will have.
-
#model_name ⇒ ActiveModel::Name
Provides a Rails compatible model name for Kaminari’s view helpers.
-
#next_page ⇒ Integer, NilClass
Gets the number of the next page.
-
#prev_page ⇒ Integer, NilClass
Gets the number of the previous page.
-
#total_pages ⇒ Integer
Calculates the total number of expected pages based on the number reported by the API.
Instance Method Details
#current_page ⇒ Integer
The page that was requested from the API.
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_value ⇒ Integer
The maximum amount of records each page will have.
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_name ⇒ ActiveModel::Name
Provides a Rails compatible model name for Kaminari’s view helpers.
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_page ⇒ Integer, NilClass
Gets the number of the next page.
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_page ⇒ Integer, NilClass
Gets the number of the previous page.
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_pages ⇒ Integer
Calculates the total number of expected pages based on the number reported by the API.
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) = params.empty? ? {} : params # Ask Shopify how many records there are for the given query. count = resource_class.count() # Calculate the number of pages. (count.to_f / limit_value.to_f).ceil end end |