Class: Feed::Google::ProductBatchLoader

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/feed/google/product_batch_loader.rb

Overview

Loads Google-shaped product-feed rows in bounded, fully prepared batches.
Google Merchant and OpenAI feeds share this path so image, classification,
variant, and canonical-path work is paid once per batch rather than once per
product (or repeatedly per field on the same product).

Defined Under Namespace

Classes: Batch, VariantMetadata

Constant Summary collapse

BATCH_SIZE =
100
PRELOADS =
[
  { catalog_item: %i[catalog store_item] },
  {
    item: [
      :primary_product_line,
      :product_category,
      :variant_groups,
      { google_feed_image_profiles: :image }
    ]
  }
].freeze

Instance Method Summary collapse

Instance Method Details

#each_batch(catalog, limit: nil, catalog_item_ids: nil, batch_size: BATCH_SIZE) {|batch| ... } ⇒ Enumerator, void

Parameters:

  • catalog (Catalog)
  • limit (Integer, nil) (defaults to: nil)
  • catalog_item_ids (Array<Integer>, nil) (defaults to: nil)
  • batch_size (Integer) (defaults to: BATCH_SIZE)

Yield Parameters:

Returns:

  • (Enumerator, void)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/feed/google/product_batch_loader.rb', line 30

def each_batch(catalog, limit: nil, catalog_item_ids: nil, batch_size: BATCH_SIZE, &block)
  return enum_for(__method__, catalog, limit:, catalog_item_ids:, batch_size:) unless block

  products = catalog.view_product_catalogs
                    .preload(*PRELOADS)
                    .joins(:catalog_item)
                    .merge(CatalogItem.for_google_feed)
  products = products.limit(limit) if limit.present?
  products = products.where(catalog_item_id: catalog_item_ids) if catalog_item_ids.present?
  country_iso = catalog.try(:country)&.iso

  ActiveRecord::Base.uncached do
    products.find_in_batches(batch_size:) do |records|
       = prepare(records, catalog)
      yield Batch.new(records:, country_iso:, variant_metadata:)
    end
  end
end