From 6a45e8be3bcca253e5c65e79faf0c56097a78909 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 18 Jan 2013 15:29:36 +1100 Subject: [PATCH] Make tests pass after changes to checkout broke them all --- app/models/enterprise.rb | 2 +- .../spree/products/_add_to_cart.html.haml | 9 ++- app/views/spree/products/_source.html.haml | 8 +-- .../enterprises_controller_spec.rb | 61 ------------------- spec/controllers/orders_controller_spec.rb | 25 ++++++++ spec/requests/consumer/checkout_spec.rb | 2 +- spec/requests/consumer/distributors_spec.rb | 23 ++++--- spec/requests/consumer/product_spec.rb | 3 +- spec/requests/consumer/taxonomy_spec.rb | 2 +- 9 files changed, 48 insertions(+), 87 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9afcaaff58..17c9380097 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -29,7 +29,7 @@ class Enterprise < ActiveRecord::Base end def available_variants - ProductDistribution.find_all_by_distributor_id( self.id ).map{ |pd| pd.product.variants }.flatten + ProductDistribution.find_all_by_distributor_id( self.id ).map{ |pd| pd.product.variants + [pd.product.master] }.flatten end diff --git a/app/views/spree/products/_add_to_cart.html.haml b/app/views/spree/products/_add_to_cart.html.haml index b60d0173fc..495ea77bbb 100644 --- a/app/views/spree/products/_add_to_cart.html.haml +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -3,12 +3,11 @@ - if !@product.has_stock? && !Spree::Config[:allow_backorders] = content_tag('strong', t(:out_of_stock)) - - elsif current_order(false) && !order.can_add_product_to_cart?(@product) + - elsif !order.nil? && !order.can_add_product_to_cart?(@product) .error-distributor Please complete your order at = link_to current_distributor.name, root_path before shopping with another distributor. - - else %div(class = "columns alpha two") %div Quantity @@ -19,12 +18,12 @@ = number_field_tag (@product.has_variants? ? :max_quantity : "variant_attributes[#{@product.master.id}][max_quantity]"), 1, :class => 'title max_quantity', :in => 1..@product.on_hand %div.cleared %br - - if order.nil? + - if order.nil? || order.distributor.nil? %div Distributor for your order: - = select_tag "distributor_id", options_from_collection_for_select([Enterprise.new]+@product.distributors, "id", "name", current_distributor.andand.id) + = select_tag "distributor_id", options_from_collection_for_select([Enterprise.new]+@product.distributors, "id", "name", :include_blank => '') - else - available_distributors = DistributorChangeValidator.new(order).available_distributors(@product.distributors) - - if available_distributors.length > 0 + - if available_distributors.length > 1 %div Distributor for your order: = select_tag "distributor_id", options_from_collection_for_select(available_distributors, "id", "name", current_distributor.andand.id) - else diff --git a/app/views/spree/products/_source.html.haml b/app/views/spree/products/_source.html.haml index 1fd3598ef4..0757658c0e 100644 --- a/app/views/spree/products/_source.html.haml +++ b/app/views/spree/products/_source.html.haml @@ -12,7 +12,7 @@ - order = current_order(false) - validator = DistributorChangeValidator.new(order) - @product.distributors.each do |distributor| - - if distributor == order.distributor + - if !order.nil? && distributor == order.distributor %tr.odd %td %b= link_to(distributor.name, [main_app, distributor]) @@ -21,10 +21,10 @@ - elsif order.nil? || validator.can_change_to_distributor?(distributor) %tr.even %td= link_to distributor.name, [main_app, distributor] - %td= link_to "Change to distributor", select_distributor_order_path(distributor) + %td Available + -#%td= link_to "Change to distributor", select_distributor_order_path(distributor) - else %tr.even %td= link_to distributor.name, [main_app, distributor] - %td - %abbr(title="One or more of the products in your cart is not available from this distributor") Unavailable + %td \ No newline at end of file diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index 1554984dfb..a5709d3a74 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -19,65 +19,4 @@ describe EnterprisesController do assigns(:suppliers).should == [s] end - - it "selects distributors" do - d = create(:distributor_enterprise) - - spree_get :select_distributor, :id => d.id - response.should be_redirect - - order = current_order(false) - order.distributor.should == d - end - - it "deselects distributors" do - d = create(:distributor_enterprise) - order = current_order(true) - order.distributor = d - order.save! - - spree_get :deselect_distributor - response.should be_redirect - - order.reload - order.distributor.should be_nil - end - - context "when a product has been added to the cart" do - it "does not allow selecting another distributor" do - # Given some distributors and an order with a product - d1 = create(:distributor_enterprise) - d2 = create(:distributor_enterprise) - p = create(:product, :distributors => [d1]) - o = current_order(true) - - o.distributor = d1 - o.save! - o.add_variant(p.master, 1) - - # When I attempt to select a distributor - spree_get :select_distributor, :id => d2.id - - # Then my distributor should remain unchanged - o.reload - o.distributor.should == d1 - end - - it "does not allow deselecting distributors" do - # Given a distributor and an order with a product - d = create(:distributor_enterprise) - p = create(:product, :distributors => [d]) - o = current_order(true) - o.distributor = d - o.save! - o.add_variant(p.master, 1) - - # When I attempt to deselect the distributor - spree_get :deselect_distributor - - # Then my distributor should remain unchanged - o.reload - o.distributor.should == d - end - end end diff --git a/spec/controllers/orders_controller_spec.rb b/spec/controllers/orders_controller_spec.rb index ded96678dd..440bf8fcbf 100644 --- a/spec/controllers/orders_controller_spec.rb +++ b/spec/controllers/orders_controller_spec.rb @@ -7,7 +7,32 @@ describe Spree::OrdersController do def current_user controller.current_user end + + it "selects distributors" do + d = create(:distributor_enterprise) + p = create(:product, :distributors => [d]) + spree_get :select_distributor, :id => d.id + response.should be_redirect + + order = current_order(false) + order.distributor.should == d + end + + it "deselects distributors" do + d = create(:distributor_enterprise) + p = create(:product, :distributors => [d]) + + order = current_order(true) + order.distributor = d + order.save! + + spree_get :deselect_distributor + response.should be_redirect + + order.reload + order.distributor.should be_nil + end context "adding the first product to the cart" do it "does not add the product if the user does not specify a distributor" do diff --git a/spec/requests/consumer/checkout_spec.rb b/spec/requests/consumer/checkout_spec.rb index 2a0bce8c5d..320dfcf281 100644 --- a/spec/requests/consumer/checkout_spec.rb +++ b/spec/requests/consumer/checkout_spec.rb @@ -118,7 +118,7 @@ feature %q{ click_button 'Save and Continue' # -- Checkout: Payment - click_button 'Save and Continue' + click_button 'Process My Order' # -- Checkout: Order complete page.should have_content('Your order has been processed successfully') diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index 32e642edf5..cd03a67def 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -67,11 +67,10 @@ feature %q{ it "displays the distributor's name on the home page" do # Given a distributor with a product d = create(:distributor_enterprise, :name => 'Melb Uni Co-op', :description => '

