From 1f9f5efb7981eac08469ead75369c7af003ee7be Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 28 May 2013 14:06:52 +1000 Subject: [PATCH] Add first product to cart with order cycle choice --- .../spree/orders_controller_decorator.rb | 1 + app/helpers/add_to_cart_helper.rb | 5 ++++ .../spree/products/_add_to_cart.html.haml | 5 ++++ .../_add_to_cart_order_cycle_choice.html.haml | 2 ++ spec/features/consumer/add_to_cart_spec.rb | 28 +++++++++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 app/views/spree/products/_add_to_cart_order_cycle_choice.html.haml diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index e38721ddbf..68c99665f7 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -33,6 +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 else if populate_order_cycle_required diff --git a/app/helpers/add_to_cart_helper.rb b/app/helpers/add_to_cart_helper.rb index 0dde582f8e..1f336033a4 100644 --- a/app/helpers/add_to_cart_helper.rb +++ b/app/helpers/add_to_cart_helper.rb @@ -10,4 +10,9 @@ module AddToCartHelper def available_distributors_for(order, product) DistributionChangeValidator.new(order).available_distributors_for(product) end + + def available_order_cycles_for(order, product) + DistributionChangeValidator.new(order).available_order_cycles_for(product) + end + end diff --git a/app/views/spree/products/_add_to_cart.html.haml b/app/views/spree/products/_add_to_cart.html.haml index 3ea1778819..aa15449f60 100644 --- a/app/views/spree/products/_add_to_cart.html.haml +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -13,6 +13,8 @@ %div.cleared %br - available_distributors = available_distributors_for(order, @product) + - available_order_cycles = available_order_cycles_for(order, @product) + - if available_distributors.length > 1 || order.andand.distributor.nil? = render 'add_to_cart_distributor_choice', distributor_collection: available_distributors @@ -21,6 +23,9 @@ - changing_distributor = distributor != order.andand.distributor = render 'add_to_cart_distributor_fixed', distributor: distributor, changing_distributor: changing_distributor + + = render 'add_to_cart_order_cycle_choice', order_cycle_collection: available_order_cycles + %br = button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do = t(:add_to_cart) diff --git a/app/views/spree/products/_add_to_cart_order_cycle_choice.html.haml b/app/views/spree/products/_add_to_cart_order_cycle_choice.html.haml new file mode 100644 index 0000000000..68c3e5f45e --- /dev/null +++ b/app/views/spree/products/_add_to_cart_order_cycle_choice.html.haml @@ -0,0 +1,2 @@ +%div Order cycle for your order: += select_tag "order_cycle_id", options_from_collection_for_select([OrderCycle.new]+order_cycle_collection, "id", "name", current_order_cycle.andand.id) diff --git a/spec/features/consumer/add_to_cart_spec.rb b/spec/features/consumer/add_to_cart_spec.rb index dc44e33b7c..a17f863eed 100644 --- a/spec/features/consumer/add_to_cart_spec.rb +++ b/spec/features/consumer/add_to_cart_spec.rb @@ -182,6 +182,34 @@ feature %q{ Spree::Order.last.should be_nil end + scenario "adding the first product to the cart" do + # Given a product and some distributors + d = create(:distributor_enterprise) + p = create(:product, :price => 12.34) + oc = create(:simple_order_cycle, :distributors => [d], :variants => [p.master]) + + # When I add an item to my cart + visit spree.product_path p + select d.name, :from => 'distributor_id' + select oc.name, :from => 'order_cycle_id' + click_button 'Add To Cart' + + # Then the correct totals should be displayed + page.should have_selector 'span.item-total', :text => '$12.34' + + # TODO: Test these when order cycle fees is implemented + # page.should have_selector 'span.shipping-total', :text => '$1.23' + # page.should have_selector 'span.grand-total', :text => '$13.57' + + # And the item should be in my cart + order = Spree::Order.last + line_item = order.line_items.first + line_item.product.should == p + + # And my order should have its distributor and order cycle set to the chosen ones + order.distributor.should == d + order.order_cycle.should == oc + end it "allows us to add two products from the same distributor" do # Given two products, each at the same distributor