From 9f169e6ef8ade8545827eb44be0fdc78e25156f6 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 14 May 2014 16:15:18 +1000 Subject: [PATCH] Adding a basic stub of the primary taxon --- app/models/spree/product_decorator.rb | 4 ++ app/views/shop/products.rabl | 4 ++ spec/controllers/shop_controller_spec.rb | 92 +++--------------------- spec/models/spree/product_spec.rb | 10 +++ 4 files changed, 26 insertions(+), 84 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 282a93cfb5..ba7cbc253d 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -118,6 +118,10 @@ Spree::Product.class_eval do order_cycle.variants_distributed_by(distributor).where(product_id: self) end + def primary_taxon + self.taxons.order.first + end + # Build a product distribution for each distributor def build_product_distributions_for_user user Enterprise.is_distributor.managed_by(user).each do |distributor| diff --git a/app/views/shop/products.rabl b/app/views/shop/products.rabl index a7430fc08c..4f0eb0ce90 100644 --- a/app/views/shop/products.rabl +++ b/app/views/shop/products.rabl @@ -13,6 +13,10 @@ child :supplier => :supplier do attributes :id, :name, :description end +child :primary_taxon => :primary_taxon do + attributes :id, :permalink, :name +end + child :master => :master do attributes :id, :is_master, :count_on_hand, :options_text, :count_on_hand, :on_demand child :images => :images do diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 88cde0e6d9..3683137b79 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -111,90 +111,6 @@ describe ShopController do response.body.should be_empty end - # TODO: this should be a controller test baby - pending "filtering products" do - let(:distributor) { create(:distributor_enterprise) } - let(:supplier) { create(:supplier_enterprise) } - let(:oc1) { create(:simple_order_cycle, distributors: [distributor], coordinator: create(:distributor_enterprise), orders_close_at: 2.days.from_now) } - let(:p1) { create(:simple_product, on_demand: false) } - let(:p2) { create(:simple_product, on_demand: true) } - let(:p3) { create(:simple_product, on_demand: false) } - let(:p4) { create(:simple_product, on_demand: false) } - let(:p5) { create(:simple_product, on_demand: false) } - let(:p6) { create(:simple_product, on_demand: false) } - let(:p7) { create(:simple_product, on_demand: false) } - let(:v1) { create(:variant, product: p4, unit_value: 2) } - let(:v2) { create(:variant, product: p4, unit_value: 3, on_demand: false) } - let(:v3) { create(:variant, product: p4, unit_value: 4, on_demand: true) } - let(:v4) { create(:variant, product: p5) } - let(:v5) { create(:variant, product: p5) } - let(:v6) { create(:variant, product: p7) } - let(:order) { create(:order, distributor: distributor, order_cycle: order_cycle) } - - before do - p1.master.count_on_hand = 1 - p2.master.count_on_hand = 0 - p1.master.update_attribute(:count_on_hand, 1) - p2.master.update_attribute(:count_on_hand, 0) - p3.master.update_attribute(:count_on_hand, 0) - p6.master.update_attribute(:count_on_hand, 1) - p6.delete - p7.master.update_attribute(:count_on_hand, 1) - v1.update_attribute(:count_on_hand, 1) - v2.update_attribute(:count_on_hand, 0) - v3.update_attribute(:count_on_hand, 0) - v4.update_attribute(:count_on_hand, 1) - v5.update_attribute(:count_on_hand, 0) - v6.update_attribute(:count_on_hand, 1) - v6.update_attribute(:deleted_at, Time.now) - exchange = Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id) - exchange.update_attribute :pickup_time, "frogs" - exchange.variants << p1.master - exchange.variants << p2.master - exchange.variants << p3.master - exchange.variants << p6.master - exchange.variants << v1 - exchange.variants << v2 - exchange.variants << v3 - # v4 is in stock but not in distribution - # v5 is out of stock and in the distribution - # Neither should display, nor should their product, p5 - exchange.variants << v5 - exchange.variants << v6 - - controller.stub(:current_order).and_return order - visit shop_path - end - - it "filters products based on availability" do - # It shows on hand products - page.should have_content p1.name - page.should have_content p4.name - - # It shows on demand products - page.should have_content p2.name - - # It does not show products that are neither on hand or on demand - page.should_not have_content p3.name - - # It shows on demand variants - page.should have_content v3.options_text - - # It does not show variants that are neither on hand or on demand - page.should_not have_content v2.options_text - - # It does not show products that have no available variants in this distribution - page.should_not have_content p5.name - - # It does not show deleted products - page.should_not have_content p6.name - - # It does not show deleted variants - page.should_not have_content v6.name - page.should_not have_content p7.name - end - end - context "RABL tests" do render_views before do @@ -211,6 +127,7 @@ describe ShopController do xhr :get, :products response.body.should_not have_content product.name end + it "strips html from description" do product.update_attribute(:description, "turtles frogs") xhr :get, :products @@ -223,6 +140,13 @@ describe ShopController do xhr :get, :products response.body.should have_content "998.0" end + + it "includes the primary taxon" do + taxon = mock_model(Spree::Taxon, name: "fruitbat") + Spree::Product.any_instance.stub(:primary_taxon).and_return taxon + xhr :get, :products + response.body.should have_content "fruitbat" + end end end end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 6743e404e9..6d90b70761 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -547,5 +547,15 @@ module Spree end end end + + describe "Taxons" do + let(:taxon1) { create(:taxon) } + let(:taxon2) { create(:taxon) } + let(:product) { create(:simple_product, taxons: [taxon1, taxon2]) } + + it "returns the first taxon as the primary taxon" do + product.primary_taxon.should == taxon1 + end + end end end