Sha256: 84110f9d24736bf381f90a7d9b53db21d4509bb77b7754bc4789abba347bbb0b

Contents?: true

Size: 881 Bytes

Versions: 17

Compression:

Stored size: 881 Bytes

Contents

module Ecom
  module Core
    class CrewIdCard < ApplicationRecord
      before_save :invalidate_other_ids

      VALID = 'Valid'.freeze
      INVALID = 'Invalid'.freeze

      STATUSES = [VALID, INVALID].freeze

      validates :crew_id, :crew, :issued_on, :valid_until,
                :status, presence: true

      validates :status, inclusion: STATUSES
      validate :valid_until_validator

      belongs_to :crew

      def valid_until_validator
        return unless valid_until && issued_on

        errors.add(:valid_until, 'cannot be before issue date.') if issued_on >= valid_until
      end

      def invalidate_other_ids
        return if crew_id.nil?

        return if status_changed?(from: 'Valid', to: 'Invalid')

        Ecom::Core::CrewIdCard
          .where(crew_id: crew_id, status: 'Valid')
          .update(status: 'Invalid')
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ecom_core-1.3.14 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.13 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.12 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.11 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.10 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.9 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.8 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.7 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.6 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.5 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.4 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.3 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.2 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.1 app/models/ecom/core/crew_id_card.rb
ecom_core-1.3.0 app/models/ecom/core/crew_id_card.rb
ecom_core-1.2.36 app/models/ecom/core/crew_id_card.rb
ecom_core-1.2.35 app/models/ecom/core/crew_id_card.rb