Master variants are valid without unit value or description

This commit is contained in:
Rohan Mitchell
2013-11-29 09:59:39 +11:00
parent 4c72170742
commit 1b63546a9e
3 changed files with 15 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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