From 7164cbd0bd2dc3141c50260bb17126553568f76f Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 15 Feb 2024 11:26:04 +1100 Subject: [PATCH] Don't validate numericality if blank This avoids unnecessary second message when left blank: > can't be blank > is not a number Ok this is a little confusing. Why is there a separate presence check above, and why is it only for measurable units, when we still require a number for _all_ units? Because, for 'items', we allow a blank value then auto-set it to 1. I don't know if it's really necessary, but that's how it currently works... --- app/models/spree/variant.rb | 2 +- spec/system/admin/bulk_product_update_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index 1d8dda53ff..d91498e310 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -69,7 +69,7 @@ module Spree %w(weight volume).include?(variant.product&.variant_unit) } - validates :unit_value, numericality: { greater_than: 0 } + validates :unit_value, numericality: { greater_than: 0 }, allow_blank: true validates :price, numericality: { greater_than_or_equal_to: 0 } validates :unit_description, presence: true, if: ->(variant) { diff --git a/spec/system/admin/bulk_product_update_spec.rb b/spec/system/admin/bulk_product_update_spec.rb index 199c8e218f..a15d7621b4 100644 --- a/spec/system/admin/bulk_product_update_spec.rb +++ b/spec/system/admin/bulk_product_update_spec.rb @@ -307,7 +307,7 @@ describe ' click_button 'Save Changes', match: :first expect(page.find("#status-message")) - .to have_content "Variant unit value can't be blank Variant unit value is not a number" + .to have_content "Variant unit value can't be blank" end it "creating a variant with unit value is: '120g' and 'on_demand' checked" do @@ -323,7 +323,7 @@ describe ' click_button 'Save Changes', match: :first expect(page.find("#status-message")) - .to have_content "Variant unit value can't be blank Variant unit value is not a number" + .to have_content "Variant unit value can't be blank" end end end