Use inner join for Spree::Taxon.distributed_taxons

This commit is contained in:
Rohan Mitchell
2016-09-16 10:50:59 +10:00
parent 306bd25dd9
commit db583df198

View File

@@ -33,20 +33,18 @@ Spree::Taxon.class_eval do
# Find all the taxons of distributed products for each enterprise, indexed by enterprise.
# Format: {enterprise_id => [taxon_id, ...]}
def self.distributed_taxons
taxons = {}
def self.distributed_taxons(which_taxons=:all)
# TODO: Why can't we merge(Spree::Product.with_order_cycles_inner) here?
taxons = Spree::Taxon.
joins(products: {variants_including_master: {exchanges: :order_cycle}}).
merge(Exchange.outgoing).
select('spree_taxons.*, exchanges.receiver_id AS enterprise_id')
Spree::Taxon.
joins(:products).
merge(Spree::Product.with_order_cycles_outer).
where('o_exchanges.incoming = ?', false).
select('spree_taxons.*, o_exchanges.receiver_id AS enterprise_id').
each do |t|
taxons[t.enterprise_id.to_i] ||= Set.new
taxons[t.enterprise_id.to_i] << t.id
end
taxons
taxons.inject({}) do |ts, t|
ts[t.enterprise_id.to_i] ||= Set.new
ts[t.enterprise_id.to_i] << t.id
ts
end
end