Sha256: 7f4b8f56b96719af15d75fc85e88de024cf0db7993e3b8e4d86454f9bd6840dd

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module Ecom
  module Core
    class SiteCrew < ApplicationRecord
      TEMPORARY_SITE_CREW = 'Temporary site crew'.freeze
      PERMANENT_SITE_CREW = 'Permanent site crew'.freeze

      SITE_CREW_TYPES = [TEMPORARY_SITE_CREW, PERMANENT_SITE_CREW].freeze

      ACTIVE = 'Active'.freeze
      INACTIVE = 'Inactive'.freeze
      STATUSES = [ACTIVE, INACTIVE].freeze

      validates :crew_id, :crew, :site_id, :site, :start_date, :site_crew_type, :status, presence: true

      validates_uniqueness_of :site_id,
                              scope: %i[crew_id status],
                              if: :active_site_crew_exists?,
                              message: 'There can only be one record with status `Active` for a given site and crew'

      validates :site_crew_type, inclusion: SITE_CREW_TYPES
      validates :status, inclusion: STATUSES

      belongs_to :crew
      belongs_to :site

      def active_site_crew_exists?
        active_site_crew = Ecom::Core::SiteCrew.where(site_id: site_id, crew_id: crew_id, status: ACTIVE)

        return true if !active_site_crew.count.zero? && status == ACTIVE

        false
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecom_core-1.3.14 app/models/ecom/core/site_crew.rb
ecom_core-1.3.13 app/models/ecom/core/site_crew.rb
ecom_core-1.3.12 app/models/ecom/core/site_crew.rb