From a223a2d662a44896b7fa1615dccda1751895bf50 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 2 Oct 2014 18:32:40 +1000 Subject: [PATCH] Cannot remove all variants from a product --- app/models/spree/product_decorator.rb | 1 + spec/models/spree/product_spec.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index bb246ace59..3d35700b7c 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -23,6 +23,7 @@ Spree::Product.class_eval do before_validation :ensure_standard_variant, if: :new_record? + validates_presence_of :variants, message: "Product must have at least one variant" validates_presence_of :supplier validates :primary_taxon, presence: { message: "^Product Category can't be blank" } validates :tax_category_id, presence: { message: "^Tax Category can't be blank" }, if: "Spree::Config.products_require_tax_category" diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 07b67ce872..35dd847d1f 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -8,7 +8,7 @@ module Spree it { should belong_to(:primary_taxon) } it { should have_many(:product_distributions) } end - + describe "validations and defaults" do it "is valid when built from factory" do build(:product).should be_valid @@ -61,6 +61,14 @@ module Spree end + it "does not allow only standard variant to be deleted" do + product = create(:simple_product) + expect(product.variants(:reload).length).to eq 1 + product.variants = [] + expect(product.save).to be_false + expect(product.errors[:variants]).to include "Product must have at least one variant" + end + context "when the product has variants" do let(:product) do product = create(:simple_product)