You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
544 B

  1. # frozen_string_literal: true
  2. module Expireable
  3. extend ActiveSupport::Concern
  4. included do
  5. scope :expired, -> { where.not(expires_at: nil).where('expires_at < ?', Time.now.utc) }
  6. attr_reader :expires_in
  7. def expires_in=(interval)
  8. self.expires_at = interval.to_i.seconds.from_now if interval.present?
  9. @expires_in = interval
  10. end
  11. def expire!
  12. touch(:expires_at)
  13. end
  14. def expired?
  15. expires? && expires_at < Time.now.utc
  16. end
  17. def expires?
  18. !expires_at.nil?
  19. end
  20. end
  21. end