diff --git a/app/assets/stylesheets/store/openfoodweb.css.scss b/app/assets/stylesheets/store/openfoodweb.css.scss
index ef94357497..107fdd6a20 100644
--- a/app/assets/stylesheets/store/openfoodweb.css.scss
+++ b/app/assets/stylesheets/store/openfoodweb.css.scss
@@ -76,6 +76,13 @@ nav#filters {
.distributors {
float: left;
margin-right: 4em;
+
+ option.local {
+ background-color: #cfc;
+ }
+ option.remote {
+ background-color: #fcc;
+ }
}
.order-cycles {
diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb
index f4744bb3a9..92b2328854 100644
--- a/app/controllers/spree/orders_controller_decorator.rb
+++ b/app/controllers/spree/orders_controller_decorator.rb
@@ -18,7 +18,11 @@ Spree::OrdersController.class_eval do
redirect_to request.referer
elsif params[:commit] == 'Choose Order Cycle'
- # TODO
+ order_cycle = OrderCycle.active.find params[:order][:order_cycle_id]
+ @order.order_cycle = order_cycle
+ @order.save!
+
+ flash[:notice] = 'Your order cycle has been selected.'
redirect_to request.referer
end
end
diff --git a/app/helpers/order_cycles_helper.rb b/app/helpers/order_cycles_helper.rb
index 3ac04651bc..4b76413b81 100644
--- a/app/helpers/order_cycles_helper.rb
+++ b/app/helpers/order_cycles_helper.rb
@@ -1,10 +1,14 @@
module OrderCyclesHelper
+ def current_order_cycle
+ @current_order_cycle ||= current_order(false).andand.order_cycle
+ end
+
def coordinating_enterprises
Enterprise.is_distributor.order('name')
end
def order_cycle_local_remote_class(distributor, order_cycle)
- if distributor.nil?
+ if distributor.nil? || order_cycle.nil?
''
elsif order_cycle.distributors.include? distributor
' local'
@@ -12,4 +16,10 @@ module OrderCyclesHelper
' remote'
end
end
+
+ def distributor_options(distributors, current_distributor, order_cycle)
+ options = distributors.map { |d| [d.name, d.id, {:class => order_cycle_local_remote_class(d, order_cycle).strip}] }
+ options_for_select(options, current_distributor)
+ end
+
end
diff --git a/app/views/order_cycles/_selection.html.erb b/app/views/order_cycles/_selection.html.erb
index bd5d6566a0..85af742e8d 100644
--- a/app/views/order_cycles/_selection.html.erb
+++ b/app/views/order_cycles/_selection.html.erb
@@ -5,7 +5,7 @@
<%= form_for current_order(true) do |f| %>
Active Hubs
- <%= f.collection_select :distributor_id, @distributors, :id, :name %>
+ <%= f.select :distributor_id, distributor_options(@distributors, f.object.distributor_id, current_order_cycle) %>
<%= f.submit 'Choose Hub' %>
diff --git a/spec/features/consumer/order_cycles_spec.rb b/spec/features/consumer/order_cycles_spec.rb
index e2f34e2eb4..e291b87eff 100644
--- a/spec/features/consumer/order_cycles_spec.rb
+++ b/spec/features/consumer/order_cycles_spec.rb
@@ -68,7 +68,34 @@ feature %q{
end
end
- scenario "selecting an order cycle highlights valid distributor choices"
+ scenario "selecting an order cycle highlights valid distributor choices" do
+ # When I go to the product listing page
+ visit spree.products_path
+
+ # And I choose an order cycle
+ choose @oc1.name
+ click_button 'Choose Order Cycle'
+
+ # Then the associated distributor should be highlighted
+ page.should have_content "Your order cycle has been selected."
+ within '#distribution-choice' do
+ page.should have_selector "option.local[value='#{@d1.id}']"
+ page.should have_selector "option.remote[value='#{@d2.id}']"
+ end
+
+ # When I choose the other order cycle
+ choose @oc2.name
+ click_button 'Choose Order Cycle'
+
+ # Then the associated distributor should be highlighted
+ page.should have_content "Your order cycle has been selected."
+ within '#distribution-choice' do
+ page.should have_selector "option.remote[value='#{@d1.id}']"
+ page.should have_selector "option.local[value='#{@d2.id}']"
+ end
+ end
+
+
scenario "selecing an invalid distributor clears the order cycle"
scenario "selecing an invalid order cycle clears the distributor"
end
diff --git a/spec/helpers/order_cycles_helper_spec.rb b/spec/helpers/order_cycles_helper_spec.rb
index d284bdf82e..6a0198caa2 100644
--- a/spec/helpers/order_cycles_helper_spec.rb
+++ b/spec/helpers/order_cycles_helper_spec.rb
@@ -7,8 +7,9 @@ describe OrderCyclesHelper do
end
describe "generating local/remote classes for order cycle selection" do
- it "returns blank when no distributor selected" do
+ it "returns blank when no distributor or order cycle is selected" do
subject.order_cycle_local_remote_class(nil, double(:order_cycle)).should == ''
+ subject.order_cycle_local_remote_class(double(:distributor), nil).should == ''
end
it "returns local when the order cycle includes the current distributor" do