From 74e2b70d3a9e6259ff3b02798d46ee7c75d5588c Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 1 Feb 2013 11:58:38 +1100 Subject: [PATCH] Without JS, select distributor, highlighting local order cycle choices --- .../stylesheets/store/openfoodweb.css.scss | 7 ++ .../spree/orders_controller_decorator.rb | 21 +++++ app/views/order_cycles/_selection.html.erb | 5 +- spec/features/consumer/order_cycles_spec.rb | 83 +++++++++++++++---- 4 files changed, 97 insertions(+), 19 deletions(-) diff --git a/app/assets/stylesheets/store/openfoodweb.css.scss b/app/assets/stylesheets/store/openfoodweb.css.scss index fe996a49bf..ef94357497 100644 --- a/app/assets/stylesheets/store/openfoodweb.css.scss +++ b/app/assets/stylesheets/store/openfoodweb.css.scss @@ -80,6 +80,13 @@ nav#filters { .order-cycles { float: left; + + tr.local { + background-color: #cfc; + } + tr.remote { + background-color: #fcc; + } } } diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 730d96b279..f4744bb3a9 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -3,6 +3,27 @@ Spree::OrdersController.class_eval do before_filter :populate_order_count_on_hand, :only => :populate after_filter :populate_variant_attributes, :only => :populate + before_filter :update_distribution, :only => :update + + + def update_distribution + @order = current_order(true) + + if params[:commit] == 'Choose Hub' + distributor = Enterprise.is_distributor.find params[:order][:distributor_id] + @order.distributor = distributor + @order.save! + + flash[:notice] = 'Your hub has been selected.' + redirect_to request.referer + + elsif params[:commit] == 'Choose Order Cycle' + # TODO + redirect_to request.referer + end + end + + def populate_order_distributor @distributor = params[:distributor_id].present? ? Enterprise.is_distributor.find(params[:distributor_id]) : nil diff --git a/app/views/order_cycles/_selection.html.erb b/app/views/order_cycles/_selection.html.erb index 1df1fd9129..bd5d6566a0 100644 --- a/app/views/order_cycles/_selection.html.erb +++ b/app/views/order_cycles/_selection.html.erb @@ -6,13 +6,15 @@

Active Hubs

<%= f.collection_select :distributor_id, @distributors, :id, :name %> +
+ <%= f.submit 'Choose Hub' %>

Closing Soon

<% @order_cycles.each do |order_cycle| %> - + <% end %>
<%= f.radio_button :order_cycle_id, order_cycle.id %> @@ -23,6 +25,7 @@
+ <%= f.submit 'Choose Order Cycle' %>
<% end %> diff --git a/spec/features/consumer/order_cycles_spec.rb b/spec/features/consumer/order_cycles_spec.rb index e44dbcd5b7..e2f34e2eb4 100644 --- a/spec/features/consumer/order_cycles_spec.rb +++ b/spec/features/consumer/order_cycles_spec.rb @@ -8,29 +8,32 @@ feature %q{ include AuthenticationWorkflow include WebHelper - scenario "viewing order cycle and distributor choices", js: true do + background do create(:itemwise_shipping_method) # Given some hubs and order cycles - coord = create(:distributor_enterprise) - d1 = create(:distributor_enterprise) - d2 = create(:distributor_enterprise) - create(:product, distributors: [d1, d2]) + create(:distributor_enterprise) + @d1 = create(:distributor_enterprise) + @d2 = create(:distributor_enterprise) + create(:product, distributors: [@d1, @d2]) - oc1 = create(:simple_order_cycle, orders_close_at: Time.zone.now + 1.week) - oc2 = create(:simple_order_cycle, orders_close_at: Time.zone.now + 2.days) - create(:exchange, order_cycle: oc1, sender: oc1.coordinator, receiver: d1) - create(:exchange, order_cycle: oc2, sender: oc2.coordinator, receiver: d2) + @oc1 = create(:simple_order_cycle, orders_close_at: Time.zone.now + 1.week) + @oc2 = create(:simple_order_cycle, orders_close_at: Time.zone.now + 2.days) + create(:exchange, order_cycle: @oc1, sender: @oc1.coordinator, receiver: @d1) + create(:exchange, order_cycle: @oc2, sender: @oc2.coordinator, receiver: @d2) + end + scenario "viewing order cycle and distributor choices" do # When I go to the product listing page visit spree.products_path # Then I should see a choice of hubs - page.should have_selector "#distribution-choice option[value='#{d1.id}']", text: d1.name - page.should have_selector "#distribution-choice option[value='#{d2.id}']", text: d2.name + page.should have_selector "#distribution-choice option[value='#{@d1.id}']", text: @d1.name + page.should have_selector "#distribution-choice option[value='#{@d2.id}']", text: @d2.name + fail "Distributors need to use new scope for order cycle activity (not with_distributed_active_products_on_hand). See application controller." # And I should see a choice of order cycles with closing times - [{oc: oc1, closing: '7 days'}, {oc: oc2, closing: '2 days'}].each do |data| + [{oc: @oc1, closing: '7 days'}, {oc: @oc2, closing: '2 days'}].each do |data| within "tr.order-cycle-#{data[:oc].id}" do page.should have_content data[:oc].name page.should have_content data[:closing] @@ -38,14 +41,58 @@ feature %q{ end end - scenario "making an order cycle or distributor choice filters the remaining choices to valid options" do - # When I select a hub - # Then my choice of order cycles should be limited to that hub - # When I select an order cycle - # Then my choice of hubs should be limited to that order cycle - pending + context "without javascript" do + scenario "selecting a distributor highlights valid order cycle choices" do + # When I go to the product listing page + visit spree.products_path + + # And I choose a distributor + select @d1.name, from: 'order_distributor_id' + click_button 'Choose Hub' + + # Then associated order cycles should be highlighted + page.should have_content "Your hub has been selected." + within '#distribution-choice' do + page.should have_selector "tr.order-cycle-#{@oc1.id}.local" + page.should have_selector "tr.order-cycle-#{@oc2.id}.remote" + end + + # When I choose the other distributor + select @d2.name, from: 'order_distributor_id' + click_button 'Choose Hub' + + # Then associated order cycles should be highlighted + within '#distribution-choice' do + page.should have_selector "tr.order-cycle-#{@oc1.id}.remote" + page.should have_selector "tr.order-cycle-#{@oc2.id}.local" + end + end + + scenario "selecting an order cycle highlights valid distributor choices" + scenario "selecing an invalid distributor clears the order cycle" + scenario "selecing an invalid order cycle clears the distributor" end + # scenario "making an order cycle or distributor choice filters the remaining choices to valid options", js: true do + # # When I go to the product listing page + # visit spree.products_path + + # # And I select a hub + # select @d1.name, from: 'order_distributor_id' + + # # Then my choice of order cycles should be limited to that hub + # page.should have_selector "input#order_order_cycle_id_#{@oc1.id}" + # page.should_not have_selector "input#order_order_cycle_id_#{@oc1.id}" + + # # When I select an order cycle + # select '', from: 'order_distributor_id' + # choose "order_distributor_id_#{@d2.id}" + + # # Then my choice of hubs should be limited to that order cycle + # page.should_not have_selector "option[value='#{@d1.id}']", text: @d1.name + # page.should have_selector "option[value='#{@d2.id}']", text: @d2.name + # end + scenario "selecting an order cycle and distributor" do # When I select a hub and an order cycle and click "Select" # Then my distribution info should be set