Skip to content

Commit

Permalink
refactored next delivery slot code to deal with day + 1 situation
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmt committed May 2, 2014
1 parent 475b881 commit ecbcb44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
42 changes: 18 additions & 24 deletions app/helpers/spree_delivery_options/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,26 @@ def current_order_cutoff_time

def next_delivery_slot
current_time_string = Time.zone.now.strftime("%H:%M")
first_possible_delivery_day = Date.current + 1.day

if delivery_options_for_time(current_time_string).empty?
first_possible_delivery_day += 1.day
current_time_string = "00:01"
end
return "" if delivery_options_for_time(current_time_string).empty?

delivery_day = next_available_day(first_possible_delivery_day)

"#{delivery_day.strftime('%A').titleize} between #{delivery_options_for_date_and_time(delivery_day, current_time_string).first}"
end

def next_available_day(current_day)
next_available_day = nil
counter = 0
until next_available_day || counter > 7 do
unless all_delivery_options_for_date(current_day).empty?
next_available_day = current_day
break
else
current_day = current_day + 1.day
counter = counter + 1
possible_delivery_day = Date.current + 1.day

possible_delivery_day_options = delivery_options_for_date_and_time(possible_delivery_day, current_time_string)
if !delivery_options_for_date_and_time(possible_delivery_day, current_time_string).empty?
return "#{possible_delivery_day.strftime('%A').titleize} between #{possible_delivery_day_options.first}"
else
counter = 0
until counter > 7 do
possible_delivery_day = possible_delivery_day + 1.day
current_time_string = "00:01"
possible_delivery_day_options = delivery_options_for_date_and_time(possible_delivery_day, current_time_string)

if !possible_delivery_day_options.empty?
return "#{possible_delivery_day.strftime('%A').titleize} between #{possible_delivery_day_options.first}"
end
counter += 1
end
end
next_available_day

""
end

end
Expand Down
9 changes: 7 additions & 2 deletions spec/helpers/spree_delivery_options/base_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@
end

it 'should skip day if deliveries are not available' do
time_now = DateTime.parse("13/03/2014 12:00 +1100")
SpreeDeliveryOptions::Config.delivery_time_options = [{
"13:15" => {monday: ['6am to 7:30am'], tuesday: ['6am to 7:30am'], wednesday: ['6am to 7:30am'], thursday: []},
"20:00" => {"11/03/2014" => ['9am to 12am'], tuesday: ['6pm to 7:30pm']}
}].to_json

time_now = DateTime.parse("02/05/2014 14:00 +1100")
Timecop.freeze(time_now)

helper.next_delivery_slot.should == 'Tuesday between 6am to 7:30am'
helper.next_delivery_slot.should == 'Monday between 6am to 7:30am'
end

it 'should return nil if no delivery times are available' do
Expand Down

0 comments on commit ecbcb44

Please sign in to comment.