aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/chouette
diff options
context:
space:
mode:
authorTeddy Wing2017-05-12 16:14:41 +0200
committerTeddy Wing2017-05-12 17:56:27 +0200
commit0898f39c35f2f853e051269ae0945eafa475d483 (patch)
tree5b5b36afa0e5adae7a4ab653c77e260b25d3259d /app/models/chouette
parentebb4281984bd16bf6ee16fad6d664b7793056d62 (diff)
downloadchouette-core-0898f39c35f2f853e051269ae0945eafa475d483.tar.bz2
VehicleJourney: Add `.departure_time_between` filter
A new class method that allows a vehicle journey collection to be filtered by a departure time range. This will eventually replace the `VehicleJourneysController#ransack_periode_filter` method, moving the filter out of Ransack and into this custom code. The reason we're doing this is because when trying to filter, Ransack inserts a duplicate `LEFT OUTER JOIN` on "vehicle_journey_at_stops" from `VehicleJourneysController#collection`. (`route.vehicle_journeys.with_stops` + the filter causes the duplicate column error.) For the moment, this method lives on its own. It needs more work to be integrated such that we can chain it with the `.with_stops` method. In particular, we still have the duplicate join problem. Refs #3370
Diffstat (limited to 'app/models/chouette')
-rw-r--r--app/models/chouette/vehicle_journey.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb
index 39dcafdeb..5d7ddafd7 100644
--- a/app/models/chouette/vehicle_journey.rb
+++ b/app/models/chouette/vehicle_journey.rb
@@ -220,5 +220,26 @@ module Chouette
.order('"vehicle_journey_at_stops"."departure_time"')
end
+ # Example:
+ # .departure_time_between('08:00', '09:45')
+ def self.departure_time_between(start_time, end_time)
+ self
+ .select('DISTINCT "vehicle_journeys".*')
+ .joins('
+ LEFT JOIN "vehicle_journey_at_stops"
+ ON "vehicle_journey_at_stops"."vehicle_journey_id" =
+ "vehicle_journeys"."id"
+ ')
+ .where(
+ %Q(
+ "vehicle_journey_at_stops"."departure_time" >= ?
+ AND "vehicle_journey_at_stops"."departure_time" < ?
+ OR "vehicle_journey_at_stops"."id" IS NULL
+ ),
+ "2000-01-01 #{start_time}:00 UTC",
+ "2000-01-01 #{end_time}:00 UTC"
+ )
+ end
+
end
end