Spree::Taxon.distributed_taxons can be scoped to taxons in open order cycles

This commit is contained in:
Rohan Mitchell
2016-09-16 11:13:24 +10:00
parent db583df198
commit c0db23af90
2 changed files with 16 additions and 6 deletions

View File

@@ -32,6 +32,9 @@ Spree::Taxon.class_eval do
end
# Find all the taxons of distributed products for each enterprise, indexed by enterprise.
# May return :all taxons (distributed in open and closed order cycles),
# or :current taxons (distributed in an open order cycle).
#
# Format: {enterprise_id => [taxon_id, ...]}
def self.distributed_taxons(which_taxons=:all)
# TODO: Why can't we merge(Spree::Product.with_order_cycles_inner) here?
@@ -40,6 +43,8 @@ Spree::Taxon.class_eval do
merge(Exchange.outgoing).
select('spree_taxons.*, exchanges.receiver_id AS enterprise_id')
taxons = taxons.merge(OrderCycle.active) if which_taxons == :current
taxons.inject({}) do |ts, t|
ts[t.enterprise_id.to_i] ||= Set.new
ts[t.enterprise_id.to_i] << t.id

View File

@@ -29,13 +29,18 @@ module Spree
end
end
describe "finding all distributed taxons" do
let!(:oc) { create(:simple_order_cycle, distributors: [e], variants: [p1.master]) }
let!(:s) { create(:supplier_enterprise) }
let!(:p1) { create(:simple_product, supplier: s, taxons: [t1, t2]) }
describe "finding distributed taxons" do
let!(:oc_open) { create(:open_order_cycle, distributors: [e], variants: [p_open.variants.first]) }
let!(:oc_closed) { create(:closed_order_cycle, distributors: [e], variants: [p_closed.variants.first]) }
let!(:p_open) { create(:simple_product, primary_taxon: t1) }
let!(:p_closed) { create(:simple_product, primary_taxon: t2) }
it "finds taxons" do
Taxon.distributed_taxons.should == {e.id => Set.new(p1.taxons.map(&:id))}
it "finds all distributed taxons" do
expect(Taxon.distributed_taxons(:all)).to eq({e.id => Set.new([t1.id, t2.id])})
end
it "finds currently distributed taxons" do
expect(Taxon.distributed_taxons(:current)).to eq({e.id => Set.new([t1.id])})
end
end
end