mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Find all supplied and distributed taxons
This commit is contained in:
@@ -9,4 +9,40 @@ Spree::Taxon.class_eval do
|
||||
#fs << Spree::ProductFilters.distributor_filter if Spree::ProductFilters.respond_to? :distributor_filter
|
||||
fs
|
||||
end
|
||||
|
||||
# Find all the taxons of supplied products for each enterprise, indexed by enterprise.
|
||||
# Format: {enterprise_id => [taxon_id, ...]}
|
||||
def self.supplied_taxons
|
||||
taxons = {}
|
||||
|
||||
Spree::Taxon.
|
||||
joins(:products => :supplier).
|
||||
select('spree_taxons.*, enterprises.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
|
||||
end
|
||||
|
||||
# Find all the taxons of distributed products for each enterprise, indexed by enterprise.
|
||||
# Format: {enterprise_id => [taxon_id, ...]}
|
||||
def self.distributed_taxons
|
||||
taxons = {}
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
28
spec/models/spree/taxon_spec.rb
Normal file
28
spec/models/spree/taxon_spec.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
describe Taxon do
|
||||
let(:e) { create(:supplier_enterprise) }
|
||||
let(:t0) { p1.taxons.order('id ASC').first }
|
||||
let(:t1) { create(:taxon) }
|
||||
let(:t2) { create(:taxon) }
|
||||
|
||||
describe "finding all supplied taxons" do
|
||||
let!(:p1) { create(:simple_product, supplier: e, taxons: [t1, t2]) }
|
||||
|
||||
it "finds taxons" do
|
||||
Taxon.supplied_taxons.should == {e.id => Set.new([t0.id, t1.id, t2.id])}
|
||||
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]) }
|
||||
|
||||
it "finds taxons" do
|
||||
Taxon.distributed_taxons.should == {e.id => Set.new([t0.id, t1.id, t2.id])}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user