lib/active_regulation/expiration.rb in active_regulation-2.2.5 vs lib/active_regulation/expiration.rb in active_regulation-2.2.6
- old
+ new
@@ -1,28 +1,22 @@
require 'date'
module ActiveRegulation
module Expiration
extend ActiveSupport::Concern
- include ActiveRegulation::Base
included do
- attr_accessor :expiration, :raw_expiration
-
- before_save :record_expiration!
- after_initialize :set_expiration!
-
scope :expired, -> { where("expires_at IS NULL OR expires_at < ?", Time.now) }
scope :unexpired, -> { where("expires_at IS NOT NULL AND expires_at >= ?", Time.now) }
end
def expire!
update(expires_at: nil) unless expires_at.nil?
end
def extend!(amount=nil)
- update(expires_at: (amount.nil? ? extension_date : amount))
+ update(expires_at: extension_date(amount))
end
def unexpire!
update(expires_at: extension_date) if expires_at.nil?
end
@@ -33,36 +27,23 @@
def unexpired?
expires_at.nil? ? false : (Time.now < expires_at)
end
+ def expires_at_or_time(amount=nil)
+ expired? ? extension_date(amount) : expires_at
+ end
+
def to_expiration
I18n.t("active_regulation.expiration.#{expired? ? :expired : :unexpired}")
end
private
- def extension_date(days=30)
- DateTime.now + days
- end
+ def extension_date(time=nil)
+ time = 30 if time.nil?
- def record_expiration!
- unless raw_expiration.nil?
- false_value = FALSE_VALUES.include?(expiration)
- true_value = TRUE_VALUES.include?(expiration)
-
- if false_value || true_value
- self.expires_at = (false_value ? extension_date : nil)
- else
- raise ArgumentError,
- "Unknown boolean: #{expiration.inspect}. Must be a valid boolean."
- end
- end
- end
-
- def set_expiration!
- self.raw_expiration = expiration
- self.expiration = expired? if expiration.nil?
+ time.is_a?(Integer) ? (DateTime.now + time) : time
end
end
end
\ No newline at end of file