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.

24 lines
503 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_at.nil? && expires_at < Time.now.utc
  16. end
  17. end
  18. end