Improves mongoid model

This commit is contained in:
Brusk Awat 2023-04-14 02:39:02 +03:00
parent bab6309502
commit e4c2638261
Signed by: broosk1993
GPG Key ID: 5D20F7E02649F74E

View File

@ -1,13 +1,12 @@
class Outbox
include Mongoid::Document
include Mongoid::Timestamps
include SimpleEnum::Mongoid
attr_writer :allow_publish
# Fields
field :status, type: String, default: 'pending'
field :size, type: String, default: 'single'
field :size, type: String, default: 'single'
field :exchange, type: String, default: ''
field :routing_key, type: String, default: ''
@ -32,10 +31,6 @@ class Outbox
before_create :set_last_attempted_at
after_save :publish, if: :allow_publish
# Enums
as_enum :status, { pending: 0, processing: 1, published: 2, failed: 3 }, pluralize_scopes: false, map: :string
as_enum :size, { single: 0, batch: 1 }, pluralize_scopes: false, map: :string
# Validations
validates :payload, :exchange, :routing_key, presence: true
@ -60,6 +55,17 @@ class Outbox
end
def allow_publish
@allow_publish || true
return true if @allow_publish.nil?
@allow_publish
end
%w[pending processing published failed].each do |status|
define_method "#{status}?" do
self.status == status
end
# define scope
scope status, -> { where(status:) }
end
end