From a0c2facca4bb9bcc00c08d2cd2680f4d86591c65 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 27 Nov 2023 15:44:42 +0100 Subject: [PATCH 1/4] validate price numericality on product and variants --- app/models/spree/product.rb | 1 + app/models/spree/variant.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index a8cb206056..41b8c8f610 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -67,6 +67,7 @@ module Spree validates :variant_unit_name, presence: { if: ->(p) { p.variant_unit == 'items' } } validate :validate_image + validates :price, numericality: { greater_than_or_equal_to: 0, if: ->{ new_record? } } accepts_nested_attributes_for :variants, allow_destroy: true accepts_nested_attributes_for :image diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index ea05767a63..f470f4bb51 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -72,6 +72,7 @@ module Spree } validates :unit_value, numericality: { greater_than: 0 } + validates :price, numericality: { greater_than_or_equal_to: 0 } validates :unit_description, presence: true, if: ->(variant) { variant.product&.variant_unit.present? && variant.unit_value.nil? From a13e087541b76dcd7a66a904d1754a6286ae017d Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 27 Nov 2023 15:49:24 +0100 Subject: [PATCH 2/4] use render edit instead of redirect_to with redirect_to, the validation errors will be lost at the rendering time. --- app/controllers/spree/admin/variants_controller.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/spree/admin/variants_controller.rb b/app/controllers/spree/admin/variants_controller.rb index 6033db7afe..47505f7b1d 100644 --- a/app/controllers/spree/admin/variants_controller.rb +++ b/app/controllers/spree/admin/variants_controller.rb @@ -50,9 +50,8 @@ module Spree flash[:success] = flash_message_for(@object, :successfully_updated) redirect_to spree.admin_product_variants_url(params[:product_id], @url_filters) else - redirect_to spree.edit_admin_product_variant_url(params[:product_id], - @object, - @url_filters) + load_data + render :edit end end From c995f5dfbe5ea2cb13f7026b6b977ad334addef3 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 28 Nov 2023 16:00:36 +0100 Subject: [PATCH 3/4] set the price to 0 on the cloned product (Spree::Code::RroductDuplicator) --- lib/spree/core/product_duplicator.rb | 1 + spec/lib/spree/core/product_duplicator_spec.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/spree/core/product_duplicator.rb b/lib/spree/core/product_duplicator.rb index a99419bc19..82837bf3a6 100644 --- a/lib/spree/core/product_duplicator.rb +++ b/lib/spree/core/product_duplicator.rb @@ -24,6 +24,7 @@ module Spree new_product.created_at = nil new_product.deleted_at = nil new_product.updated_at = nil + new_product.price = 0 new_product.unit_value = %w(weight volume).include?(product.variant_unit) ? 1.0 : nil new_product.product_properties = reset_properties new_product.image = duplicate_image(product.image) if product.image diff --git a/spec/lib/spree/core/product_duplicator_spec.rb b/spec/lib/spree/core/product_duplicator_spec.rb index 08a8d9364e..265f2e1d53 100644 --- a/spec/lib/spree/core/product_duplicator_spec.rb +++ b/spec/lib/spree/core/product_duplicator_spec.rb @@ -71,6 +71,7 @@ describe Spree::Core::ProductDuplicator do expect(new_product).to receive(:sku=).with("") expect(new_product).to receive(:product_properties=).with([new_property]) expect(new_product).to receive(:created_at=).with(nil) + expect(new_product).to receive(:price=).with(0) expect(new_product).to receive(:unit_value=).with(nil) expect(new_product).to receive(:updated_at=).with(nil) expect(new_product).to receive(:deleted_at=).with(nil) From da900321ca454805a3096c8dc5cffa9a49356e54 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 28 Nov 2023 16:03:58 +0100 Subject: [PATCH 4/4] fix product controller tests --- spec/controllers/api/v0/products_controller_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/controllers/api/v0/products_controller_spec.rb b/spec/controllers/api/v0/products_controller_spec.rb index 574a4df07d..c31e323896 100644 --- a/spec/controllers/api/v0/products_controller_spec.rb +++ b/spec/controllers/api/v0/products_controller_spec.rb @@ -102,7 +102,8 @@ describe Api::V0::ProductsController, type: :controller do expect(response.status).to eq(422) expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.") errors = json_response["errors"] - expect(errors.keys).to match_array(["name", "primary_taxon", "supplier", "variant_unit"]) + expect(errors.keys).to match_array(["name", "primary_taxon", "supplier", "variant_unit", + "price"]) end it "can update a product" do