mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-04 07:09:14 +00:00
update product's unit_value validation
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user