Browse Source

Turn status into :open, :closed or :unknown triplet.

tags/0.3.0^2
Bèr Kessels 1 year ago
parent
commit
eb99f85d6a

+ 4
- 2
app/models/place.rb View File

@@ -28,9 +28,11 @@ module Hours
end

def status
ITERATOR.is_opened?(opening_hours)
return :unknown if opening_hours&.empty?

ITERATOR.is_opened?(opening_hours) ? :open : :closed
rescue OpeningHoursConverter::ParseError
false
:unknown
end

def open_this_week

+ 1
- 3
app/views/place.erb View File

@@ -17,9 +17,7 @@
<div class="col" itemscope itemtype="http://schema.org/PostalAddress">
<h2 class="title" itemprop="name">
<%= @place.name %>
<span class="badge badge-<%= @place.status ? 'success' : 'danger' %>">
<%= @place.status ? 'Open' : 'Gesloten' %>
</span>
<%== erb :status_badge_partial, locals: { status: @place.status } %>
</h2>
<address itemprop="address">
<span itemprop="streetAddress">

+ 19
- 9
app/views/region.erb View File

@@ -26,6 +26,15 @@
shadowSize: [41, 41],
shadowAnchor: [14, 41]
});"
js += "var unknownIcon = L.icon({
iconUrl: '/images/marker-icon-unknown.png',
iconSize: [31, 41],
iconAnchor: [15, 41],
popupAnchor: [0, -28],
shadowUrl: '/images/marker-shadow.png',
shadowSize: [41, 41],
shadowAnchor: [14, 41]
});"
js += "var centerIcon = L.icon({
iconUrl: '/images/location-icon.png',
iconSize: [40, 40],
@@ -37,12 +46,15 @@
js += "var markers = [];"

@region.each do |place|
if place.status
icon = 'openedIcon'
else
icon = 'closedIcon'
end
popup_content = "<strong>#{Rack::Utils.escape_html(place.name)}</strong> <span class=\"badge badge-#{place.status ? 'success' : 'danger' }\">#{place.status ? 'Open' : 'Gesloten'}</span>"
icon = if place.status == :open
'openedIcon'
elsif place.status == :closed
'closedIcon'
else
'unknownIcon'
end
popup_content = "<strong>#{Rack::Utils.escape_html(place.name)}</strong>"
popup_content += erb(:status_badge_partial, locals: { status: place.status }).delete("\n")
js += "markers.push(L.marker([#{place.location.y}, #{place.location.x}], {icon: #{icon}}).addTo(map).bindPopup('#{popup_content}'));"
end
js += "var group = L.featureGroup(markers);"
@@ -61,9 +73,7 @@
</div>
<p class="mb-1"><%= place.address.street %>, <%= place.address.city %></p>
<p class="mb-1">
<span class="badge badge-<%= place.status ? 'success' : 'danger' %>">
<%= place.status ? 'Open' : 'Gesloten' %>
</span>
<%== erb :status_badge_partial, locals: { status: place.status } %>
<%= place.opening_hours %>
</p>
</a>

+ 7
- 0
app/views/status_badge_partial.erb View File

@@ -0,0 +1,7 @@
<% if status == :open %>
<span class="badge badge-success">Open</span>
<% elsif status == :closed %>
<span class="badge badge-danger">Gesloten</span>
<% else %>
<span class="badge badge-secondary">Onbekend</span>
<% end %>

BIN
public/images/marker-icon-unknown.png View File


+ 1
- 1
test/fixtures/output/hm_burchtstraat.json View File

@@ -7,7 +7,7 @@
"name": "H&M",
"lat": 51.8473397,
"lon": 5.8653234,
"status": true,
"status": "open",
"open_this_week": {
"1": [
{

+ 1
- 1
test/fixtures/output/places.json View File

@@ -45,7 +45,7 @@
}]
},
"name" : "H&M",
"status" : true,
"status" : "open",
"lat": 51.8473397,
"lon": 5.8653234,
"address" : {

+ 2
- 2
test/integration/api/view_places_test.rb View File

@@ -75,7 +75,7 @@ describe 'api view places' do
expected = {
"data": [
{
attributes: { status: false }.ignore_extra_keys!
attributes: { status: 'closed' }.ignore_extra_keys!
}.ignore_extra_keys!
]
}.ignore_extra_keys!
@@ -120,7 +120,7 @@ describe 'api view places' do
expected = {
'data' => {
'attributes' => {
'status' => false, # Should default to false
'status' => 'unknown', # Should default to false
'open_this_week' => {} # Should defailt to an empty week hash
}.ignore_extra_keys!
}.ignore_extra_keys!

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

@@ -46,6 +46,19 @@ describe 'web view places' do
end
end

describe 'GET /places/:uuid with unknown Opening Hours' do
let(:input) { [json_fixtures('input/marienburg_parking.json')] }

it 'on a thursday, after 21:00 it is closed' do
assert_nil input.first[:properties][:opening_hours]
Timecop.travel(die_wende) do
visit "/places/#{hm_id}"
# A badge with class secondary means unknown.
page.assert_selector('span.badge-secondary', text: 'Onbekend')
end
end
end

describe 'GET /places/:uuid with unescaped values' do
let(:input) { [json_fixtures('input/hacked.json')] }


+ 10
- 4
test/models/place_test.rb View File

@@ -64,21 +64,27 @@ describe Hours::Models::Place do
end

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

assert_equal true, subject.status
assert_equal :open, subject.status
end
end

it 'is false after 19:00' do
it 'is :closed after 19:00' do
at_time(die_wende + 3600) do
assert_time_after(Time.parse('19:00'))
assert_equal false, subject.status
assert_equal :closed, subject.status
end
end

it 'is :unknown without opening_hours' do
subject.opening_hours = ''
assert_time_after(Time.parse('19:00'))
assert_equal :unknown, subject.status
end
end
end
end

Loading…
Cancel
Save