diff options
| author | Teddy Wing | 2017-05-22 13:44:45 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-05-22 13:44:45 +0200 |
| commit | 9ff6f376add6ff06ccd09f9b73b8d903149c7a7b (patch) | |
| tree | c75004c5be780847944e4756c9c203258111a293 | |
| parent | 61cffec83b8d83fd330175cd9798cecd9338e967 (diff) | |
| download | chouette-core-9ff6f376add6ff06ccd09f9b73b8d903149c7a7b.tar.bz2 | |
VehicleJourney: Add #exclude_journeys_without_time_tables
A new class method that allows us to filter out vehicle journeys that
aren't associated with `TimeTable`s.
This is what back the filter toggle on VehicleJourneys#index for
"Afficher les courses sans calendrier".
Using a subquery to allow us to filter out duplicates, so we don't end
up with duplicate vehicle journeys in the final result set. Then we just
remove the vehicle journeys that have no entries in the
`time_tables_vehicle_journeys` through table, leaving us with only the
vehicle journeys that have associated calendars.
Refs #3427
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 21 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 21 |
2 files changed, 42 insertions, 0 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 852139625..514e89fb1 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -252,5 +252,26 @@ module Chouette ) end + def self.exclude_journeys_without_time_tables + # Joins the VehicleJourney–TimeTable through table to remove + # VehicleJourneys that don't have an associated TimeTable. The subquery + # allows us to select unique VehicleJourney IDs from the through table, + # which prevents VehicleJourneys from appearing multiple times in the + # result set due to the join. Using a DISTINCT in a subquery avoids messy + # DISTINCT and GROUP BY clauses in the main query. + self + .joins(' + LEFT JOIN ( + SELECT DISTINCT + "time_tables_vehicle_journeys"."vehicle_journey_id" + FROM "time_tables_vehicle_journeys" + ) AS "distinct_time_tables_vehicle_journeys" + ON "distinct_time_tables_vehicle_journeys"."vehicle_journey_id" = + "vehicle_journeys"."id" + ') + .where('"distinct_time_tables_vehicle_journeys"."vehicle_journey_id" + IS NOT NULL') + end + end end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 7f2305134..446eb9260 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -365,6 +365,27 @@ describe Chouette::VehicleJourney, :type => :model do end end + describe ".exclude_journeys_without_time_tables" do + it "filters out vehicle journeys not associated with a calendar" do + journey = create(:vehicle_journey) + route = journey.route + + journey_with_time_table = create( + :vehicle_journey, + route: route, + journey_pattern: journey.journey_pattern + ) + journey_with_time_table.time_tables << create(:time_table) + + expect( + route + .vehicle_journeys + .exclude_journeys_without_time_tables + .to_a + ).to eq([journey_with_time_table]) + end + end + subject { create(:vehicle_journey_odd) } describe "in_relation_to_a_journey_pattern methods" do let!(:route) { create(:route)} |
