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
22
23
24
25
26
27
# File 'app/services/spree/stock_locations/stock_items/create.rb', line 7

def call(stock_location:, variants_scope: Spree::Variant)
  if Rails::VERSION::MAJOR >= 6
    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
  else
    variants_scope.find_each do |variant|
      stock_location.propagate_variant(variant)
    end
  end
end