Find properties of items sold by a shop

This commit is contained in:
Rohan Mitchell
2016-08-10 15:34:47 +10:00
parent 509564819a
commit 4529ced3f2
2 changed files with 46 additions and 0 deletions

View File

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

View File

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