From ec72e9137abfdba1fd4383d7c042e076eb546fbe Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Thu, 5 Dec 2013 15:07:46 +1100 Subject: [PATCH] We can now hit the index page --- app/controllers/shop_controller.rb | 17 ++++++++++++++++- app/helpers/order_cycles_helper.rb | 4 ++-- app/helpers/shop_helper.rb | 9 ++++++++- app/views/shop/index.html.haml | 4 +++- spec/controllers/shop_controller_spec.rb | 19 +++++++++++++++++-- spec/features/consumer/shopping_spec.rb | 4 ++-- spec/helpers/distributors_helper_spec.rb | 15 --------------- spec/helpers/order_cycles_helper_spec.rb | 13 +++++++++++++ spec/helpers/shop_helper_spec.rb | 11 +++++++++++ spec/models/order_cycle_spec.rb | 7 +++++++ 10 files changed, 79 insertions(+), 24 deletions(-) delete mode 100644 spec/helpers/distributors_helper_spec.rb create mode 100644 spec/helpers/shop_helper_spec.rb diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 777b7b26c7..0df04edb50 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -1,9 +1,24 @@ class ShopController < BaseController layout "darkswarm" + before_filter :set_distributor + before_filter :set_order_cycles + def index + end + + private + + def set_distributor @distributor = current_distributor end - + def set_order_cycles + @order_cycles = OrderCycle.with_distributor(@distributor).active + + # And default to the only order cycle if there's only the one + if @order_cycles.count == 1 + current_order(true).set_order_cycle! @order_cycles.first + end + end end diff --git a/app/helpers/order_cycles_helper.rb b/app/helpers/order_cycles_helper.rb index af5f16e10c..8f1e0ae930 100644 --- a/app/helpers/order_cycles_helper.rb +++ b/app/helpers/order_cycles_helper.rb @@ -52,8 +52,8 @@ module OrderCyclesHelper OpenFoodNetwork::FeatureToggle.enabled? :order_cycles end - def pickup_time - current_order_cycle.exchanges.to_enterprises(current_distributor).outgoing.first.pickup_time + def pickup_time(order_cycle = current_order_cycle) + order_cycle.exchanges.to_enterprises(current_distributor).outgoing.first.pickup_time end end diff --git a/app/helpers/shop_helper.rb b/app/helpers/shop_helper.rb index 6d2457ec16..3066bfbe05 100644 --- a/app/helpers/shop_helper.rb +++ b/app/helpers/shop_helper.rb @@ -1,3 +1,10 @@ module ShopHelper - + def order_cycles_name_and_pickup_times(order_cycles) + order_cycles.map do |oc| + [ + pickup_time(oc), + oc.id + ] + end + end end diff --git a/app/views/shop/index.html.haml b/app/views/shop/index.html.haml index 029f130512..9ce7b3af3a 100644 --- a/app/views/shop/index.html.haml +++ b/app/views/shop/index.html.haml @@ -1,10 +1,12 @@ = @distributor.name +Ready for: += select_tag :order_cycle_id, options_for_select(order_cycles_name_and_pickup_times(@order_cycles), current_order_cycle.id) + %description = @distributor.long_description.andand.html_safe - = render partial: "enterprises/contact_us" = render partial: "enterprises/about_us" diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index bf5c8008b6..1747dcff5c 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -1,7 +1,22 @@ require 'spec_helper' -describe DistributorsController do - it "should set the distributor when loading show" +describe ShopController do + it "should select an order cycle when only one order cycle is open" do + d = create(:distributor_enterprise) + oc1 = create(:order_cycle, distributors: [d]) + controller.stub(:current_distributor).and_return d + spree_get :index + controller.current_order_cycle.should == oc1 + end + + it "should not set an order cycle when multiple order cycles are open" do + d = create(:distributor_enterprise) + oc1 = create(:order_cycle, distributors: [d]) + oc2 = create(:order_cycle, distributors: [d]) + controller.stub(:current_distributor).and_return d + spree_get :index + controller.current_order_cycle.should == nil + end it "should create/load an order when loading show" end diff --git a/spec/features/consumer/shopping_spec.rb b/spec/features/consumer/shopping_spec.rb index fb3b0c3e55..81aa9e12d7 100644 --- a/spec/features/consumer/shopping_spec.rb +++ b/spec/features/consumer/shopping_spec.rb @@ -21,11 +21,11 @@ feature "As a consumer I want to shop with a distributor" do it "selects an order cycle if only one is open" do # create order cycle oc1 = create(:simple_order_cycle, name: 'oc 1', distributors: [distributor]) - exchange = Exchange.find(oc1.exchanges.to_enterprises(d).outgoing.first.id) + exchange = Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id) exchange.update_attribute :pickup_time, "turtles" visit shop_index_path - page.should have_selector "option[selected]", text: 'Packing' + page.should have_selector "option[selected]", text: 'turtles' # Should see order cycle selected in dropdown # (Should also render products) diff --git a/spec/helpers/distributors_helper_spec.rb b/spec/helpers/distributors_helper_spec.rb deleted file mode 100644 index 3599f42962..0000000000 --- a/spec/helpers/distributors_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the DistributorsHelper. For example: -# -# describe DistributorsHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe DistributorsHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/order_cycles_helper_spec.rb b/spec/helpers/order_cycles_helper_spec.rb index df5c11982d..fce1095591 100644 --- a/spec/helpers/order_cycles_helper_spec.rb +++ b/spec/helpers/order_cycles_helper_spec.rb @@ -32,4 +32,17 @@ describe OrderCyclesHelper do helper.stub!(:current_distributor).and_return d helper.pickup_time.should == "turtles" end + + it "should give me the pickup time for any order cycle" do + d = create(:distributor_enterprise, name: 'Green Grass') + oc1 = create(:simple_order_cycle, name: 'oc 1', distributors: [d]) + oc2= create(:simple_order_cycle, name: 'oc 1', distributors: [d]) + + exchange = Exchange.find(oc2.exchanges.to_enterprises(d).outgoing.first.id) + exchange.update_attribute :pickup_time, "turtles" + + helper.stub!(:current_order_cycle).and_return oc1 + helper.stub!(:current_distributor).and_return d + helper.pickup_time(oc2).should == "turtles" + end end diff --git a/spec/helpers/shop_helper_spec.rb b/spec/helpers/shop_helper_spec.rb new file mode 100644 index 0000000000..39ef99ba65 --- /dev/null +++ b/spec/helpers/shop_helper_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' +describe ShopHelper do + + it "should build order cycle select options" do + d = create(:distributor_enterprise) + o1 = create(:simple_order_cycle, distributors: [d]) + helper.stub(:current_distributor).and_return d + + helper.order_cycles_name_and_pickup_times([o1]).should == [[helper.pickup_time(o1), o1.id]] + end +end diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 1f3f4a0ad2..bae4516447 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -30,6 +30,13 @@ describe OrderCycle do oc.exchanges.count.should == 3 end + it "gives me the outgoing exchange" do + d = create(:distributor_enterprise) + oc = create(:simple_order_cycle), distributors: [d]) + + oc.sender.should == d + end + it "finds order cycles in various stages of their lifecycle" do oc_active = create(:simple_order_cycle, orders_open_at: 1.week.ago, orders_close_at: 1.week.from_now) oc_not_yet_open = create(:simple_order_cycle, orders_open_at: 1.week.from_now, orders_close_at: 2.weeks.from_now)