require 'subj_models/concerns/comprising_external_id' module SubjModels module NomenclaturePriceModule def self.included(including_class) including_class.class_eval do include SubjModels::ComprisingExternalId belongs_to :nomenclature, touch: true belongs_to :nomenclature_variety belongs_to :quality belongs_to :measure_unit belongs_to :event has_many :order_items enum price_type: [:b2b, :b2c] validates :current_price, presence: true after_create :update_nomenclature_availability scope :nomenclature_id, -> (nomenclature_id) { parent_id_scope("nomenclature", nomenclature_id) } scope :belongs_to_nomenclature, -> (nomenclature_id) do where(nomenclature_id: nomenclature_id).union(joins(:nomenclature_variety).where("nomenclature_varieties.nomenclature_id" => nomenclature_id)) end def update_nomenclature_availability if quality && nomenclature quality.is_stock ? nomenclature.is_available_for_stock = true : nomenclature.is_available = true end end end end def to_s id.to_s # TODO end end end