Class: Showroom::Collection
- 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.
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
Class Method Summary collapse
-
.find(handle) ⇒ Collection
Fetches a single collection by handle.
- .index_key ⇒ Object
- .index_path ⇒ Object
-
.where(limit: Showroom.per_page, **params) ⇒ Array<Collection>
Fetches collections matching the given query parameters.
Instance Method Summary collapse
-
#products(**params) ⇒ Array<Product>
Fetches a single page of products belonging to this collection.
-
#products_count ⇒ Integer?
Returns the number of products in this collection as reported by the API.
-
#url ⇒ String
Returns the canonical storefront URL for this collection.
Methods included from Showroom::Core::Countable
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.
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_key ⇒ Object
20 |
# File 'lib/showroom/models/collection.rb', line 20 def index_key = 'collections' |
.index_path ⇒ Object
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.
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.
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_count ⇒ Integer?
Returns the number of products in this collection as reported by the API.
48 49 50 |
# File 'lib/showroom/models/collection.rb', line 48 def products_count @attrs['products_count'] end |