From 3aa48907a66cc892a17ebe094a6ecf0db953cf79 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 5 Apr 2013 12:45:01 +1100 Subject: [PATCH] Add per-product tests for membership of distributor or order cycle --- app/models/spree/product_decorator.rb | 8 ++++++ spec/models/product_spec.rb | 38 ++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 5a1648d045..71901652de 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -53,6 +53,14 @@ Spree::Product.class_eval do # -- Methods + def in_distributor?(distributor) + Spree::Product.in_distributor(distributor).include? self + end + + def in_order_cycle?(order_cycle) + Spree::Product::in_order_cycle(order_cycle).include? self + end + def shipping_method_for_distributor(distributor) distribution = self.product_distributions.find_by_distributor_id(distributor) raise ArgumentError, "This product is not available through that distributor" unless distribution diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index fd6922b40c..08dd2e2a70 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -21,9 +21,6 @@ module Spree end describe "scopes" do - # Other things to test: - # - use 1.9 hash syntax - describe "in_supplier" do it "shows products in supplier" do s1 = create(:supplier_enterprise) @@ -155,5 +152,40 @@ module Spree end.to raise_error "This product is not available through that distributor" end end + + describe "membership" do + it "queries its membership of a particular product distribution" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p = create(:product, distributors: [d1]) + + p.should be_in_distributor d1 + p.should_not be_in_distributor d2 + end + + it "queries its membership of a particular order cycle distribution" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p1 = create(:product) + p2 = create(:product) + oc1 = create(:order_cycle, :distributors => [d1], :variants => [p1.master]) + oc2 = create(:order_cycle, :distributors => [d2], :variants => [p2.master]) + + p1.should be_in_distributor d1 + p1.should_not be_in_distributor d2 + end + + it "queries its membership of a particular order cycle" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p1 = create(:product) + p2 = create(:product) + oc1 = create(:order_cycle, :distributors => [d1], :variants => [p1.master]) + oc2 = create(:order_cycle, :distributors => [d2], :variants => [p2.master]) + + p1.should be_in_order_cycle oc1 + p1.should_not be_in_order_cycle oc2 + end + end end end