aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/chouette/route.rb
diff options
context:
space:
mode:
authorTeddy Wing2018-04-06 16:37:33 +0200
committerAlban Peignier2018-04-16 09:09:20 +0200
commitb05676bdda09c10a7130e0f86527a75e6535dad8 (patch)
tree928784b2203a5838cae56ddd7f10cd0422efcfc2 /app/models/chouette/route.rb
parent2346cdedc9f75cc3af36f856d2b732209af8caab (diff)
downloadchouette-core-b05676bdda09c10a7130e0f86527a75e6535dad8.tar.bz2
Route: Change `#calculate_costs!` to `after_commit` callback
Use an `after_commit` instead of an `after_save`. The `after_save` could cause intermittent problems due to Sidekiq starting before the record/transaction actually gets committed to the database. In those cases, the record wouldn't be found and cause an error. With an `after_commit` callback, the record is sure to be committed before Sidekiq takes over. Additionally, limit the callback to `:create` and `:update` because otherwise it will be active on `:destroy` by default also. This work isn't relevant on destroy, so don't do it then. Update the tests: * Update labels * Use `build` instead of `create` because we're saving the record at the end anyway so we don't need an object that starts out persisted * Use `truncation: true` for these tests because otherwise the `commit` callback doesn't get called due to transactional teardown Refs #6407
Diffstat (limited to 'app/models/chouette/route.rb')
-rw-r--r--app/models/chouette/route.rb4
1 files changed, 3 insertions, 1 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 = {