From fb22b5e2b7c932edf0886233c387d299eb1fe404 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 24 Oct 2013 10:32:30 +1100 Subject: [PATCH] When there are no order cycles open, display an 'orders closed' message --- app/controllers/base_controller.rb | 1 + app/models/order_cycle.rb | 3 +++ app/views/order_cycles/_selection.html.haml | 15 +++++++++++++++ spec/features/consumer/order_cycles_spec.rb | 16 ++++++++++++++++ spec/models/order_cycle_spec.rb | 8 ++++++++ 5 files changed, 43 insertions(+) diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index dcf4797448..a1717a3d2c 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -1,6 +1,7 @@ class BaseController < ApplicationController include Spree::Core::ControllerHelpers include Spree::Core::ControllerHelpers::RespondWith + include OrderCyclesHelper helper 'spree/base' diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index bfdf29ed66..a7961e5928 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -25,6 +25,9 @@ class OrderCycle < ActiveRecord::Base joins(:exchanges).merge(Exchange.outgoing).where('exchanges.receiver_id = ?', distributor) } + scope :most_recently_closed, lambda { + where('orders_close_at < ?', Time.now).order('orders_close_at DESC') + } scope :managed_by, lambda { |user| if user.has_spree_role?('admin') diff --git a/app/views/order_cycles/_selection.html.haml b/app/views/order_cycles/_selection.html.haml index 73f351ded1..5fe769894a 100644 --- a/app/views/order_cycles/_selection.html.haml +++ b/app/views/order_cycles/_selection.html.haml @@ -13,6 +13,21 @@ .countdown-panel %h1= distance_of_time_in_words_to_now(current_order_cycle.orders_close_at) + - elsif order_cycle_options.empty? + .columns.two= image_tag 'pickup.png' + .columns.nine + %h2 Orders are currently closed for this hub + %p + - if OrderCycle.most_recently_closed.present? + The last cycle closed + = distance_of_time_in_words_to_now OrderCycle.most_recently_closed.first.orders_close_at + ago. + Please contact your hub directly to see if they accept late orders, or wait until the next cycle opens. + %p + = "Email: #{current_distributor.email}" if current_distributor.email + %br/ + = "Phone: #{current_distributor.phone}" if current_distributor.phone + - else .columns.two= image_tag 'pickup.png' diff --git a/spec/features/consumer/order_cycles_spec.rb b/spec/features/consumer/order_cycles_spec.rb index 9f99bfd3cf..5ea99f7690 100644 --- a/spec/features/consumer/order_cycles_spec.rb +++ b/spec/features/consumer/order_cycles_spec.rb @@ -45,6 +45,22 @@ feature %q{ page.should have_content 'Your order will be ready on' end + scenario "when there are no available order cycles" do + Timecop.freeze do + d = create(:distributor_enterprise, name: 'Green Grass') + oc1 = create(:simple_order_cycle, name: 'oc 1', distributors: [d], orders_close_at: 5.minutes.ago) + + visit spree.root_path + click_link d.name + + page.should have_content "Orders are currently closed for this hub" + page.should have_content "The last cycle closed 5 minutes ago." + page.should have_content "Please contact your hub directly to see if they accept late orders, or wait until the next cycle opens." + page.should have_content d.email + page.should have_content d.phone + end + end + scenario "changing order cycle", js: true do s = create(:supplier_enterprise) diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 8777f2ecba..dca452ac9e 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -81,6 +81,14 @@ describe OrderCycle do end end + it "finds the most recently closed order cycles" do + oc1 = create(:order_cycle, orders_close_at: 2.hours.ago) + oc2 = create(:order_cycle, orders_close_at: 1.hour.ago) + oc3 = create(:order_cycle, orders_close_at: 1.hour.from_now) + + OrderCycle.most_recently_closed.should == [oc2, oc1] + end + describe "finding order cycles with a particular distributor" do let(:c) { create(:supplier_enterprise) } let(:d) { create(:distributor_enterprise) }