From cb899575b73367befda72e6f941b0e633b4794c2 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 18 Jun 2012 19:53:53 +1000 Subject: [PATCH] Product requires at least one distributor --- app/models/spree/product_decorator.rb | 2 ++ spec/models/product_spec.rb | 12 ++++++++++++ spec/support/blueprints.rb | 1 + 3 files changed, 15 insertions(+) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 3626d15630..04c0735ec4 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -3,4 +3,6 @@ Spree::Product.class_eval do has_and_belongs_to_many :distributors attr_accessible :supplier_id + + validates_presence_of :distributors end diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 712942a375..c4fa936e32 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -7,4 +7,16 @@ describe Spree::Product do it { should have_and_belong_to_many(:distributors) } end + describe "validations" do + it "is valid when created from factory" do + Spree::Product.make.should be_valid + end + + it "requires at least one distributor" do + product = Spree::Product.make + product.distributors.clear + product.should_not be_valid + end + end + end diff --git a/spec/support/blueprints.rb b/spec/support/blueprints.rb index 44554de459..279df8338d 100644 --- a/spec/support/blueprints.rb +++ b/spec/support/blueprints.rb @@ -35,6 +35,7 @@ Spree::Product.blueprint do available_on{ Date.today - 2.days } count_on_hand { 5 } price { 10.99 } + distributors{[Spree::Distributor.first || Spree::Distributor.make]} end Spree::Variant.blueprint do