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