Class: Delivery::ApplySelectedShippingCost

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/delivery/apply_selected_shipping_cost.rb

Overview

Centralized application of a selected ShippingCost to a delivery and its
shipping LineItem — ensures consistency whether the selection was
auto-computed (SetProperShippingCost) or explicitly chosen
(cart shipping picker, CRM shipping-method update).

Syncs the shipping line (item, price, description, discount reset), the
delivery's selection columns, and pushes the carrier down onto every
shipment. With persist: true the delivery columns are written via
update_columns (no callbacks) and the shipping line is saved immediately;
otherwise everything stays in-memory for the caller's save.

Extracted from Delivery#apply_selected_shipping_cost! (god-object
decomposition); the model keeps a thin delegator.

Examples:

Delivery::ApplySelectedShippingCost.new(delivery, shipping_cost).process
Delivery::ApplySelectedShippingCost.new(delivery, sc, previous_selected_id: old_id, persist: true).process

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(delivery, shipping_cost_entry, previous_selected_id: nil, persist: false) ⇒ ApplySelectedShippingCost

Returns a new instance of ApplySelectedShippingCost.



22
23
24
25
26
27
28
# File 'app/services/delivery/apply_selected_shipping_cost.rb', line 22

def initialize(delivery, shipping_cost_entry, previous_selected_id: nil, persist: false)
  super()
  @delivery = delivery
  @entry = shipping_cost_entry
  @previous_selected_id = previous_selected_id
  @persist = persist
end

Instance Method Details

#processvoid

This method returns an undefined value.

Raises:

  • (RuntimeError)

    when the entry's shipping option has no item (a
    shipping line item cannot be built without one)



33
34
35
36
37
38
39
40
41
# File 'app/services/delivery/apply_selected_shipping_cost.rb', line 33

def process
  guard_option_item!
  shipping_line = sync_shipping_line!
  sync_delivery_fields!
  propagate_carrier_to_shipments!
  persist_or_prune!(shipping_line)
  reset_resource_totals!
  Rails.logger.debug { "apply_selected_shipping_cost!, after: shipping_line: #{shipping_line.inspect}" }
end