On enterprise page, do not display products outside of the outgoing exchange

This commit is contained in:
Rohan Mitchell
2013-11-15 15:08:45 +11:00
parent 59935619e3
commit 7a8bd16083
4 changed files with 56 additions and 7 deletions

View File

@@ -48,8 +48,8 @@ class EnterprisesController < BaseController
@searcher = Spree::Config.searcher_class.new(options)
@products = @searcher.retrieve_products
order_cycle_products = current_order_cycle.products
@products.select! { |p| order_cycle_products.include? p }
order_cycle_products = current_order_cycle.products_distributed_by(current_distributor)
@products = @products & order_cycle_products
end
end

View File

@@ -97,6 +97,10 @@ class OrderCycle < ActiveRecord::Base
map(&:variants).flatten.uniq
end
def products_distributed_by(distributor)
variants_distributed_by(distributor).map(&:product).uniq
end
def products
self.variants.map(&:product).uniq
end

View File

@@ -10,7 +10,48 @@ describe EnterprisesController do
assigns(:suppliers).should == [s]
end
context "shopping for a distributor" do
describe "displaying an enterprise and its products" do
let(:p) { create(:simple_product, supplier: s) }
let(:s) { create(:supplier_enterprise) }
let!(:c) { create(:distributor_enterprise) }
let(:d1) { create(:distributor_enterprise) }
let(:d2) { create(:distributor_enterprise) }
let(:oc1) { create(:simple_order_cycle) }
let(:oc2) { create(:simple_order_cycle) }
it "displays products for the selected (order_cycle -> outgoing exchange)" do
create(:exchange, order_cycle: oc1, sender: s, receiver: c, variants: [p.master])
create(:exchange, order_cycle: oc1, sender: c, receiver: d1, variants: [p.master])
controller.stub(:current_distributor) { d1 }
controller.stub(:current_order_cycle) { oc1 }
spree_get :show, {id: d1}
assigns(:products).should include p
end
it "does not display other products in the order cycle or in the distributor" do
# Given a product that is in this order cycle on a different distributor
create(:exchange, order_cycle: oc1, sender: s, receiver: c, variants: [p.master])
create(:exchange, order_cycle: oc1, sender: c, receiver: d2, variants: [p.master])
# And is also in this distributor in a different order cycle
create(:exchange, order_cycle: oc2, sender: s, receiver: c, variants: [p.master])
create(:exchange, order_cycle: oc2, sender: c, receiver: d1, variants: [p.master])
# When I view the enterprise page for d1 x oc1
controller.stub(:current_distributor) { d1 }
controller.stub(:current_order_cycle) { oc1 }
spree_get :show, {id: d1}
# Then I should not see the product
assigns(:products).should_not include p
end
end
describe "shopping for a distributor" do
before(:each) do
@current_distributor = create(:distributor_enterprise)
@@ -69,7 +110,7 @@ describe EnterprisesController do
end
end
context "BaseController: handling order cycles expiring mid-order" do
describe "BaseController: handling order cycles expiring mid-order" do
it "clears the order and displays an expiry message" do
oc = double(:order_cycle, id: 123, expired?: true)
controller.stub(:current_order_cycle) { oc }

View File

@@ -172,9 +172,9 @@ describe OrderCycle do
@e2 = create(:exchange,
order_cycle: @oc, sender: @oc.coordinator, receiver: @d2)
@p0 = create(:product)
@p1 = create(:product)
@p2 = create(:product)
@p0 = create(:simple_product)
@p1 = create(:simple_product)
@p2 = create(:simple_product)
@p2_v = create(:variant, product: @p2)
@e0.variants << @p0.master
@@ -196,6 +196,10 @@ describe OrderCycle do
@oc.variants_distributed_by(@d2).should == [@p1.master]
end
it "reports on the products distributed by a particular distributor" do
@oc.products_distributed_by(@d2).should == [@p1]
end
it "reports on the products exchanged" do
@oc.products.sort.should == [@p0, @p1, @p2]
end