From b082475c3537f310cbe7092ff9fde11ec1f6b139 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 21 Aug 2023 15:29:27 +0100 Subject: [PATCH] update product's unit_value validation --- app/models/spree/product.rb | 22 ++++------------------ spec/system/admin/products_spec.rb | 2 +- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index 76284f532c..71e6ae5dea 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -53,7 +53,10 @@ module Spree validates :name, presence: true validates :variant_unit, presence: true - validate :validate_unit_value + validates :unit_value, numericality: { + greater_than: 0, + if: ->(p) { p.variant_unit.in?(%w(weight volume)) && new_record? } + } validates :variant_unit_scale, presence: { if: ->(p) { %w(weight volume).include? p.variant_unit } } validates :variant_unit_name, @@ -206,23 +209,6 @@ module Spree }.inject(:or) end - def validate_unit_value - return unless %w(weight volume).include?(variant_unit) && new_record? - - return errors.add(:unit_value, I18n.t('errors.messages.blank')) if unit_value.blank? - - value = Float(unit_value, exception: false) - - return if value.is_a?(Numeric) && value > 0 - - error = if value.nil? - I18n.t('errors.messages.not_a_number') - else - I18n.t('errors.messages.greater_than', count: 0) - end - errors.add(:unit_value, error) - end - def property(property_name) return nil unless prop = properties.find_by(name: property_name) diff --git a/spec/system/admin/products_spec.rb b/spec/system/admin/products_spec.rb index fe1716596a..a6c2183b6e 100644 --- a/spec/system/admin/products_spec.rb +++ b/spec/system/admin/products_spec.rb @@ -199,7 +199,7 @@ describe ' click_button 'Create' expect(current_path).to eq spree.admin_products_path - expect(page).to have_content "Unit value can't be blank" + expect(page).to have_content "Unit value is not a number" end end