Class: Spree::StockLocations::StockItems::Create

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

Instance Method Summary collapse

Methods included from Spree::ServiceModule::Base

prepended

Instance Method Details

#call(stock_location:, variants_scope: Spree::Variant) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/spree/stock_locations/stock_items/create.rb', line 7

def call(stock_location:, variants_scope: Spree::Variant)
  prepared_stock_items = variants_scope.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_items.any?
    stock_location.stock_items.insert_all(prepared_stock_items)
    variants_scope.touch_all
  end
end