module Ecom module Core class MeasurementUnit < ApplicationRecord METRIC = 'Metric'.freeze IMPERIAL = 'Imperial'.freeze SYSTEM_OF_MEASURMENT = [METRIC, IMPERIAL].freeze # commonly used physical quantities in construction LENGTH = 'Length'.freeze MASS = 'Mass'.freeze TIME = 'Time'.freeze CURRENT = 'Current'.freeze TEMPRATURE = 'Temprature'.freeze AREA = 'Area'.freeze VOLUME = 'Volume'.freeze ENERGY = 'Energy'.freeze DENSITY = 'DENSITY'.freeze PHYSICAL_QUANTITIES = [LENGTH, MASS, TIME, CURRENT, TEMPRATURE, AREA, VOLUME, ENERGY, DENSITY].freeze validates :name, :abbrivation, :physical_quantity, :conversion_factor, :system_of_measurement, presence: true validates :is_si_unit, inclusion: { in: [true, false] } validates :system_of_measurement, inclusion: SYSTEM_OF_MEASURMENT validates :physical_quantity, inclusion: PHYSICAL_QUANTITIES validate :si_unit_with_conversion_factor validates_uniqueness_of :is_si_unit, scope: %i[physical_quantity system_of_measurement], if: -> { is_si_unit == true }, message: 'There exist an si unit of measurement for the '\ 'given physical quantity and system of measurement' def si_unit_with_conversion_factor if is_si_unit && conversion_factor != 1 errors.add(:base, 'Conversion factor for an SI unit has to be 1') elsif !is_si_unit && conversion_factor == 1 errors.add(:base, 'Only SI units can have a conversion factor 1') end end end end end