Class: Spree::Delivery

Inherits:
Object
  • Object
show all
Includes:
HasTrackingCarrier, SingleStoreResource
Defined in:
app/models/spree/delivery.rb

Overview

One consignment's carrier journey — a parcel, or three pallets under one PRO number — with the tracking number, carrier and the carrier's own status axis (docs/plans/6.0-shipping-labels-and-deliveries.md).

Owned polymorphically by a Spree::Fulfillment (outbound) or a Spree::Return (inbound), and optionally minted by a shipping label. The owner's own lifecycle never lives here: a fulfillment is delivered when every one of its deliveries is, and that rollup belongs to Spree::Deliveries::UpdateTracking.

status is data the carrier reports, written by that workflow. It has no transition graph: carriers resend the whole picture on every update.

Constant Summary collapse

STATUSES =
%w[
  pending pre_transit in_transit out_for_delivery available_for_pickup
  delivered return_to_sender failure unknown
].freeze
OWNER_TYPES =
%w[Spree::Fulfillment Spree::Return].freeze

Instance Method Summary collapse

Methods included from HasTrackingCarrier

#carrier_name

Instance Method Details

#correction_attributes(attributes) ⇒ Hash

What changes when a merchant corrects the tracking number: to the carrier a different number is a different parcel, so the journey starts over and everything belonging to the old number goes with it — its status, its arrival and ETA, the scans it collected, and the carrier and link that described it. Otherwise a UPS badge and a UPS page survive onto a FedEx number, a stale arrival keeps dating the returns window, and the new parcel shows the old one's scan history.

Parameters:

  • attributes (Hash) —

    the merchant's edit

Returns:

  • (Hash) —

    the edit, widened when the number really changed



71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/spree/delivery.rb', line 71

def correction_attributes(attributes)
  number = attributes[:tracking_number] || attributes['tracking_number']
  return attributes if number.blank? || number.to_s.squish == tracking_number

  corrected = attributes.merge(
    status: 'pending', delivered_at: nil, estimated_delivery_at: nil, details: nil
  )
  corrected[:carrier] = nil if corrected[:carrier].blank? && corrected['carrier'].blank?
  corrected[:tracking_url] = nil if corrected[:tracking_url].blank? && corrected['tracking_url'].blank?
  corrected
end

#delivered? ⇒ Boolean

The one status with a reader beyond the scopes: arrival is what the fulfillment rolls up from. The rest are read as status.

Returns:

  • (Boolean)


57
58
59
# File 'app/models/spree/delivery.rb', line 57

def delivered?
  status == 'delivered'
end

#pasted_link? ⇒ Boolean

Returns whether the tracking value is a full link rather than a number.

Returns:

  • (Boolean) —

    whether the tracking value is a full link rather than a number



108
109
110
# File 'app/models/spree/delivery.rb', line 108

def pasted_link?
  tracking_number.to_s.start_with?('https://', 'http://')
end

#resolved_tracking_url ⇒ String?

The public page where this consignment can be followed, best answer first: the link stored on the row (pasted by the merchant, or the provider's tracker page), the pinned carrier's registered tracking page, the delivery method's configured format, then detection from the number's format.

Distinct from the tracking_url column so that clearing the column means "no link I chose" rather than "no link at all" — the derived answer still applies, and a merchant can always store one explicitly.

Returns:

  • (String, nil)


96
97
98
99
100
101
102
103
104
105
# File 'app/models/spree/delivery.rb', line 96

def resolved_tracking_url
  return if tracking_number.blank?

  safe_tracking_url ||
    (tracking_number if pasted_link?) ||
    provider_tracking_url.presence ||
    carrier_tracking_url.presence ||
    delivery_method_tracking_url.presence ||
    detected_tracking_url
end