Module: ConcernsOnRails::Models::Expirable

Extended by:
ActiveSupport::Concern
Defined in:
lib/concerns_on_rails/models/expirable.rb

Constant Summary collapse

DEFAULT_FIELD =
:expires_at

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/concerns_on_rails/models/expirable.rb', line 53

def active?
  !expired?
end

#expire!(time = Time.zone.now) ⇒ Object



65
66
67
# File 'lib/concerns_on_rails/models/expirable.rb', line 65

def expire!(time = Time.zone.now)
  update(self.class.expirable_field => time)
end

#expired?Boolean

nil means never expires; equal-to-now is treated as expired (exclusive boundary).

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/concerns_on_rails/models/expirable.rb', line 58

def expired?
  value = self[self.class.expirable_field]
  return false if value.nil?

  value <= Time.zone.now
end

#extend_expiry!(by:) ⇒ Object

Push expiry forward by by:. If the record has no expiry yet, or has already expired, the new expiry is now + by. Otherwise it's added to the existing expiry.



72
73
74
# File 'lib/concerns_on_rails/models/expirable.rb', line 72

def extend_expiry!(by:)
  update(self.class.expirable_field => expiry_extension_base + by)
end

#time_until_expiryObject

Returns an ActiveSupport::Duration of how long until expiry, or nil when there's no expiry set, or 0.seconds when already expired.



78
79
80
81
82
83
84
85
86
# File 'lib/concerns_on_rails/models/expirable.rb', line 78

def time_until_expiry
  value = self[self.class.expirable_field]
  return nil if value.nil?

  now = Time.zone.now
  return 0.seconds if value <= now

  (value - now).seconds
end