Sha256: 033e19c5abed5dd9404f0bbb9d10a502eb1054e35a98b2776004a8e22ec3247e

Contents?: true

Size: 935 Bytes

Versions: 3

Compression:

Stored size: 935 Bytes

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: (amount.nil? ? 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 to_expiration
      I18n.t("active_regulation.expiration.#{expired? ? :expired : :unexpired}")
    end

    private

    def extension_date(days=30)
      DateTime.now + days
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_regulation-2.2.1 lib/active_regulation/expiration.rb
active_regulation-2.2.0 lib/active_regulation/expiration.rb
active_regulation-2.0.0 lib/active_regulation/expiration.rb