module Ecom module Core class BookingRequest < ApplicationRecord DRAFT = 'Draft'.freeze UNDER_REVIEW = 'Under Review'.freeze APPROVED = 'Approved'.freeze REJECTED = 'Rejected'.freeze STATUSES = [DRAFT, UNDER_REVIEW, APPROVED, REJECTED].freeze belongs_to :equipment belongs_to :country, optional: true belongs_to :requested_by, class_name: 'Ecom::Core::User' belongs_to :approved_by, class_name: 'Ecom::Core::User', optional: true validates :start_date, :end_date, :quantity, :status, presence: true validates :status, inclusion: STATUSES validates_with DateRangeValidator scope :by_status, ->(status) { where(status: status) } def validate_date_range errors.add(:end_date, 'cannot be before start date') if start_date && end_date && start_date > end_date end end end end