Sha256: 192baa9f90b7146d7f12d5cb05b1db90b64da6b928efbafa0900af6e16ba3db5

Contents?: true

Size: 804 Bytes

Versions: 3

Compression:

Stored size: 804 Bytes

Contents

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

  before_save :check_publishing
  # Callbacks
  before_create :set_last_attempted_at
  after_save :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

3 entries across 3 versions & 1 rubygems

Version Path
outboxable-1.0.4 lib/templates/activerecrod_outbox.rb
outboxable-1.0.3 lib/templates/activerecrod_outbox.rb
outboxable-1.0.2 lib/templates/activerecrod_outbox.rb