Class: Stall::AddToCartService
- Inherits:
-
BaseService
- Object
- BaseService
- Stall::AddToCartService
- Defined in:
- app/services/stall/add_to_cart_service.rb
Instance Attribute Summary collapse
-
#cart ⇒ Object
readonly
Returns the value of attribute cart.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #call ⇒ Object
- #enough_stock? ⇒ Boolean
-
#initialize(cart, params) ⇒ AddToCartService
constructor
A new instance of AddToCartService.
- #line_item ⇒ Object
- #line_item_valid? ⇒ Boolean
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
#cart ⇒ Object (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 |
#params ⇒ Object (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
#call ⇒ Object
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
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_item ⇒ Object
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
26 27 28 |
# File 'app/services/stall/add_to_cart_service.rb', line 26 def line_item_valid? line_item.valid? && enough_stock? end |