Sha256: 06bae0a441b640f2f4f34d40370fe2ed9d508be08fec7badcd7b86472faee5bc

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require 'devise'

module Lcms
  module Engine
    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable, :confirmable,
             :recoverable, :rememberable, :trackable, :validatable

      enum role: { admin: 1, user: 0 }

      validates_presence_of :access_code, on: :create, unless: 'admin?'
      validates_presence_of :email, :role
      validate :access_code_valid?, on: :create, unless: 'admin?'

      def full_name
        [
          survey&.fetch('first_name', nil),
          survey&.fetch('last_name', nil)
        ].reject(&:blank?).join(' ')
      end

      def generate_password
        pwd = Devise.friendly_token.first(20)
        self.password = pwd
        self.password_confirmation = pwd
      end

      def name
        super.presence || full_name
      end

      def ready_to_go?
        admin? || survey.present?
      end

      private

      def access_code_valid?
        return if AccessCode.by_code(access_code).exists?

        errors.add :access_code, 'not found'
      end

      protected

      # NOTE: temporary disable confirmable due to issues with server setup
      def confirmation_required?
        false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lcms-engine-0.1.4 app/models/lcms/engine/user.rb
lcms-engine-0.1.3 app/models/lcms/engine/user.rb
lcms-engine-0.1.2 app/models/lcms/engine/user.rb
lcms-engine-0.1.0 app/models/lcms/engine/user.rb