diff options
| author | Teddy Wing | 2017-05-12 16:14:41 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-05-12 17:56:27 +0200 |
| commit | 0898f39c35f2f853e051269ae0945eafa475d483 (patch) | |
| tree | 5b5b36afa0e5adae7a4ab653c77e260b25d3259d | |
| parent | ebb4281984bd16bf6ee16fad6d664b7793056d62 (diff) | |
| download | chouette-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
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 21 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 32 |
2 files changed, 53 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 diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 7ddb82b70..231cbfaaf 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -310,6 +310,38 @@ describe Chouette::VehicleJourney, :type => :model do end end + describe ".departure_time_between" do + it "selects vehicle journeys whose departure times are between the + specified range" do + journey_early = create( + :vehicle_journey, + stop_departure_time: '02:00:00' + ) + + route = journey_early.route + journey_pattern = journey_early.journey_pattern + + journey_middle = create( + :vehicle_journey, + route: route, + journey_pattern: journey_pattern, + stop_departure_time: '03:00:00' + ) + journey_late = create( + :vehicle_journey, + route: route, + journey_pattern: journey_pattern, + stop_departure_time: '04:00:00' + ) + + expect(route + .vehicle_journeys + .departure_time_between('02:30', '03:30') + .to_a + ).to eq([journey_middle]) + end + end + subject { create(:vehicle_journey_odd) } describe "in_relation_to_a_journey_pattern methods" do let!(:route) { create(:route)} |
