app/models/jobshop/rfq.rb in jobshop-0.0.167 vs app/models/jobshop/rfq.rb in jobshop-0.0.179

- old
+ new

@@ -1,28 +1,46 @@ -# frozen_string_literals: true +# frozen_string_literal: true module Jobshop class RFQ < ApplicationRecord - self.primary_keys = %i[ organization_id rfq_id ] + belongs_to :organization, -> { readonly }, inverse_of: :rfqs - after_initialize { self.rfq_id ||= SecureRandom.uuid if new_record? } - after_create { reload } + belongs_to :customer, -> { readonly }, inverse_of: :rfqs, optional: true - belongs_to :organization, inverse_of: :rfqs - - belongs_to :customer, inverse_of: :rfqs, optional: true, - foreign_key: %i[ organization_id customer_id ] - belongs_to :requested_by, class_name: "Jobshop::Customer::Contact", - primary_key: %i[ organization_id customer_id contact_id ], - foreign_key: %i[ organization_id customer_id requested_by_id ], optional: true - has_many :rfq_lines, inverse_of: :rfq, - foreign_key: %i[ organization_id rfq_id ] + 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.customer_id : 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