diff options
| -rw-r--r-- | app/models/chouette/route.rb | 4 | ||||
| -rw-r--r-- | app/workers/route_way_cost_worker.rb | 11 | ||||
| -rw-r--r-- | spec/models/chouette/route/route_base_spec.rb | 8 |
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 |
