Sha256: d2109702febe1978922e32f79bc070dfe08dc1c1f5e6e7dd0208cf03a1bcf467

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Ecom
  module Core
    class CrewContract < ApplicationRecord
      include AASM

      belongs_to :crew
      belongs_to :crew_type

      validates :status, :contract_no, :from, :to, :place_of_work, :probation_period,
                :contract_type, :wage, :wage_in_words, :crew_type_id, :crew_type, presence: true
      validates :contract_no, uniqueness: true
      validate :validate_date_range

      validates :wage, presence: true, numericality: { greater_than: 0 }

      validates_uniqueness_of :crew_id,
                              conditions: -> { where(status: :in_effect) },
                              message: 'There can only be one contract' \
                               ' with status `In effect` for a given crew at a time'

      def validate_date_range
        return unless from && to

        errors.add(:to, 'cannot be before from date.') if from >= to
      end

      aasm column: 'status' do
        state :draft, initial: true
        state :submitted_for_approval
        state :approved
        state :rejected
        state :in_effect
        state :void

        event :submit_for_approval do
          transitions from: :draft, to: :submitted_for_approval, guard: -> { status == 'draft' }
        end

        event :approve do
          transitions from: :submitted_for_approval, to: :approved, guard: -> { status == 'submitted_for_approval' }
        end

        event :reject do
          transitions from: :submitted_for_approval, to: :rejected, guard: -> { status == 'submitted_for_approval' }
        end

        event :commence do
          transitions from: :approved, to: :in_effect, guard: -> { status == 'approved' }
        end

        event :terminate do
          transitions from: :in_effect, to: :void, guard: -> { status == 'in_effect' }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecom_core-1.2.35 app/models/ecom/core/crew_contract.rb