Class: Stall::CartUpdateService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart, params = {}) ⇒ CartUpdateService

Returns a new instance of CartUpdateService.



5
6
7
8
# File 'app/services/stall/cart_update_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/cart_update_service.rb', line 3

def cart
  @cart
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#callObject



10
11
12
13
14
15
# File 'app/services/stall/cart_update_service.rb', line 10

def call
  cart.update(params).tap do |saved|
    return false unless saved
    refresh_associated_services!
  end
end

#refresh_associated_services!Object



17
18
19
20
21
22
23
24
25
# File 'app/services/stall/cart_update_service.rb', line 17

def refresh_associated_services!
  # 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?

  # Recalculate the credit usage amount if already used to avoid negative
  # cart totals
  credit_usage_service.ensure_valid_or_remove! if credit_usage_service.available? && credit_usage_service.credit_used?
end