diff options
| author | Teddy Wing | 2017-05-22 15:11:05 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-05-22 15:38:09 +0200 |
| commit | 44712fe052e0c554c0510d4665a35c66540ffa05 (patch) | |
| tree | d519ff8221dd7c469e8026955f515cda0ae14400 | |
| parent | 897bc5fb10b1867970367ede388feac0ac1a5791 (diff) | |
| download | chouette-core-44712fe052e0c554c0510d4665a35c66540ffa05.tar.bz2 | |
VehicleJourney: Make `#exclude_journeys_without_time_tables` do opposite
Cédric clarified the task:
https://projects.af83.io/projects/stif-boiv-realisation/activity?from=2017-05-22
Apparently, the "Afficher les courses sans calendrier" filter needs to
only show vehicle journeys that have no calendar associated when toggled
to "Oui".
When I was implementing this, I had thought that it was the other way
around, and the default neutral state was "Oui", and when set to "Non",
vehicle journeys without calendars would be excluded from the list. This
was apparently wrong.
The default state should actually be "Non", and only non-calendared
vehicle journeys should appear when "Oui".
Update the test and code to reflect this. Rename
`#exclude_journeys_without_time_tables` to `#without_time_tables`
because it should now only select vehicle journeys without time tables.
We can also simplify our query quite a bit, since vehicle journeys that
have no time tables will always be distinct in the left join, so we
don't need to explicitly write an extra step in the join to do that.
Refs #3427
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 23 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 8 |
2 files changed, 11 insertions, 20 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 514e89fb1..d20a3d315 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -252,25 +252,16 @@ 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. + def self.without_time_tables + # Joins the VehicleJourney–TimeTable through table to select only those + # VehicleJourneys that don't have an associated TimeTable. 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" + LEFT JOIN "time_tables_vehicle_journeys" + ON "time_tables_vehicle_journeys"."vehicle_journey_id" = + "vehicle_journeys"."id" ') - .where('"distinct_time_tables_vehicle_journeys"."vehicle_journey_id" - IS NOT NULL') + .where('"time_tables_vehicle_journeys"."vehicle_journey_id" IS NULL') end end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 446eb9260..ed76c278c 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -365,8 +365,8 @@ 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 + describe ".without_time_tables" do + it "selects only vehicle journeys that have no associated calendar" do journey = create(:vehicle_journey) route = journey.route @@ -380,9 +380,9 @@ describe Chouette::VehicleJourney, :type => :model do expect( route .vehicle_journeys - .exclude_journeys_without_time_tables + .without_time_tables .to_a - ).to eq([journey_with_time_table]) + ).to eq([journey]) end end |
