Sha256: d07df58614a7583a0abd56312dd6a11aa5b7a771c6e27d99038e90cd2edba995

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

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

    included do
      attr_accessor :visibility, :raw_visibility

      before_save :record_visibility!, unless: -> (obj) { obj.raw_visibility.nil? }
      after_initialize :set_visibility!

      scope :visible,   -> { where(invisible_at: nil) }
      scope :invisible, -> { where.not(invisible_at: nil) }
    end

    def invisible!
      update(invisible_at: Time.now)
    end

    def visible!
      update(invisible_at: nil)
    end

    def invisible?
      !visible?
    end

    def visible?
      invisible_at.nil?
    end

    def to_visibility
      I18n.t("active_regulation.visibility.#{visible? ? :visible : :invisible}")
    end

    private

    def record_visibility!
      false_value = FALSE_VALUES.include?(visibility)
      true_value  = TRUE_VALUES.include?(visibility)

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

    def set_visibility!
      self.raw_visibility = visibility
      self.visibility     = visible? if visibility.nil?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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