Sha256: 5449ab19f53bcb9462ae4c13c2eceac28643d5b21d6f5c36c11690abd60ba96d

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

module Challah
  module UserStatusable
    extend ActiveSupport::Concern

    included do
      begin
        if columns.map(&:name).include?("status")
          additional_statuses = Array(Challah.options[:additional_statuses])
          enum status: [:active, :inactive, *additional_statuses]
        end
      rescue ActiveRecord::StatementInvalid => exception
        raise exception unless exception.message =~ /could not find table/i ||
                               exception.message =~ /does not exist/i
      end
    end

    # Fallback to pre-enum active column (pre challah 1.4)
    def active=(enabled)
      if self.class.columns.map(&:name).include?("status")
        self.status = (!!enabled ? :active : :inactive)
      else
        write_attribute(:active, !!enabled)
      end
    end

    def active?
      # enum-based status
      if self.class.columns.map(&:name).include?("status")
        read_attribute(:status).to_s == "active"

      # support for non-enum status column (pre challah 1.4)
      else
        !!read_attribute(:active)
      end
    end

    def active
      active?
    end

    def valid_session?
      active?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
challah-1.6.1 lib/challah/concerns/user/statusable.rb
challah-1.6.0 lib/challah/concerns/user/statusable.rb
challah-1.5.0 lib/challah/concerns/user/statusable.rb