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 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? 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/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 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)