Class: FreightEventStatusSummary
- Inherits:
-
Object
- Object
- FreightEventStatusSummary
- Defined in:
- app/services/freight_event_status_summary.rb
Overview
Compresses a delivery's freight_events rows into the 0–3 status badges
rendered in the warehouse dashboard and on the delivery show page header
for Freightquote LTL shipments.
Each bucket (load / pickup / delivery) yields a single badge — or nothing,
if no event for that bucket has landed yet. Within a bucket, a "completed"
event (LOAD BOOKED, LOAD PICKED UP, LOAD DELIVERED, …) trumps the earlier
in-progress event (LOAD CREATED, APPOINTMENT UPDATED, IN TRANSIT) and the
badge flips from orange (:warning) to green (:success).
Use FreightEventStatusSummary.for (or FreightEventStatusSummary.for_delivery) from a view / helper; the result is small
enough to compute on demand per render.
Defined Under Namespace
Classes: Badge
Constant Summary collapse
- LOAD_COMPLETED_TYPES =
Bucketing rules — first match in the priority order below wins.
['LOAD BOOKED'].freeze
- LOAD_CREATED_TYPES =
['LOAD CREATED'].freeze
- PICKUP_COMPLETED_TYPES =
['CARRIER ARRIVED', 'LOAD PICKED UP', 'CARRIER DEPARTED'].freeze
- PICKUP_SCHEDULED_TYPES =
['APPOINTMENT UPDATED'].freeze
- DELIVERY_COMPLETED_TYPES =
['LOAD DELIVERED', 'ORDER COMPLETED'].freeze
- DELIVERY_IN_TRANSIT_TYPES =
['IN TRANSIT'].freeze
Class Method Summary collapse
-
.for(delivery) ⇒ Array<Badge>
(also: for_delivery)
Zero, one, two, or three badges; never nil.
Instance Method Summary collapse
- #call ⇒ Array<Badge>
-
#initialize(delivery) ⇒ FreightEventStatusSummary
constructor
A new instance of FreightEventStatusSummary.
Constructor Details
#initialize(delivery) ⇒ FreightEventStatusSummary
Returns a new instance of FreightEventStatusSummary.
48 49 50 51 52 53 54 |
# File 'app/services/freight_event_status_summary.rb', line 48 def initialize(delivery) @delivery = delivery # Sort in Ruby instead of via `.order(:event_time)` on the association # so that callers who preloaded `:freight_events` (warehouse dashboard) # don't pay an extra round-trip per row. Stable ordering: oldest first. @events = delivery.freight_events.to_a.sort_by { |e| e.event_time || Time.zone.at(0) } end |
Class Method Details
.for(delivery) ⇒ Array<Badge> Also known as: for_delivery
Returns zero, one, two, or three badges; never nil.
38 39 40 |
# File 'app/services/freight_event_status_summary.rb', line 38 def self.for(delivery) new(delivery).call end |
Instance Method Details
#call ⇒ Array<Badge>
57 58 59 |
# File 'app/services/freight_event_status_summary.rb', line 57 def call [load_badge, pickup_badge, delivery_badge].compact end |