Sha256: cd0df191bac1ef8a1ec8fbed0636e641ed255e3db2519f695ccdfcae322f13a1

Contents?: true

Size: 807 Bytes

Versions: 2

Compression:

Stored size: 807 Bytes

Contents

class Outbox < ApplicationRecord
  attribute :allow_publish, :boolean, default: true

  before_save :check_publishing
  # Callbacks
  before_create :set_last_attempted_at
  after_commit :publish, if: :allow_publish?
  # Enums
  enum status: { pending: 0, processing: 1, published: 2, failed: 3 }
  enum size: { single: 0, batch: 1 }

  # Validations
  validates :payload, :exchange, :routing_key, presence: true

  # Associations
  belongs_to :outboxable, polymorphic: true, optional: true

  def set_last_attempted_at
    self.last_attempted_at = 10.seconds.from_now
  end

  def publish
    Outboxable::Worker.perform_async(id)
    update(status: :processing, last_attempted_at: 1.minute.from_now, allow_publish: false)
  end

  def check_publishing
    self.allow_publish = false if published?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
outboxable-1.0.0 lib/templates/activerecrod_outbox.rb
outboxable-0.1.8 lib/templates/outbox.rb