1
0

expireable.rb 544 B

12345678910111213141516171819202122232425262728
  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