Class: SpreeCmCommissioner::Transit::TripForm
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Transit::TripForm
- Includes:
- ActiveModel::Model
- Defined in:
- lib/spree_cm_commissioner/transit/trip_form.rb
Instance Attribute Summary collapse
-
#allow_booking ⇒ Object
Returns the value of attribute allow_booking.
-
#allow_seat_selection ⇒ Object
Returns the value of attribute allow_seat_selection.
-
#is_open_dated ⇒ Object
Returns the value of attribute is_open_dated.
-
#name ⇒ Object
Returns the value of attribute name.
-
#open_dated_validity_days ⇒ Object
Returns the value of attribute open_dated_validity_days.
-
#price ⇒ Object
Returns the value of attribute price.
-
#route_id ⇒ Object
Returns the value of attribute route_id.
-
#route_type ⇒ Object
Returns the value of attribute route_type.
-
#service_calendar ⇒ Object
Returns the value of attribute service_calendar.
-
#short_name ⇒ Object
Returns the value of attribute short_name.
-
#total_vehicles ⇒ Object
Returns the value of attribute total_vehicles.
-
#trip_stops ⇒ Object
Returns the value of attribute trip_stops.
Instance Method Summary collapse
- #multi_leg? ⇒ Boolean
- #total_duration_seconds ⇒ Object
-
#trip_stops_attributes=(arr) ⇒ Object
Convert params hashes into TripStopForm objects.
-
#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.
-
#valid_data? ⇒ Boolean
rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity.
- #valid_open_dated_data? ⇒ Boolean
Instance Attribute Details
#allow_booking ⇒ Object
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_selection ⇒ Object
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 |
#is_open_dated ⇒ Object
Returns the value of attribute is_open_dated.
5 6 7 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5 def is_open_dated @is_open_dated end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5 def name @name end |
#open_dated_validity_days ⇒ Object
Returns the value of attribute open_dated_validity_days.
5 6 7 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 5 def open_dated_validity_days @open_dated_validity_days end |
#price ⇒ Object
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_id ⇒ Object
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_type ⇒ Object
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_calendar ⇒ Object
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_name ⇒ Object
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_vehicles ⇒ Object
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_stops ⇒ Object
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
44 45 46 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 44 def multi_leg? trip_stops.count(&:branch?).size >= 3 end |
#total_duration_seconds ⇒ Object
48 49 50 51 52 53 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 48 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 19 def trip_stops_attributes=(arr) # rubocop:disable Metrics/AbcSize 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_minutes = h[:duration_in_minutes] 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] ts.offset_days = h[:offset_days] 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]]
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 94 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 55 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 |
#valid_open_dated_data? ⇒ Boolean
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 71 def valid_open_dated_data? errors.clear errors.add(:route_id, 'must be present') if route_id.blank? errors.add(:price, 'must be present') if price.blank? errors.add(:is_open_dated, 'must be true') unless is_open_dated errors.add(:open_dated_validity_days, 'must be present') if open_dated_validity_days.blank? errors.empty? end |