Sha256: 95019608e6e0a12da2e9eb48658482e70be058d4a3114904b4c31aa22814b85b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

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

    included do
      attr_accessor :activation, :raw_activation

      before_save :record_activation!
      after_initialize :set_activation!

      scope :active,   -> { where(inactivated_at: nil) }
      scope :inactive, -> { where.not(inactivated_at: nil) }
    end

    def active!
      update(inactivated_at: nil) if inactive?
    end

    def inactive!
      update(inactivated_at: Time.now) if active?
    end

    def active?
      inactivated_at.nil?
    end

    def inactive?
      !active?
    end

    def to_activation
      I18n.t("active_regulation.activation.#{active? ? :active : :inactive}")
    end

    private

    def record_activation!
      unless raw_activation.nil?
        false_value = FALSE_VALUES.include?(activation)
        true_value  = TRUE_VALUES.include?(activation)

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

    def set_activation!
      self.raw_activation = activation
      self.activation     = active? if activation.nil?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_regulation-2.2.5 lib/active_regulation/activation.rb