Sha256: d348be1854c0de003eac81a3006bea17961196a0d221e59942f6694e4ce5363a

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module ActiveRegulation
  module Containment
    extend ActiveSupport::Concern
    include ActiveRegulation::Base

    included do
      attr_accessor :containment, :raw_containment

      before_save :record_containment!, unless: -> (obj) { obj.raw_containment.nil? }
      after_initialize :set_containment!

      scope :contained,   -> { where.not(contained_at: nil) }
      scope :uncontained, -> { where(contained_at: nil) }
    end

    def contain!
      update(contained_at: Time.now) if uncontained?
    end

    def uncontain!
      update(contained_at: nil) if contained?
    end

    def contained?
      !uncontained?
    end

    def uncontained?
      contained_at.nil?
    end

    def to_containment
      I18n.t("active_regulation.containment.#{uncontained? ? :uncontained : :contained}")
    end

    private

    def record_containment!
      false_value = FALSE_VALUES.include?(containment)
      true_value  = TRUE_VALUES.include?(containment)

      if false_value || true_value
        self.contained_at = (false_value ? nil : Time.now)
      else
        raise ArgumentError,
          "Unknown boolean: #{containment.inspect}. Must be a valid boolean."
      end
    end

    def set_containment!
      self.raw_containment = containment
      self.containment     = contained? if containment.nil?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_regulation-2.2.4 lib/active_regulation/containment.rb