Sha256: 2ac67c80172283cabc47c47d82c3a0a487505f307c46dd3e2468f418fd26998f

Contents?: true

Size: 1.43 KB

Versions: 11

Compression:

Stored size: 1.43 KB

Contents

module Symphonia
  module UserManagement

    extend ActiveSupport::Concern

    included do

      enum status: { active: 1, pending: 5, archived: 9 }

      scope :sorted, -> { order(:first_name, :last_name) }

      validates :email, presence: true, uniqueness: { case_sensitive: false }
      validates :login, presence: true, uniqueness: { case_sensitive: false }

      before_validation :set_uuid, only: -> { uuid.blank? }
      before_validation :set_login, only: -> { login.blank? }

      def self.current=(entity)
        Rails.logger.info("* #{entity.class.name}: #{entity.login} id: #{entity.id}") if entity
        Thread.current[:"current_#{name.underscore}"] = entity
      end

      def self.current
        Thread.current[:"current_#{name.underscore}"] ||= "#{self}::Anonymous".constantize.new
      end

      def name
        "#{first_name} #{last_name}"
      end

      def name=(string)
        words = string.split
        self.first_name = words[0..-2].join(" ")
        self.last_name = words[-1]
      end

      def status_new?
        pending?
      end

      def lock?
        !active?
      end

      alias_method :archive?, :lock?

      def archive!
        archived!
      end

      def unarchive!
        active!
      end

      alias_method :activate!, :unarchive!

      private

      def set_uuid
        self.uuid ||= SecureRandom.uuid
      end

      def set_login
        self.login ||= email
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
symphonia-6.0.6 lib/symphonia/user_management.rb
symphonia-6.0.5 lib/symphonia/user_management.rb
symphonia-6.0.4 lib/symphonia/user_management.rb
symphonia-6.0.2 lib/symphonia/user_management.rb
symphonia-6.0.1 lib/symphonia/user_management.rb
symphonia-6.0.0 lib/symphonia/user_management.rb
symphonia-5.0.6 lib/symphonia/user_management.rb
symphonia-5.0.5 lib/symphonia/user_management.rb
symphonia-5.0.4 lib/symphonia/user_management.rb
symphonia-5.0.3 lib/symphonia/user_management.rb
symphonia-5.0.0 lib/symphonia/user_management.rb