Class: SpreeCmCommissioner::Transit::TripForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/spree_cm_commissioner/transit/trip_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allow_bookingObject

Returns the value of attribute allow_booking.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def allow_booking
  @allow_booking
end

#allow_seat_selectionObject

Returns the value of attribute allow_seat_selection.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def allow_seat_selection
  @allow_seat_selection
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def name
  @name
end

#priceObject

Returns the value of attribute price.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def price
  @price
end

#route_idObject

Returns the value of attribute route_id.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def route_id
  @route_id
end

#route_typeObject

Returns the value of attribute route_type.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def route_type
  @route_type
end

#service_calendarObject

Returns the value of attribute service_calendar.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def service_calendar
  @service_calendar
end

#short_nameObject

Returns the value of attribute short_name.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def short_name
  @short_name
end

#total_vehiclesObject

Returns the value of attribute total_vehicles.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def total_vehicles
  @total_vehicles
end

#trip_stopsObject

Returns the value of attribute trip_stops.



5
6
7
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5

def trip_stops
  @trip_stops
end

Instance Method Details

#multi_leg?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 41

def multi_leg?
  trip_stops.map(&:location_id).uniq.size > 2
end

#total_duration_secondsObject



45
46
47
48
49
50
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 45

def total_duration_seconds
  return 0 if trip_stops.size < 2

  minutes = trip_stops[0...-1].sum { |s| s.duration_in_minutes || 0 }
  minutes * 60
end

#trip_stops_attributes=(arr) ⇒ Object

Convert params hashes into TripStopForm objects



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 17

def trip_stops_attributes=(arr)
  items = arr.is_a?(Hash) ? arr.values : Array(arr)
  @trip_stops = items.map do |h|
    h = h.to_h.symbolize_keys
    TripStopForm.new.tap do |ts|
      ts.location_id = h[:location_id]
      ts.stop_id = h[:stop_id]
      ts.departure_time = h[:departure_time]
      ts.duration_in_hours = h[:duration_in_hours]
      ts.route_type = h[:route_type]
      ts.vehicle_type_id = h[:vehicle_type_id]
      ts.vehicle_id = h[:vehicle_id]
      ts.board_to_trip_id = h[:board_to_trip_id]
      ts.allow_boarding = h[:allow_boarding]
      ts.allow_drop_off = h[:allow_drop_off]
      ts.allow_seat_selection = h[:allow_seat_selection]
      ts.allow_booking = h[:allow_booking]
      ts.sequence = h[:sequence]
      ts.vendor_id = h[:vendor_id]
      ts.stop_type = h[:stop_type]
    end
  end
end

#trip_stops_grouped_by_leg(stops) ⇒ Object

Groups trip_stops into legs by location changes Separates legs when location_id changes (at branch stops) Break stops (stop_type = :stop) are included but don’t trigger separation

Example:

PP1, PP2, PTT1(break stop), BTB1, BTB2, PPT2(break stop), POIPET1, POIPET1

Output (legs): [

[PP1, PP2, PTT, BTB1, BTB2],
[BTB1, BTB2, PPT1, POIPET1, POIPET1]

]



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 80

def trip_stops_grouped_by_leg(stops)
  return [] if trip_stops.empty?

  legs = []
  current_leg = []
  last_location_id = nil

  trip_stops.each do |trip_stop|
    stop = stops[trip_stop.stop_id]

    # If location changed and we have stops in current leg, start a new leg
    current_leg << trip_stop
    if last_location_id.present? && last_location_id != trip_stop.location_id && current_leg.any?
      # Include the first stop of new location in previous leg
      legs << current_leg
      current_leg = [trip_stop]
    end

    # Update location only for branch stops (not for "just stops")
    last_location_id = trip_stop.location_id if stop.branch?
  end

  # Add final leg if it has stops
  legs << current_leg if current_leg.any?
  legs
end

#valid_data?Boolean

rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 52

def valid_data? # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  errors.clear

  errors.add(:route_id, 'must be present') if route_id.blank?
  errors.add(:trip_stops, 'must be present') if trip_stops.blank? || trip_stops.size < 2

  first_stop = trip_stops&.first
  last_stop  = trip_stops&.last

  errors.add(:trip_stops, 'first stop must allow boarding') unless first_stop&.allow_boarding?
  errors.add(:trip_stops, 'last stop must allow drop off')   unless last_stop&.allow_drop_off?
  errors.add(:departure_time, 'missing on first stop')       if first_stop&.departure_time.blank?

  errors.empty?
end