Module: SpreeCmCommissioner::StockItemDecorator

Defined in:
app/models/spree_cm_commissioner/stock_item_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'app/models/spree_cm_commissioner/stock_item_decorator.rb', line 3

def self.prepended(base)
  base.has_one :vendor, through: :variant
  base.after_save :update_vendor_total_inventory, if: :saved_change_to_count_on_hand?

  base.after_commit :create_inventory_items, on: :create
  base.after_commit :adjust_inventory_items_async, on: :destroy

  # Create maintaining task to purge product related caches
  base.after_commit :schedule_product_cache_invalidation
end

Instance Method Details

#schedule_product_cache_invalidationObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/spree_cm_commissioner/stock_item_decorator.rb', line 14

def schedule_product_cache_invalidation
  return unless saved_change_to_count_on_hand?

  # Only invalidate when transitioning between in-stock and out-of-stock:
  # - out-of-stock → in-stock (0 → N): invalidate
  # - in-stock → out-of-stock (N → 0): invalidate
  # - non-zero to non-zero (5 → 3): skip — cached in_stock value is unchanged
  old_val, new_val = saved_change_to_count_on_hand
  return if old_val.to_i.positive? && new_val.to_i.positive?

  SpreeCmCommissioner::MaintenanceTasks::CacheInvalidation.pending.create_or_find_by(maintainable: variant.product)
end

#update_vendor_total_inventoryObject



27
28
29
# File 'app/models/spree_cm_commissioner/stock_item_decorator.rb', line 27

def update_vendor_total_inventory
  SpreeCmCommissioner::VendorJob.perform_later(vendor_id: vendor.id) if vendor.present?
end