aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlban Peignier2018-04-16 09:23:25 +0200
committerGitHub2018-04-16 09:23:25 +0200
commit9e5577115003e84b501f4879ba4dcdac3167acb9 (patch)
tree689a8fcb64cf3a418e2ddee83ea5b03b43472db0
parent2346cdedc9f75cc3af36f856d2b732209af8caab (diff)
parent0bfb394fea60f2f7a3680eb8768118cd99758d39 (diff)
downloadchouette-core-9e5577115003e84b501f4879ba4dcdac3167acb9.tar.bz2
Merge pull request #459 from af83/6407-route--calculate-way-costs-on-after_commit-instead-of-a
Route: Change `#calculate_costs!` to `after_commit` callback. Refs #6407
-rw-r--r--app/models/chouette/route.rb4
-rw-r--r--app/workers/route_way_cost_worker.rb11
-rw-r--r--spec/models/chouette/route/route_base_spec.rb8
3 files changed, 13 insertions, 10 deletions
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb
index 14bfa47b6..9c722851f 100644
--- a/app/models/chouette/route.rb
+++ b/app/models/chouette/route.rb
@@ -77,7 +77,9 @@ module Chouette
validates_presence_of :published_name
validates_presence_of :line
validates :wayback, inclusion: { in: self.wayback.values }
- after_save :calculate_costs!, if: ->() { TomTom.enabled? }
+ after_commit :calculate_costs!,
+ on: [:create, :update],
+ if: ->() { TomTom.enabled? }
def duplicate opposite=false
overrides = {
diff --git a/app/workers/route_way_cost_worker.rb b/app/workers/route_way_cost_worker.rb
index d6bfed592..b62416c3d 100644
--- a/app/workers/route_way_cost_worker.rb
+++ b/app/workers/route_way_cost_worker.rb
@@ -7,10 +7,11 @@ class RouteWayCostWorker
# Prevent recursive worker spawning since this call updates the
# `costs` field of the route.
- Chouette::Route.skip_callback(:save, :after, :calculate_costs!)
-
- RouteWayCostCalculator.new(route).calculate!
-
- Chouette::Route.set_callback(:save, :after, :calculate_costs!)
+ begin
+ Chouette::Route.skip_callback(:commit, :after, :calculate_costs!)
+ RouteWayCostCalculator.new(route).calculate!
+ ensure
+ Chouette::Route.set_callback(:commit, :after, :calculate_costs!)
+ end
end
end
diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb
index 3d4a87791..43ff28c40 100644
--- a/spec/models/chouette/route/route_base_spec.rb
+++ b/spec/models/chouette/route/route_base_spec.rb
@@ -62,17 +62,17 @@ RSpec.describe Chouette::Route, :type => :model do
end
context "callbacks" do
- it "calls #calculate_costs! after_save when TomTom is enabled" do
+ it "calls #calculate_costs! after_commit when TomTom is enabled", truncation: true do
allow(TomTom).to receive(:enabled?).and_return(true)
- route = create(:route)
+ route = build(:route)
expect(route).to receive(:calculate_costs!)
route.save
end
- it "doesn't call #calculate_costs! after_save if TomTom is disabled" do
+ it "doesn't call #calculate_costs! after_commit if TomTom is disabled", truncation: true do
allow(TomTom).to receive(:enabled?).and_return(false)
- route = create(:route)
+ route = build(:route)
expect(route).not_to receive(:calculate_costs!)
route.save