# frozen_string_literal: true module Jobshop class RFQ < ApplicationRecord belongs_to :organization, -> { readonly }, inverse_of: :rfqs belongs_to :customer, -> { readonly }, inverse_of: :rfqs, optional: true belongs_to :requested_by, class_name: "Jobshop::Customer::Contact", optional: true has_many :lines, inverse_of: :rfq scope :undelegated, -> { left_outer_joins(lines: :assignments).merge(Jobshop::RFQ::Line.unassigned).distinct } scope :verified, -> { where.not(customer_id: nil, requested_by_id: :nil) } scope :verifiable, -> { where.not(customer_id: nil) .where(requested_by_id: :nil) } scope :unverifiable, -> { where(customer_id: nil, requested_by_id: nil) } accepts_nested_attributes_for :lines, reject_if: proc { |attributes| attributes["description"].blank? && attributes["identifier"].blank? }, allow_destroy: true def customer=(value) customer_id = (Jobshop::Customer === value) ? value.id : value write_attribute(:customer_id, customer_id) end def verified? customer.present? && requested_by.present? end def verifiable? customer.present? && requested_by.nil? end def unverifiable? customer.nil? && requested_by.nil? end end end