From 54fe63e7f30280d28770055e7338f3327f01fcb1 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 28 May 2013 16:04:05 +1000 Subject: [PATCH] User can add subsequent order cycles product to cart, show order cycle selection in page header along with distributor --- .../stylesheets/store/openfoodweb.css.scss | 2 +- .../spree/orders_controller_decorator.rb | 2 +- app/models/spree/product_decorator.rb | 2 +- ...d_current_distribution_to_main_nav_bar.rb} | 4 +- .../shared/_current_distribution.html.haml | 9 ++++ .../shared/_current_distributor.html.haml | 4 -- spec/features/consumer/add_to_cart_spec.rb | 46 +++++++++++++++++-- .../features/consumer/browse_products_spec.rb | 19 +++++++- 8 files changed, 74 insertions(+), 14 deletions(-) rename app/overrides/{add_current_distributor_to_main_nav_bar.rb => add_current_distribution_to_main_nav_bar.rb} (91%) create mode 100644 app/views/spree/shared/_current_distribution.html.haml delete mode 100644 app/views/spree/shared/_current_distributor.html.haml diff --git a/app/assets/stylesheets/store/openfoodweb.css.scss b/app/assets/stylesheets/store/openfoodweb.css.scss index 0aaad1ab0e..bbb0f2f1a5 100644 --- a/app/assets/stylesheets/store/openfoodweb.css.scss +++ b/app/assets/stylesheets/store/openfoodweb.css.scss @@ -22,7 +22,7 @@ table { /* Style current distributor in main nav bar */ nav #main-nav-bar { li { - &#current-distributor { + &#current-distribution { float: right; clear: right; margin: 0.5em 5px 0 0; diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 68c99665f7..9ea0826782 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -33,7 +33,7 @@ Spree::OrdersController.class_eval do if populate_valid? @distributor order = current_order(true) order.set_distributor! @distributor - order.set_order_cycle! @order_cycle + order.set_order_cycle! @order_cycle if @order_cycle else if populate_order_cycle_required diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 3fbb44d342..7f3c7d73b1 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -71,7 +71,7 @@ Spree::Product.class_eval do distribution = self.product_distributions.find_by_distributor_id(distributor) unless distribution - Bugsnag.notify(Exception.new "No product distribution for product #{id} at distributor #{distributor.id}. Perhaps this product is distributed via an order cycle? This is a warning that OrderCycle fees and shipping methods are not yet implemented, and the shipping fee charged is undefined until then.") + Bugsnag.notify(Exception.new "No product distribution for product #{id} at distributor #{distributor.andand.id}. Perhaps this product is distributed via an order cycle? This is a warning that OrderCycle fees and shipping methods are not yet implemented, and the shipping fee charged is undefined until then.") end distribution.andand.shipping_method || Spree::ShippingMethod.where("name != 'Delivery'").last diff --git a/app/overrides/add_current_distributor_to_main_nav_bar.rb b/app/overrides/add_current_distribution_to_main_nav_bar.rb similarity index 91% rename from app/overrides/add_current_distributor_to_main_nav_bar.rb rename to app/overrides/add_current_distribution_to_main_nav_bar.rb index 371b09247e..9f7f0cd4b2 100644 --- a/app/overrides/add_current_distributor_to_main_nav_bar.rb +++ b/app/overrides/add_current_distribution_to_main_nav_bar.rb @@ -1,5 +1,5 @@ Deface::Override.new(:virtual_path => "spree/shared/_main_nav_bar", :name => "add_current_distributor_to_main_nav_bar", :insert_after => "li#link-to-cart", - :partial => "spree/shared/current_distributor", - :original => '0d843946b3a53643c5a7da90a3a21610208db866') \ No newline at end of file + :partial => "spree/shared/current_distribution", + :original => '0d843946b3a53643c5a7da90a3a21610208db866') diff --git a/app/views/spree/shared/_current_distribution.html.haml b/app/views/spree/shared/_current_distribution.html.haml new file mode 100644 index 0000000000..9f47ed6592 --- /dev/null +++ b/app/views/spree/shared/_current_distribution.html.haml @@ -0,0 +1,9 @@ +- if current_distributor || current_order_cycle + %li#current-distribution{'data-hook' => ''} + You are shopping + - if current_distributor + at + = link_to current_distributor.name, root_path + - if current_order_cycle + in + = link_to current_order_cycle.name, root_path diff --git a/app/views/spree/shared/_current_distributor.html.haml b/app/views/spree/shared/_current_distributor.html.haml deleted file mode 100644 index 1b556e7e4f..0000000000 --- a/app/views/spree/shared/_current_distributor.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -- if current_distributor - %li#current-distributor{'data-hook' => ''} - You are shopping at - = link_to current_distributor.name, root_path \ No newline at end of file diff --git a/spec/features/consumer/add_to_cart_spec.rb b/spec/features/consumer/add_to_cart_spec.rb index a17f863eed..15e405d235 100644 --- a/spec/features/consumer/add_to_cart_spec.rb +++ b/spec/features/consumer/add_to_cart_spec.rb @@ -88,7 +88,7 @@ feature %q{ visit spree.cart_path page.should have_selector 'h4 a', :text => p1.name page.should have_selector 'h4 a', :text => p2.name - page.should have_selector "#current-distributor a", :text => d2.name + page.should have_selector "#current-distribution a", :text => d2.name end it "when the only valid distributor is the chosen one, does not allow the user to choose a distributor" do @@ -136,7 +136,7 @@ feature %q{ click_button 'Add To Cart' # Then My distributor should have changed - page.should have_selector "#current-distributor a", :text => d2.name + page.should have_selector "#current-distribution a", :text => d2.name end it "does not allow the user to add a product from a distributor that cannot supply the cart's products" do @@ -211,6 +211,44 @@ feature %q{ order.order_cycle.should == oc end + context "adding a subsequent product to the cart" do + it "when there are several valid order cycles, allows a choice from these options" do + # Given two products, both distributed by two distributors + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p1 = create(:product) + p2 = create(:product) + oc1 = create(:simple_order_cycle, + :distributors => [d1, d2], :variants => [p1.master, p2.master]) + oc2 = create(:simple_order_cycle, + :distributors => [d1, d2], :variants => [p1.master, p2.master]) + + # When I add the first to my cart via d1/oc1 + visit spree.product_path p1 + select d1.name, :from => 'distributor_id' + select oc1.name, :from => 'order_cycle_id' + click_button 'Add To Cart' + + # And I go to add the second, I should have a choice of order cycle and distributor + visit spree.product_path p2 + page.should have_selector '#distributor_id option', :text => d1.name + page.should have_selector '#distributor_id option', :text => d2.name + page.should have_selector '#order_cycle_id option', :text => oc1.name + page.should have_selector '#order_cycle_id option', :text => oc2.name + + # When I add the second, both should be in my cart, and my + # distributor and order cycle should be the one chosen second + select d2.name, :from => 'distributor_id' + select oc2.name, :from => 'order_cycle_id' + click_button 'Add To Cart' + visit spree.cart_path + page.should have_selector 'h4 a', :text => p1.name + page.should have_selector 'h4 a', :text => p2.name + page.should have_selector "#current-distribution a", :text => d2.name + page.should have_selector "#current-distribution a", :text => oc2.name + end + end + it "allows us to add two products from the same distributor" do # Given two products, each at the same distributor d = create(:distributor_enterprise) @@ -259,8 +297,8 @@ feature %q{ # When I add the second to my cart click_button 'Add To Cart' - # Then My distributor should have changed - page.should have_selector "#current-distributor a", :text => d2.name + # Then my distributor should have changed + page.should have_selector "#current-distribution a", :text => d2.name end end diff --git a/spec/features/consumer/browse_products_spec.rb b/spec/features/consumer/browse_products_spec.rb index 2ec4d5f0c4..119c2a3aee 100644 --- a/spec/features/consumer/browse_products_spec.rb +++ b/spec/features/consumer/browse_products_spec.rb @@ -38,6 +38,23 @@ feature %q{ page.should_not have_selector 'div.distributor-description' end + it "displays the distributor and order cycle name on the home page when an order cycle is selected" do + # Given a distributor with a product + d = create(:distributor_enterprise, :name => 'Melb Uni Co-op') + p = create(:product) + oc = create(:simple_order_cycle, :name => 'Bulk Foods', :distributors => [d], :variants => [p.master]) + + # When I select the distributor and order cycle + visit spree.product_path p + select d.name, :from => 'distributor_id' + select oc.name, :from => 'order_cycle_id' + click_button 'Add To Cart' + + # Then I should see the name of the distributor and order cycle that I've selected + page.should have_content 'You are shopping at Melb Uni Co-op in Bulk Foods' + page.should_not have_selector 'div.distributor-description' + end + it "splits the product listing by local/remote distributor" do # Given two distributors, with a product under each, and each product under a taxon taxonomy = Spree::Taxonomy.find_by_name('Products') || create(:taxonomy, :name => 'Products') @@ -78,7 +95,7 @@ feature %q{ click_button 'Browse All Distributors' # Then I should have left the distributor - page.should_not have_selector '#current-distributor', :text => 'You are shopping at Melb Uni Co-op' + page.should_not have_selector '#current-distribution', :text => 'You are shopping at Melb Uni Co-op' end context "viewing a product, it provides a choice of distributor when adding to cart" do