Skip to content

Commit

Permalink
Optimise Spree::Taxon.supplied_taxons
Browse files Browse the repository at this point in the history
  • Loading branch information
johansenja committed Aug 14, 2024
1 parent 565cb82 commit 797afe6
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ def pretty_name
#
# Optionally, specify some enterprise_ids to scope the results
def self.supplied_taxons(enterprise_ids = nil)
taxons = Spree::Taxon.
joins(variants: :supplier).
select('spree_taxons.*, enterprises.id AS enterprise_id')
taxons = Spree::Taxon.joins(variants: :supplier)

taxons = taxons.where(enterprises: { id: enterprise_ids }) if enterprise_ids.present?

taxons.each_with_object({}) do |t, collection|
collection[t.enterprise_id.to_i] ||= Set.new
collection[t.enterprise_id.to_i] << t.id
taxons
.pluck('spree_taxons.id, enterprises.id AS enterprise_id')
.each_with_object({}) do |(taxon_id, enterprise_id), collection|
collection[enterprise_id.to_i] ||= Set.new
collection[enterprise_id.to_i] << taxon_id
end
end

Expand Down

0 comments on commit 797afe6

Please sign in to comment.