diff options
| author | Teddy Wing | 2018-05-14 12:41:27 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-05-14 12:41:27 +0200 |
| commit | 84683c111e16e19c94a71f36f1d66c0745ad33d8 (patch) | |
| tree | 6f2672dec257ffcd940442f8d815293bc9c270e4 | |
| parent | 4c94257d93c33bc2d6a2c4f146ebe0843763c89a (diff) | |
| download | chouette-core-84683c111e16e19c94a71f36f1d66c0745ad33d8.tar.bz2 | |
CleanUp: Add `#destroy_routes_outside_referential`
This builds on `#destroy_vehicle_journeys_outside_referential` (which
will soon be removed in favour of this new method).
It destroys orphaned routes in a referential.
Refs #6854
| -rw-r--r-- | app/models/clean_up.rb | 5 | ||||
| -rw-r--r-- | spec/models/clean_up_spec.rb | 20 |
2 files changed, 25 insertions, 0 deletions
diff --git a/app/models/clean_up.rb b/app/models/clean_up.rb index 0f73e07b2..cf8908263 100644 --- a/app/models/clean_up.rb +++ b/app/models/clean_up.rb @@ -105,6 +105,11 @@ class CleanUp < ApplicationModel Chouette::VehicleJourney.joins(:route).where(["routes.line_id not in (?)", line_ids]).destroy_all end + def destroy_routes_outside_referential + line_ids = referential.metadatas.pluck(:line_ids).flatten.uniq + Chouette::Route.where(['line_id not in (?)', line_ids]).destroy_all + end + def destroy_vehicle_journeys Chouette::VehicleJourney.where("id not in (select distinct vehicle_journey_id from time_tables_vehicle_journeys)").destroy_all end diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index afd2d8721..cf15ab934 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -264,4 +264,24 @@ RSpec.describe CleanUp, :type => :model do } end end + + describe "#destroy_routes_outside_referential" do + let(:line_referential) { create(:line_referential) } + let(:line) { create(:line, line_referential: line_referential) } + let(:metadata) { create(:referential_metadata, lines: [line]) } + let(:referential) { create(:workbench_referential, metadatas: [metadata]) } + let(:cleaner) { create(:clean_up, referential: referential) } + + it "destroys routes not in the the referential" do + route = create(:route) + + cleaner.destroy_routes_outside_referential + + expect(Chouette::Route.exists?(route.id)).to be false + + line.routes.each do |route| + expect(route).not_to be_destroyed + end + end + end end |
