Browse Source

Include sundays in open_this_week attribute.

This was not the case due to an ugly off-by-one error.
tags/0.3.0^2
Bèr Kessels 6 months ago
parent
commit
7a62e2e608

+ 19
- 9
app/models/place.rb View File

@@ -65,28 +65,38 @@ module Hours
def get_intervals_as_week(date_ranges, date_in_week)
from = beginning_of_week(date_in_week)
to = end_of_week(date_in_week)
# Ensure we always get an array to append to.
as_week = Hash.new { |hash, key| hash[key] = [] }

this_week = ITERATOR.get_time_iterator(date_ranges).select do |interval|
interval[:start] >= from && interval[:end] <= to
end

this_week.each do |interval|
as_week = european_week.dup
intervals_for(date_ranges, from, to).each do |interval|
as_week[interval[:start].wday] << interval
end

as_week
end

def intervals_for(date_ranges, from, to)
ITERATOR.get_time_iterator(date_ranges).select do |interval|
interval[:start] >= from && interval[:end] <= to
end
end

# Sets up an empty 7-day week template
def european_week
week = {}
[1, 2, 3, 4, 5, 6, 0].each do |wday|
week[wday] = []
end
week
end

def beginning_of_week(date)
days_to_monday = date.wday != 0 ? date.wday - 1 : 6
(date - days_to_monday).to_time
end

def end_of_week(date)
days_to_sunday = date.wday != 0 ? 7 - date.wday : 0
(date + days_to_sunday).to_time
days_to_next_monday = date.wday != 0 ? 8 - date.wday : 1
(date + days_to_next_monday).to_time - 1
end
end
end

+ 6
- 0
test/fixtures/output/hm_burchtstraat.json View File

@@ -44,6 +44,12 @@
"start": "1989-11-11 09:30:00 +0100",
"end": "1989-11-11 17:30:00 +0100"
}
],
"0": [
{
"start": "1989-11-12 12:00:00 +0100",
"end": "1989-11-12 17:30:00 +0100"
}
]
},
"raw_opening_hours": "Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30; Su 12:00-17:30",

+ 4
- 0
test/fixtures/output/places.json View File

@@ -42,6 +42,10 @@
"6": [{
"start" : "1989-11-11 09:30:00 +0100",
"end" : "1989-11-11 17:30:00 +0100"
}],
"0": [{
"start": "1989-11-12 12:00:00 +0100",
"end": "1989-11-12 17:30:00 +0100"
}]
},
"name" : "H&M",

+ 1
- 0
test/integration/web/view_places_test.rb View File

@@ -36,6 +36,7 @@ describe 'web view places' do

within('table#opening_hours') do
page.assert_selector('tr', text: 'Ma 10:00 18:00')
page.assert_selector('tr', text: 'Zo 12:00 17:30')

# Current day is Thursday. Current Day is bold
assert_equal 4, die_wende.wday

+ 62
- 2
test/models/place_test.rb View File

@@ -65,13 +65,73 @@ describe Hours::Models::Place do
end

describe 'opening_hours' do
let(:opening_hours) { '1989 Mo-Fr 10:00-19:00' }
let(:opening_hours) { '1989 Mo-Fr 10:00-12:30,13:00-19:00' }

let(:first_start) { Time.parse('1989-11-06 10:00:00 +0100') }

before do
subject.opening_hours = opening_hours
end

describe 'status' do
describe '#open_this_week' do
it 'has all 7 days' do
at_time do
assert_equal 7, subject.open_this_week.length
assert_equal [1, 2, 3, 4, 5, 6, 0], subject.open_this_week.keys
end
end

it 'has a start and end entry for each timerange' do
at_time do
# Sanity check: it should be wednesday
assert_equal 4, Date.today.wday
assert_equal 2, subject.open_this_week[4].length
assert_equal(
[{
start: Time.parse('1989-11-09 10:00:00 +0100'),
end: Time.parse('1989-11-09 12:30:00 +0100')
}, {
start: Time.parse('1989-11-09 13:00:00 +0100'),
end: Time.parse('1989-11-09 19:00:00 +0100')
}],
subject.open_this_week[4]
)
end
end

it 'correctly identifies the beginning of this week' do
at_time do
assert_equal 4, Date.today.wday
assert_equal first_start, subject.open_this_week[1][0][:start]
end
# When it is monday, we must still correctly identify this week
# Checking for fencepost errors
at_time(Time.parse('1989-11-06 00:00:00')) do
assert_equal 1, Date.today.wday
assert_equal first_start, subject.open_this_week[1][0][:start]
end
end

it 'correctly identifies the end of this week' do
at_time do
assert_equal 4, Date.today.wday
# Sunday has no entries
assert_nil subject.open_this_week[0][0]
end
# When it is the last second of sunday, we must still correctly
# identify this week.
# Checking for fencepost errors
at_time(Time.parse('1989-11-12 23:59:59')) do
assert_equal 0, Date.today.wday
# Still no entries for sunday
assert_nil subject.open_this_week[0][0]
# But let's check that we are in the correct week by checking monday
assert_equal first_start, subject.open_this_week[1][0][:start]
end
end
end

describe '#status' do
it 'is :open between 10:00 and 19:00' do
at_time do
assert_time_after(Time.parse('10:00'))

+ 9
- 0
test/support/time_helpers.rb View File

@@ -14,6 +14,15 @@ module TimeHelpers

##
# Time Helper, returns a Time
# Time is the moment the Berlin Wall fell: 9-11-1989 18:57:00
#
# November 1989
# zo ma di wo do vr za
# 1 2 3 4
# > 5 6 7 8 9 10 11
# 12 13 14 15 16 17 18
# 19 20 21 22 23 24 25
# 26 27 28 29 30
def die_wende
Time.local(1989, 11, 9, 18, 57, 0, 0)
end

Loading…
Cancel
Save