Sha256: 3060b3fa68fd43802b2cdb09b88e94c1ef4fe757a414d8bd482c7e8fc52a8572

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

require 'date'

module ActiveRegulation
  module Expiration
    extend ActiveSupport::Concern

    included do
      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: extension_date(amount))
    end

    def unexpire!
      update(expires_at: extension_date) if expires_at.nil?
    end

    def expired?
      expires_at.nil? ? true : (Time.now >= expires_at)
    end

    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(time = nil)
      time = 30 if time.nil?

      time.is_a?(Integer) ? (DateTime.now + time) : time
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_regulation-4.0.3 lib/active_regulation/expiration.rb
active_regulation-4.0.2 lib/active_regulation/expiration.rb
active_regulation-4.0.1 lib/active_regulation/expiration.rb
active_regulation-4.0.0 lib/active_regulation/expiration.rb