Class: Stall::AddToCartService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart, params) ⇒ AddToCartService

Returns a new instance of AddToCartService.



5
6
7
8
# File 'app/services/stall/add_to_cart_service.rb', line 5

def initialize(cart, params)
  @cart = cart
  @params = params
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



3
4
5
# File 'app/services/stall/add_to_cart_service.rb', line 3

def cart
  @cart
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'app/services/stall/add_to_cart_service.rb', line 3

def params
  @params
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/stall/add_to_cart_service.rb', line 10

def call
  return false unless line_item_valid?

  unless assemble_identical_line_items
    cart.line_items << line_item
  end

  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)


30
31
32
33
# File 'app/services/stall/add_to_cart_service.rb', line 30

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

#line_itemObject



35
36
37
38
39
# File 'app/services/stall/add_to_cart_service.rb', line 35

def line_item
  @line_item ||= sellable.to_line_item.tap do |line_item|
    line_item.quantity = line_item_params[:quantity]
  end
end

#line_item_valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/services/stall/add_to_cart_service.rb', line 26

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