diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 13a2795127..85a3998c60 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -106,6 +106,7 @@ class Enterprise < ActiveRecord::Base count(distinct: true) end + def self.find_near(suburb) enterprises = [] @@ -137,6 +138,13 @@ class Enterprise < ActiveRecord::Base Spree::Variant.joins(:product => :product_distributions).where('product_distributions.distributor_id=?', self.id) end + # Return all taxons for all distributed products + def taxons + Spree::Product.in_distributor(self).map do |p| + p.taxons + end.flatten.uniq + end + private def initialize_country diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index ae687fa155..d9c1808e94 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -383,4 +383,17 @@ describe Enterprise do Enterprise.find_near(@suburb_in_nsw).count.should eql(0) end end + + describe "taxons" do + let(:distributor) { create(:distributor_enterprise) } + let(:taxon1) { create(:taxon) } + let(:taxon2) { create(:taxon) } + let(:product1) { create(:simple_product, taxons: [taxon1]) } + let(:product2) { create(:simple_product, taxons: [taxon1, taxon2]) } + + it "gets all taxons of all products" do + Spree::Product.stub(:in_distributor).and_return [product1, product2] + distributor.taxons.should == [taxon1, taxon2] + end + end end