From 6793ac10c5df53c4dfe08a886eb82ccdf5cf59bf Mon Sep 17 00:00:00 2001 From: Brusk Awat Date: Wed, 5 Apr 2023 23:51:15 +0300 Subject: [PATCH] Sets last_attempted_at at creation time to avoid null --- lib/templates/outbox.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/templates/outbox.rb b/lib/templates/outbox.rb index af428ca..10e4583 100644 --- a/lib/templates/outbox.rb +++ b/lib/templates/outbox.rb @@ -2,9 +2,9 @@ class Outbox < ApplicationRecord attribute :allow_publish, :boolean, default: true # Callbacks - after_commit :publish, if: :allow_publish? + before_create :set_last_attempted_at before_save :check_publishing - + after_commit :publish, if: :allow_publish? # Enums enum status: { pending: 0, published: 1, failed: 2 } enum size: { single: 0, batch: 1 } @@ -17,8 +17,7 @@ class Outbox < ApplicationRecord # Associations belongs_to :outboxable, polymorphic: true, optional: true - def increment_attempt - self.attempts = attempts + 1 + def set_last_attempted_at self.last_attempted_at = Time.zone.now end