diff options
| -rw-r--r-- | app/controllers/referential_vehicle_journeys_controller.rb | 12 | ||||
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 19 |
2 files changed, 29 insertions, 2 deletions
diff --git a/app/controllers/referential_vehicle_journeys_controller.rb b/app/controllers/referential_vehicle_journeys_controller.rb index 3f3f3d9e6..b07d6c600 100644 --- a/app/controllers/referential_vehicle_journeys_controller.rb +++ b/app/controllers/referential_vehicle_journeys_controller.rb @@ -21,8 +21,16 @@ class ReferentialVehicleJourneysController < ChouetteController @q = ransack_period_range(scope: @q, error_message: t('vehicle_journeys.errors.time_table'), query: :with_matching_timetable, prefix: :time_table) @starting_stop = params[:q] && params[:q][:stop_areas] && params[:q][:stop_areas][:start].present? ? Chouette::StopArea.find(params[:q][:stop_areas][:start]) : nil @ending_stop = params[:q] && params[:q][:stop_areas] && params[:q][:stop_areas][:end].present? ? Chouette::StopArea.find(params[:q][:stop_areas][:end]) : nil - @q = @q.starting_with(@starting_stop&.id) - @q = @q.ending_with(@ending_stop&.id) + + if @starting_stop + @q = + unless @ending_stop + @q.with_stop_area_id(@starting_stop.id) + else + @q.with_ordered_stop_area_ids(@starting_stop.id, @ending_stop.id) + end + end + @q = @q.ransack(params[:q]) @vehicle_journeys ||= @q.result @vehicle_journeys = parse_order @vehicle_journeys diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index e17376612..1963797d2 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -52,6 +52,25 @@ module Chouette end } + scope :with_stop_area_id, ->(id){ + if id.present? + joins(journey_pattern: :stop_points).where('stop_points.stop_area_id = ?', id) + else + all + end + } + + scope :with_ordered_stop_area_ids, ->(first, second){ + if first.present? && second.present? + joins(journey_pattern: :stop_points). + joins('INNER JOIN "journey_patterns" ON "journey_patterns"."id" = "vehicle_journeys"."journey_pattern_id" INNER JOIN "journey_patterns_stop_points" ON "journey_patterns_stop_points"."journey_pattern_id" = "journey_patterns"."id" INNER JOIN "stop_points" as "second_stop_points" ON "stop_points"."id" = "journey_patterns_stop_points"."stop_point_id"'). + where('stop_points.stop_area_id = ?', first). + where('second_stop_points.stop_area_id = ? and stop_points.position < second_stop_points.position', second) + else + all + end + } + scope :starting_with, ->(id){ if id.present? joins(journey_pattern: :stop_points).where('stop_points.position = 0 AND stop_points.stop_area_id = ?', id) |
