From 1b63546a9ec72eb332364e4eb09d40ddaffa76ae Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 29 Nov 2013 09:59:39 +1100 Subject: [PATCH] Master variants are valid without unit value or description --- app/models/spree/variant_decorator.rb | 6 ++++-- spec/models/spree/product_spec.rb | 2 +- spec/models/spree/variant_spec.rb | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 10fe4234db..28e422049f 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -1,7 +1,9 @@ Spree::Variant.class_eval do validates_presence_of :unit_value, - if: -> v { %w(weight volume).include? v.product.variant_unit } + if: -> v { %w(weight volume).include? v.product.variant_unit }, + unless: :is_master validates_presence_of :unit_description, - if: -> v { v.product.variant_unit.present? && v.unit_value.nil? } + if: -> v { v.product.variant_unit.present? && v.unit_value.nil? }, + unless: :is_master end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 7fe3c4219a..ab3d7cb0f1 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -29,7 +29,7 @@ module Spree let(:product) do product = create(:simple_product) create(:variant, product: product) - product + product.reload end it "requires a unit" do diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 0baf0b53bc..544bfc5548 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -10,6 +10,7 @@ module Spree context "when the product's unit is #{unit}" do before do product.update_attribute :variant_unit, unit + product.reload end it "is valid when unit value is set and unit description is not" do @@ -22,12 +23,17 @@ module Spree variant.unit_value = nil variant.should_not be_valid end + + it "has a valid master variant" do + product.master.should be_valid + end end end context "when the product's unit is items" do before do product.update_attribute :variant_unit, 'items' + product.reload end it "is valid with only unit value set" do @@ -47,6 +53,10 @@ module Spree variant.unit_description = nil variant.should_not be_valid end + + it "has a valid master variant" do + product.master.should be_valid + end end end