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, if: -> { 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