Adding a taxon collection mechanism to Enterprise

This commit is contained in:
Will Marshall
2014-04-25 12:45:03 +10:00
parent dbf15c03d3
commit cf67b5fcac
2 changed files with 21 additions and 0 deletions

View File

@@ -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

View File

@@ -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