update product's unit_value validation

This commit is contained in:
Mohamed ABDELLANI
2023-08-21 15:29:27 +01:00
parent a896d414c2
commit b082475c35
2 changed files with 5 additions and 19 deletions

View File

@@ -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)

View File

@@ -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