aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-04-16 17:41:09 +0200
committerTeddy Wing2018-04-16 17:46:36 +0200
commit220e839fddec44c9a1e73fb5e7cd7f701faf726d (patch)
tree55a761fa34198ac1217753c798860e2f3d7c55e7
parent85bea8da4f994496b636e2a79dae2f269acbd501 (diff)
downloadchouette-core-220e839fddec44c9a1e73fb5e7cd7f701faf726d.tar.bz2
Route: Don't run `#calculate_costs!` during a referential merge6410-route--don,t-run-calculate_costs-during-referential-mer
This callback got triggered during a referential merge, but it isn't needed at that point. It should only be executed as a result of a user updating the route (via the web interface). Using Alban's suggestion, we can detect that we're in the process of a merge if the associated referential is in a `ReferentialSuite`. Use this as a condition to run the callback. Refs #6410
-rw-r--r--app/models/chouette/route.rb6
-rw-r--r--spec/models/chouette/route/route_base_spec.rb19
2 files changed, 24 insertions, 1 deletions
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb
index c5e909d7e..a5eab3002 100644
--- a/app/models/chouette/route.rb
+++ b/app/models/chouette/route.rb
@@ -80,7 +80,11 @@ module Chouette
validates :wayback, inclusion: { in: self.wayback.values }
after_commit :calculate_costs!,
on: [:create, :update],
- if: ->() { TomTom.enabled? }
+ if: ->() {
+ # Ensure the call back doesn't run during a referential merge
+ !referential.in_referential_suite? &&
+ TomTom.enabled?
+ }
def duplicate opposite=false
overrides = {
diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb
index 43ff28c40..e76f10a13 100644
--- a/spec/models/chouette/route/route_base_spec.rb
+++ b/spec/models/chouette/route/route_base_spec.rb
@@ -77,5 +77,24 @@ RSpec.describe Chouette::Route, :type => :model do
expect(route).not_to receive(:calculate_costs!)
route.save
end
+
+ it "doesn't call #calculate_costs! after_commit if in a ReferentialSuite",
+ truncation: true do
+ begin
+ allow(TomTom).to receive(:enabled?).and_return(true)
+
+ referential_suite = create(:referential_suite)
+ referential = create(:referential, referential_suite: referential_suite)
+
+ referential.switch do
+ route = build(:route)
+
+ expect(route).not_to receive(:calculate_costs!)
+ route.save
+ end
+ ensure
+ referential.destroy
+ end
+ end
end
end