lib/active_regulation/containment.rb in active_regulation-2.2.1 vs lib/active_regulation/containment.rb in active_regulation-2.2.2
- old
+ new
@@ -1,10 +1,18 @@
module ActiveRegulation
module Containment
extend ActiveSupport::Concern
included do
+ attr_accessor :containment
+
+ validates :containment, inclusion: { in: 0..1 },
+ allow_blank: true,
+ allow_nil: true
+
+ before_save :record_containment!
+
scope :contained, -> { where.not(contained_at: nil) }
scope :uncontained, -> { where(contained_at: nil) }
end
def contain!
@@ -23,9 +31,15 @@
contained_at.nil?
end
def to_containment
I18n.t("active_regulation.containment.#{uncontained? ? :uncontained : :contained}")
+ end
+
+ private
+
+ def record_containment!
+ self.contained_at = (containment.zero? ? nil : Time.now) unless containment.blank?
end
end
end
\ No newline at end of file