From 9f150c8d87fb3597a5668c268c0b46f3adfff209 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 1 Nov 2013 16:12:31 +1100 Subject: [PATCH] Adding a small display on the distributor page indicating when the next order cycle is, if any - BugHerd#127 --- app/models/order_cycle.rb | 12 ++++++- app/views/order_cycles/_selection.html.haml | 6 ++++ spec/features/consumer/order_cycles_spec.rb | 39 ++++++++++++++------- spec/models/order_cycle_spec.rb | 21 +++++++++++ 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index a7961e5928..505985ba78 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -26,9 +26,14 @@ class OrderCycle < ActiveRecord::Base } scope :most_recently_closed, lambda { - where('orders_close_at < ?', Time.now).order('orders_close_at DESC') + where('order_cycles.orders_close_at < ?', Time.now).order('order_cycles.orders_close_at DESC') } + scope :soonest_opening, lambda { + where('order_cycles.orders_open_at > ?', Time.now).order('order_cycles.orders_open_at ASC') + } + + scope :managed_by, lambda { |user| if user.has_spree_role?('admin') scoped @@ -53,6 +58,9 @@ class OrderCycle < ActiveRecord::Base joins('LEFT OUTER JOIN enterprises ON (enterprises.id = exchanges.sender_id OR enterprises.id = exchanges.receiver_id)') } + def self.first_opening_for(distributor) + with_distributor(distributor).soonest_opening.first + end def clone! oc = self.dup @@ -116,6 +124,8 @@ class OrderCycle < ActiveRecord::Base end + + private # -- Fees diff --git a/app/views/order_cycles/_selection.html.haml b/app/views/order_cycles/_selection.html.haml index 5fe769894a..f07e1a4984 100644 --- a/app/views/order_cycles/_selection.html.haml +++ b/app/views/order_cycles/_selection.html.haml @@ -23,6 +23,12 @@ = 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. + + - if next_oc = OrderCycle.first_opening_for(@enterprise) + %p + The next order cycle opens in + = distance_of_time_in_words_to_now next_oc.orders_open_at + %p = "Email: #{current_distributor.email}" if current_distributor.email %br/ diff --git a/spec/features/consumer/order_cycles_spec.rb b/spec/features/consumer/order_cycles_spec.rb index c34eabaecf..0ffdba9db4 100644 --- a/spec/features/consumer/order_cycles_spec.rb +++ b/spec/features/consumer/order_cycles_spec.rb @@ -46,24 +46,39 @@ 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') + context "when there are no available order cycles" do + let(:d) { create(:distributor_enterprise, name: 'Green Grass') } + before do create_enterprise_group_for d - oc1 = create(:simple_order_cycle, name: 'oc 1', distributors: [d], orders_close_at: 5.minutes.ago) - visit spree.root_path - click_link d.name + end + it "indicates there are no current order cycles" do + Timecop.freeze do + oc1 = create(:simple_order_cycle, name: 'oc 1', distributors: [d], orders_close_at: 5.minutes.ago) + 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 + 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 + context "displaying future order cycles" do + it "should show the time until the next order cycle opens" do + create(:simple_order_cycle, name: 'oc 1', distributors: [d], orders_open_at: 10.days.from_now, orders_close_at: 11.days.from_now) + + click_link d.name + page.should have_content "The next order cycle opens in 10 days" + end + + it "should show nothing when there is no next order cycle" do + click_link d.name + page.should_not have_content "The next order cycle opens" + end end end - scenario "changing order cycle", js: true do s = create(:supplier_enterprise) d = create(:distributor_enterprise, name: 'Green Grass') diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index dca452ac9e..d0b0831e7c 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -89,6 +89,14 @@ describe OrderCycle do OrderCycle.most_recently_closed.should == [oc2, oc1] end + it "finds the soonest opening order cycles" do + oc1 = create(:order_cycle, orders_open_at: 1.weeks.from_now) + oc2 = create(:order_cycle, orders_open_at: 2.hours.from_now) + oc3 = create(:order_cycle, orders_open_at: 1.hour.ago) + + OrderCycle.soonest_opening.should == [oc2, oc1] + end + describe "finding order cycles with a particular distributor" do let(:c) { create(:supplier_enterprise) } let(:d) { create(:distributor_enterprise) } @@ -305,4 +313,17 @@ describe OrderCycle do oc.send(:adjustment_label_for, line_item, enterprise_fee, 'distributor').should == "Bananas - packing fee by distributor Ballantyne" end end + + describe "finding order cycles opening in the future" do + it "should give the soonest opening order cycle for a distributor" do + distributor = create(:distributor_enterprise) + oc = create(:simple_order_cycle, name: 'oc 1', distributors: [distributor], orders_open_at: 10.days.from_now, orders_close_at: 11.days.from_now) + OrderCycle.first_opening_for(distributor).should == oc + end + + it "should return no order cycle when none are impending" do + distributor = create(:distributor_enterprise) + OrderCycle.first_opening_for(distributor).should == nil + end + end end