Class: Workarea::CartCleaner

Inherits:
Object
  • Object
show all
Defined in:
app/services/workarea/cart_cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart) ⇒ CartCleaner

Returns a new instance of CartCleaner.



5
6
7
# File 'app/services/workarea/cart_cleaner.rb', line 5

def initialize(cart)
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



3
4
5
# File 'app/services/workarea/cart_cleaner.rb', line 3

def cart
  @cart
end

Instance Method Details

#cleanObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/workarea/cart_cleaner.rb', line 17

def clean
  items_to_remove = []

  cart.items.each do |item|
    product = products.detect { |p| p.id == item.product_id }
    variant = product && product.variants.detect { |v| v.sku == item.sku }
    price = pricing.for_sku(item.sku, quantity: item.quantity)

    if product.blank?
      items_to_remove << item.id
      self.messages << I18n.t('workarea.carts.product_unavailable', product_id: item.product_id)
      next
    end

    unless product.purchasable? && variant.try(:active?)
      items_to_remove << item.id
      self.messages << I18n.t('workarea.carts.product_unavailable', product_id: item.product_id)
      next
    end

    unless price.persisted?
      items_to_remove << item.id
      self.messages << I18n.t('workarea.carts.product_unavailable', product_id: item.product_id)
      next
    end
  end

  items_to_remove.each do |item_id|
    cart.remove_item(item_id)
  end
end

#message?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/services/workarea/cart_cleaner.rb', line 13

def message?
  messages.present?
end

#messagesObject



9
10
11
# File 'app/services/workarea/cart_cleaner.rb', line 9

def messages
  @messages ||= []
end