Working scopes for child variant and master variants

This commit is contained in:
Will Marshall
2013-11-20 14:11:22 +11:00
parent 84346c7fbd
commit ca16314535
2 changed files with 25 additions and 0 deletions

View File

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

View File

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