diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index cbfed6b3ef..8be6946927 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -136,6 +136,14 @@ class Enterprise < ActiveRecord::Base ", self.id, self.id) end + def distributors + self.relatives.is_distributor + end + + def suppliers + self.relatives.is_supplier + end + def distributed_variants Spree::Variant.joins(:product).merge(Spree::Product.in_distributor(self)).select('spree_variants.*') end @@ -149,11 +157,17 @@ class Enterprise < ActiveRecord::Base end # Return all taxons for all distributed products - def taxons + def distributed_taxons Spree::Product.in_distributor(self).map do |p| p.taxons end.flatten.uniq end + # Return all taxons for all supplied products + def supplied_taxons + Spree::Product.in_supplier(self).map do |p| + p.taxons + end.flatten.uniq + end private diff --git a/app/views/json/_enterprises.rabl b/app/views/json/_enterprises.rabl index 61db6f9e8d..97e040eca9 100644 --- a/app/views/json/_enterprises.rabl +++ b/app/views/json/_enterprises.rabl @@ -1,9 +1,5 @@ # DON'T USE DIRECTLY - for inheritance -attributes :name, :id - -child :taxons => :taxons do - attributes :name, :id -end +attributes :name, :id, :description child :address do extends "json/partials/address" diff --git a/app/views/json/_hubs.rabl b/app/views/json/_hubs.rabl index 8a1bc5464c..1950c9903b 100644 --- a/app/views/json/_hubs.rabl +++ b/app/views/json/_hubs.rabl @@ -1,6 +1,17 @@ collection Enterprise.visible.is_distributor extends 'json/enterprises' +child distributed_taxons: :taxons do + attributes :name, :id +end + +child producers: :producers do + attributes :name, :id + node :path do |producer| + producer_path(producer_path) + end +end + node :pickup do |hub| not hub.shipping_methods.where(:require_ship_address => false).empty? end diff --git a/app/views/json/_producers.rabl b/app/views/json/_producers.rabl index 02264cedb2..b5111bc0cb 100644 --- a/app/views/json/_producers.rabl +++ b/app/views/json/_producers.rabl @@ -1,6 +1,17 @@ collection @producers extends 'json/enterprises' +child supplied_taxons: :taxons do + attributes :name, :id +end + +child distributors: :distributors do + attributes :name, :id + node :path do |distributor| + distributor_path(distributor) + end +end + node :path do |producer| producer_path(producer) end diff --git a/app/views/producers/_fat.html.haml b/app/views/producers/_fat.html.haml index ba1c8889b3..dfac8fd31f 100644 --- a/app/views/producers/_fat.html.haml +++ b/app/views/producers/_fat.html.haml @@ -3,14 +3,17 @@ %strong Shop for %p.trans-sentence {{ producer.taxons | printArrayOfObjects }} - .columns.small-8 - About Us + .columns.small-4 + %strong About us + {{ producer.description }} + .columns.small-4 + %strong Our distributors .row.active_table_row.link{"ng-show" => "open()", "ng-repeat" => "hub in producer.hubs"} .columns.small-11 %a{"bo-href" => "hub.path"} Shop at - %strong {{ hub.name }} + %strong {{ producer.name }} .columns.small-1.text-right %a{"bo-href" => "hub.path"} %i.fi-arrow-right diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index fb749bb1ce..96f64ecfee 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -28,14 +28,28 @@ describe Enterprise do end describe "relationships to other enterprises" do - it "finds relatives" do - e, p, c = create(:enterprise), create(:enterprise), create(:enterprise) - + let(:e) { create(:distributor_enterprise) } + let(:p) { create(:supplier_enterprise) } + let(:c) { create(:distributor_enterprise) } + before do EnterpriseRelationship.create! parent_id: p.id, child_id: e.id EnterpriseRelationship.create! parent_id: e.id, child_id: c.id - + end + it "finds relatives" do e.relatives.sort.should == [p, c].sort end + + it "scopes relatives to distributors" do + e.should_receive(:relatives).and_return(relatives = []) + relatives.should_receive(:is_distributor) + e.distributors + end + + it "scopes relatives to producers" do + e.should_receive(:relatives).and_return(relatives = []) + relatives.should_receive(:is_supplier) + e.suppliers + end end end @@ -405,14 +419,20 @@ describe Enterprise do describe "taxons" do let(:distributor) { create(:distributor_enterprise) } + let(:supplier) { create(:supplier_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 + it "gets all taxons of all distributed products" do Spree::Product.stub(:in_distributor).and_return [product1, product2] - distributor.taxons.should == [taxon1, taxon2] + distributor.distributed_taxons.should == [taxon1, taxon2] + end + + it "gets all taxons of all supplied products" do + Spree::Product.stub(:in_supplier).and_return [product1, product2] + supplier.supplied_taxons.should == [taxon1, taxon2] end end end