From 4529ced3f2a75e0111469d33da7f19aca93df76f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 10 Aug 2016 15:34:47 +1000 Subject: [PATCH] Find properties of items sold by a shop --- app/models/spree/property_decorator.rb | 7 +++++ spec/models/spree/property_spec.rb | 39 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/app/models/spree/property_decorator.rb b/app/models/spree/property_decorator.rb index f6eaefc75f..ede97bcc2d 100644 --- a/app/models/spree/property_decorator.rb +++ b/app/models/spree/property_decorator.rb @@ -6,6 +6,13 @@ module Spree where('spree_product_properties.product_id IN (?)', enterprise.supplied_product_ids) } + scope :sold_by, ->(shop) { + joins(products: {variants: {exchanges: :order_cycle}}). + merge(Exchange.outgoing). + merge(Exchange.to_enterprise(shop)). + merge(OrderCycle.active) + } + after_save :refresh_products_cache # When a Property is destroyed, dependent-destroy will destroy all ProductProperties, diff --git a/spec/models/spree/property_spec.rb b/spec/models/spree/property_spec.rb index 6867b9de1f..f291c4a7af 100644 --- a/spec/models/spree/property_spec.rb +++ b/spec/models/spree/property_spec.rb @@ -30,6 +30,45 @@ module Spree expect(Spree::Property.applied_by(producer).to_a.count).to eq 1 end end + + describe ".sold_by" do + let!(:shop) { create(:distributor_enterprise) } + let!(:shop_other) { create(:distributor_enterprise) } + let!(:product) { create(:simple_product) } + let!(:product_other_ex) { create(:simple_product) } + let!(:product_no_oc) { create(:simple_product) } + let!(:product_closed_oc) { create(:simple_product) } + let!(:oc) { create(:simple_order_cycle, distributors: [shop], variants: [product.variants.first]) } + let!(:oc_closed) { create(:closed_order_cycle, distributors: [shop], variants: [product_closed_oc.variants.first]) } + let!(:exchange_other_shop) { create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: shop_other, variants: [product_other_ex.variants.first]) } + let(:property) { product.properties.last } + let(:property_other_ex) { product_other_ex.properties.last } + let(:property_no_oc) { product_no_oc.properties.last } + let(:property_closed_oc) { product_closed_oc.properties.last } + + before do + product.set_property 'Organic', 'NASAA 12345' + product_other_ex.set_property 'Biodynamic', 'ASDF 12345' + product_no_oc.set_property 'Shiny', 'Very' + product_closed_oc.set_property 'Spiffy', 'Ooh yeah' + end + + it "returns the property" do + expect(Property.sold_by(shop)).to eq [property] + end + + it "doesn't return the property from another exchange" do + expect(Property.sold_by(shop)).not_to include property_other_ex + end + + it "doesn't return the property with no order cycle" do + expect(Property.sold_by(shop)).not_to include property_no_oc + end + + it "doesn't return the property from a closed order cycle" do + expect(Property.sold_by(shop)).not_to include property_closed_oc + end + end end describe "callbacks" do