Sha256: 72bea5a30619e52dda4440d4cfbd21c6649924a41c03f8606e33d4c00aede018

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literals: true

module Jobshop # :nodoc:
  class Employee < ApplicationRecord
    self.primary_keys = %i[ organization_id employee_id ]

    after_initialize { self.employee_id ||= SecureRandom.uuid if new_record? }
    after_create { reload }

    belongs_to :organization, -> { readonly }, inverse_of: :employees

    has_many :employments, inverse_of: :employee,
      foreign_key: %i[ organization_id employee_id ],
      dependent: :restrict_with_exception

    validates :email,
      presence:   { if: :email_required? },
      format:     { if: :email_required?, with: /\A[^@\s]+@[^@\s]+\z/ },
      uniqueness: { if: :email_changed?, scope: :organization_id,
                    case_sensitive: false }

    scope :active, -> {
      joins(:employments).merge(Jobshop::Employment.active)
    }

    scope :inactive, -> {
      joins(:employments).merge(Jobshop::Employment.inactive)
    }

    def active?
      !!employments.active.first
    end

    def inactive?
      !employments.active.first
    end

    def name
      @name = [ forename, surname ].join(" ")
    end

    private def email_required?
      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jobshop-0.0.167 app/models/jobshop/employee.rb