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.
-
#name ⇒ Object
Returns the value of attribute name.
-
#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.
-
#trip_stops ⇒ Object
Returns the value of attribute trip_stops.
Instance Method Summary collapse
- #multi_leg? ⇒ Boolean
- #total_duration_seconds ⇒ Object
-
#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.
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 |
#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 |
#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 |
#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
15 16 17 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 15 def multi_leg? trip_stops.map(&:location_id).uniq.size > 2 end |
#total_duration_seconds ⇒ Object
19 20 21 22 23 24 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 19 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_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]]
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 54 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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/spree_cm_commissioner/transit/trip_form.rb', line 26 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 |