expireable.rb 696 B

123456789101112131415161718192021222324252627282930313233343536
  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. def expires_in
  7. return @expires_in if defined?(@expires_in)
  8. if expires_at.nil?
  9. nil
  10. else
  11. (expires_at - created_at).to_i
  12. end
  13. end
  14. def expires_in=(interval)
  15. self.expires_at = interval.present? ? interval.to_i.seconds.from_now : nil
  16. @expires_in = interval
  17. end
  18. def expire!
  19. touch(:expires_at)
  20. end
  21. def expired?
  22. expires? && expires_at < Time.now.utc
  23. end
  24. def expires?
  25. !expires_at.nil?
  26. end
  27. end
  28. end