Skip to content

Commit

Permalink
Enterprise distributing_products scope uses INNER JOINS instead of OU…
Browse files Browse the repository at this point in the history
…TER JOINS
  • Loading branch information
oeoeaio committed Jan 17, 2017
1 parent 20d01c3 commit 017916b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions app/models/enterprise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,18 @@ class Enterprise < ActiveRecord::Base
}

scope :distributing_products, lambda { |products|
with_distributed_products_outer.with_order_cycles_and_exchange_variants_outer.
where('product_distributions.product_id IN (?) OR spree_variants.product_id IN (?)', products, products).
select('DISTINCT enterprises.*')
# TODO: remove this when we pull out product distributions
pds = joins("INNER JOIN product_distributions ON product_distributions.distributor_id = enterprises.id").
where("product_distributions.product_id IN (?)", products).select('DISTINCT enterprises.id')

exs = joins("INNER JOIN exchanges ON (exchanges.receiver_id = enterprises.id AND exchanges.incoming = 'f')").
joins('INNER JOIN exchange_variants ON (exchange_variants.exchange_id = exchanges.id)').
joins('INNER JOIN spree_variants ON (spree_variants.id = exchange_variants.variant_id)').
where('spree_variants.product_id IN (?)', products).select('DISTINCT enterprises.id')

where(id: pds | exs)
}

scope :managed_by, lambda { |user|
if user.has_spree_role?('admin')
scoped
Expand Down

0 comments on commit 017916b

Please sign in to comment.