Module: Showroom::Core::Countable

Included in:
Showroom::Collection, Product
Defined in:
lib/showroom/core/countable.rb

Overview

Mixin that adds #calculate_count to model classes exposing a paginated index endpoint.

Uses an exponential probe followed by binary search to locate the last non-empty page, then derives the total count. Costs O(log n) HTTP requests where n is the number of pages.

The count is approximate — items may be added or removed between requests.

Examples:

Product.calculate_count    # => 2847  (O(log n) requests)
Collection.calculate_count # => 42

Constant Summary collapse

MAX_PER_PAGE =
250
MAX_PAGE =
100
MAX_COUNT =

25_000

MAX_PER_PAGE * MAX_PAGE

Instance Method Summary collapse

Instance Method Details

#calculate_count(**params) ⇒ Integer

Estimates the total number of items via binary search over pages.

Shopify’s public endpoints reject page numbers above 100, so the maximum reportable count is **25,000** (100 pages × 250 per page). Stores with more items will return 25,000. Any limit: key in params is ignored — the probe always uses MAX_PER_PAGE (250).

Parameters:

  • params (Hash)

    additional query parameters forwarded to the index endpoint (e.g. product_type:, vendor:). limit: is ignored.

Returns:

  • (Integer)

    approximate total item count, capped at 25,000



33
34
35
36
37
38
39
# File 'lib/showroom/core/countable.rb', line 33

def calculate_count(**params)
  fetch = ->(page) { page_size(page, **params.except(:limit)) }
  upper = probe_upper_bound(fetch)
  return 0 if upper == 1 && fetch.call(1).zero?

  tally(fetch, upper)
end