Hello, world!

') - create(:product, :distributors => [d]) + p1 = create(:product, :distributors => [d]) # When I select the distributor - visit spree.root_path - click_link d.name + visit spree.select_distributor_order_path(d) visit spree.root_path # Then I should see the name of the distributor that I've selected @@ -90,8 +89,7 @@ feature %q{ p2 = create(:product, :distributors => [d2], :taxons => [taxon]) # When I select the first distributor - visit spree.root_path - click_link d1.name + visit spree.select_distributor_order_path(d1) visit spree.root_path # Then I should see products split by local/remote distributor @@ -112,11 +110,11 @@ feature %q{ it "allows the user to leave the distributor" do # Given a distributor with a product d = create(:distributor_enterprise, :name => 'Melb Uni Co-op') - create(:product, :distributors => [d]) + p1 = create(:product, :distributors => [d]) # When I select the distributor and then leave it + visit spree.select_distributor_order_path(d) visit spree.root_path - click_link d.name click_button 'Browse All Distributors' # Then I should have left the distributor @@ -139,16 +137,17 @@ feature %q{ it "displays the local distributor as the default choice when available for the current product" do # Given a distributor and a product under it - distributor = create(:distributor_enterprise) - product = create(:product, :distributors => [distributor]) + distributor1 = create(:distributor_enterprise) + distributor2 = create(:distributor_enterprise) + product = create(:product, :distributors => [distributor1,distributor2]) # When we select the distributor and view the product - visit spree.root_path - click_link distributor.name + visit spree.select_distributor_order_path(distributor1) visit spree.product_path(product) + binding.pry # Then we should see our distributor as the default option when adding the item to our cart - page.should have_selector "select#distributor_id option[value='#{distributor.id}'][selected='selected']" + page.should have_selector "select#distributor_id option[value='#{distributor1.id}'][selected='selected']" end it "works when viewing a product from a remote distributor" do diff --git a/spec/requests/consumer/product_spec.rb b/spec/requests/consumer/product_spec.rb index e8741548e2..1cd5f81574 100644 --- a/spec/requests/consumer/product_spec.rb +++ b/spec/requests/consumer/product_spec.rb @@ -36,8 +36,7 @@ feature %q{ d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) - visit spree.root_path - click_link d.name + visit spree.select_distributor_order_path(d) visit spree.product_path p within '#product-distributor-details' do diff --git a/spec/requests/consumer/taxonomy_spec.rb b/spec/requests/consumer/taxonomy_spec.rb index 9a2190c1a0..47021bb848 100644 --- a/spec/requests/consumer/taxonomy_spec.rb +++ b/spec/requests/consumer/taxonomy_spec.rb @@ -49,7 +49,7 @@ feature %q{ 2.times { create(:product, :taxons => [taxon_three], :distributors => [my_distributor]) } # When I visit the home page and select my distributor - visit spree.root_path + visit spree.select_distributor_order_path(my_distributor) click_link my_distributor.name page.should have_content 'You are shopping at My Distributor'