aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/chouette/vehicle_journey.rb21
-rw-r--r--spec/models/chouette/vehicle_journey_spec.rb21
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)}