From ca163145353dcd797c1711bba7faddfdd4e21525 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 20 Nov 2013 14:11:22 +1100 Subject: [PATCH] Working scopes for child variant and master variants --- .../products_and_inventory_report.rb | 9 +++++++++ .../products_and_inventory_report_spec.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/open_food_network/products_and_inventory_report.rb b/lib/open_food_network/products_and_inventory_report.rb index 052a865d13..942d777b3e 100644 --- a/lib/open_food_network/products_and_inventory_report.rb +++ b/lib/open_food_network/products_and_inventory_report.rb @@ -39,5 +39,14 @@ module OpenFoodNetwork .joins(:product) .merge(Spree::Product.managed_by(@user)) end + + def master_variants + Spree::Variant.where(:is_master => true) + .joins(:product) + .where("(select spree_variants.id from spree_variants as other_spree_variants + WHERE other_spree_variants.product_id = spree_variants.product_id + AND other_spree_variants.is_master = 'f' LIMIT 1) IS NULL") + .merge(Spree::Product.managed_by(@user)) + end end end diff --git a/spec/lib/open_food_network/products_and_inventory_report_spec.rb b/spec/lib/open_food_network/products_and_inventory_report_spec.rb index a6accc0722..1403a57916 100644 --- a/spec/lib/open_food_network/products_and_inventory_report_spec.rb +++ b/spec/lib/open_food_network/products_and_inventory_report_spec.rb @@ -73,6 +73,22 @@ module OpenFoodNetwork end end + describe "fetching master variants" do + it "should only return variants managed by the user" do + product1 = create(:simple_product, supplier: create(:supplier_enterprise)) + product2 = create(:simple_product, supplier: supplier) + + subject.master_variants.should == [product2.master] + end + + it "doesn't return master variants with siblings" do + product = create(:simple_product, supplier: supplier) + create(:variant, product: product) + + subject.master_variants.should be_empty + end + end + end it "should fetch variants"