2021-11-18 22:05:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module TtlExpirable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
validates :status, presence: true
|
2021-12-11 22:18:48 +05:30
|
|
|
default_value_for :read_at, Time.zone.now
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
enum status: { default: 0, expired: 1, processing: 2, error: 3 }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
scope :read_before, ->(number_of_days) { where("read_at <= ?", Time.zone.now - number_of_days.days) }
|
2021-11-18 22:05:49 +05:30
|
|
|
scope :active, -> { where(status: :default) }
|
|
|
|
|
|
|
|
scope :lock_next_by, ->(sort) do
|
|
|
|
order(sort)
|
|
|
|
.limit(1)
|
|
|
|
.lock('FOR UPDATE SKIP LOCKED')
|
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
def read!
|
|
|
|
self.update(read_at: Time.zone.now)
|
|
|
|
end
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|