Sha256: 1e7136c6729760e335395c6de280407b1c15b919565a7777cb479b8dd1bf5b7f

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

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

    included do
      attr_accessor :containment, :raw_containment

      before_save :record_containment!
      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!
      unless raw_containment.nil?
        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
    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.5 lib/active_regulation/containment.rb