Class: Stall::AddToCartService

Inherits:
AddToProductListService show all
Defined in:
app/services/stall/add_to_cart_service.rb

Instance Attribute Summary

Attributes inherited from AddToProductListService

#params, #product_list

Instance Method Summary collapse

Methods inherited from AddToProductListService

#initialize, #line_item, #line_item_params?

Constructor Details

This class inherits a constructor from Stall::AddToProductListService

Instance Method Details

#callObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/services/stall/add_to_cart_service.rb', line 5

def call
  return false unless line_item_valid?

  cart.line_items << line_item unless assemble_identical_line_items

  cart.save.tap do |saved|
    return false unless saved

    # Recalculate shipping fee if available for calculation to ensure
    # that the fee us always up to date when displayed to the customer
    shipping_fee_service.call if shipping_fee_service.available?
  end
end

#enough_stock?Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'app/services/stall/add_to_cart_service.rb', line 23

def enough_stock?
  stock_service = Stall.config.service_for(:available_stocks).new
  stock_service.on_hand?(line_item)
end

#line_item_valid?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/services/stall/add_to_cart_service.rb', line 19

def line_item_valid?
  line_item.valid? && enough_stock?
end