Sha256: 53003bb2489ff935edf5e7e597887efe46b2778491342b23c1c90a1fd59a3d68
Contents?: true
Size: 1.63 KB
Versions: 13
Compression:
Stored size: 1.63 KB
Contents
module Comee module Core class Product < ApplicationRecord belongs_to :product_type belongs_to :unit has_many :inventories validates :code, presence: true, uniqueness: true validates :name, presence: true validate :metadata_must_be_based_on_metadata_schema, if: -> { product_type.present? } delegate(:code, :name, to: :product_type, prefix: true) delegate(:code, :name, to: :unit, prefix: true) def metadata_must_be_based_on_metadata_schema metadata_schema = product_type.metadata_schema return unless metadata_schema.present? && metadata.present? properties = metadata_schema["properties"] fields = properties.keys required = metadata_schema["required"] diff = metadata.keys - fields unless diff.empty? errors.add(:metadata, "holds fields that are not allowed #{diff}.") return end diff = required - metadata.keys unless diff.empty? errors.add(:metadata, "should contain the following required fields #{diff}.") return end metadata.each do |k, v| errors.add(:metadata, "contains invalid value for field #{k}.") if v.class.to_s.downcase != properties[k]["type"] if properties[k].key?("min") && v < properties[k]["min"] errors.add(:base, "The value of #{k} cannot be lower than #{properties[k]['min']}.") end if properties[k].key?("max") && v > properties[k]["max"] errors.add(:base, "The value of #{k} cannot be higher than #{properties[k]['max']}.") end end end end end end
Version data entries
13 entries across 13 versions & 1 rubygems