Class: Showroom::Collection

Inherits:
Resource show all
Extended by:
Showroom::Core::Countable
Defined in:
lib/showroom/models/collection.rb

Overview

Represents a Shopify collection with class-level query methods that delegate to client and an instance-level products fetcher.

Examples:

Fetching collections

collections = Showroom::Collection.where
collection  = Showroom::Collection.find('lorem-helmets')

Fetching products in a collection

collection.products(limit: 10)

Constant Summary

Constants included from Showroom::Core::Countable

Showroom::Core::Countable::MAX_COUNT, Showroom::Core::Countable::MAX_PAGE, Showroom::Core::Countable::MAX_PER_PAGE

Instance Attribute Summary

Attributes inherited from Resource

#attrs, #client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Showroom::Core::Countable

calculate_count

Methods inherited from Resource

#==, #[], has_many, has_one, #initialize, #inspect, main_attr_keys, main_attrs, #method_missing, #respond_to_missing?, #to_h

Constructor Details

This class inherits a constructor from Showroom::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Showroom::Resource

Class Method Details

.find(handle) ⇒ Collection

Fetches a single collection by handle.

Parameters:

  • handle (String)

    the collection handle

Returns:

Raises:



38
39
40
41
42
# File 'lib/showroom/models/collection.rb', line 38

def find(handle)
  Showroom.client.get("/collections/#{handle}.json")
          .fetch('collection') { raise Showroom::NotFound, handle }
          .then { |h| new(h) }
end

.index_keyObject



20
# File 'lib/showroom/models/collection.rb', line 20

def index_key  = 'collections'

.index_pathObject



19
# File 'lib/showroom/models/collection.rb', line 19

def index_path = '/collections.json'

.where(limit: Showroom.per_page, **params) ⇒ Array<Collection>

Fetches collections matching the given query parameters.

Parameters:

  • params (Hash)

    Shopify query parameters

Returns:



27
28
29
30
31
# File 'lib/showroom/models/collection.rb', line 27

def where(limit: Showroom.per_page, **params)
  Showroom.client.get('/collections.json', params.merge(limit: limit))
          .fetch('collections', [])
          .map { |h| new(h) }
end

Instance Method Details

#products(**params) ⇒ Array<Product>

Fetches a single page of products belonging to this collection.

Returns at most one page of results. Use limit: 250 to maximise the number of products returned in a single request. For collections with more products than the page size, pass page: explicitly to retrieve subsequent pages.

Parameters:

  • params (Hash)

    Shopify query parameters (e.g. limit:, page:)

Returns:



61
62
63
64
65
66
# File 'lib/showroom/models/collection.rb', line 61

def products(**params)
  conn = client || Showroom.client
  conn.get("/collections/#{handle}/products.json", params)
      .fetch('products', [])
      .map { |h| Product.new(h).tap { |r| r.client = conn } }
end

#products_countInteger?

Returns the number of products in this collection as reported by the API.

Returns:

  • (Integer, nil)


48
49
50
# File 'lib/showroom/models/collection.rb', line 48

def products_count
  @attrs['products_count']
end

#urlString

Returns the canonical storefront URL for this collection.

Returns:

  • (String)


71
72
73
74
# File 'lib/showroom/models/collection.rb', line 71

def url
  conn = client || Showroom.client
  "#{conn.base_url}/collections/#{handle}"
end