Class: Spree::StockLocations::StockLevels::Create

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree/stock_locations/stock_levels/create.rb

Instance Method Summary collapse

Methods included from Spree::ServiceModule::Base

prepended

Instance Method Details

#call(stock_location:, variants_scope: nil) ⇒ Object

Parameters:

  • stock_location (Spree::StockLocation)
  • variants_scope (ActiveRecord::Relation, nil) (defaults to: nil) —

    defaults to what the location may stock; always narrowed to it, so no caller can seed rows for another store's or another seller's variants



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/spree/stock_locations/stock_levels/create.rb', line 11

def call(stock_location:, variants_scope: nil)
  variant_ids = propagatable_variants(stock_location, variants_scope).ids
  prepared_stock_levels = variant_ids.map do |variant_id|
    Hash[
      'stock_location_id', stock_location.id,
      'variant_id', variant_id,
      'backorderable', stock_location.backorderable_default,
      'created_at', Time.current,
      'updated_at', Time.current
    ]
  end
  if prepared_stock_levels.any?
    stock_location.stock_levels.insert_all(prepared_stock_levels)
    # By id: MySQL refuses an UPDATE whose own table appears in a subquery.
    Spree::Variant.where(id: variant_ids).touch_all
  end
end