diff --git a/app/models/place.rb b/app/models/place.rb index e5b09e10..b1af13ba 100644 --- a/app/models/place.rb +++ b/app/models/place.rb @@ -46,11 +46,16 @@ class Place < ApplicationRecord self.startdate = "#{startdate_date} #{startdate_time}" elsif startdate_date.present? self.startdate = "#{startdate_date} 00:00:00" + # nil from factories, blank from post request + elsif startdate_date.nil? || startdate_date.blank? + self.startdate = nil end if enddate_date.present? && enddate_time.present? self.enddate = "#{enddate_date} #{enddate_time}" elsif enddate_date.present? self.enddate = "#{enddate_date} 00:00:00" + elsif enddate_date.nil? || startdate_date.blank? + self.enddate = nil end end diff --git a/spec/controllers/public/layers_controller_spec.rb b/spec/controllers/public/layers_controller_spec.rb index 31467bf2..698ea39c 100644 --- a/spec/controllers/public/layers_controller_spec.rb +++ b/spec/controllers/public/layers_controller_spec.rb @@ -61,9 +61,9 @@ it 'returns sorted results a published layer, sorted by startdate' do layer = FactoryBot.create(:layer, :sorted_by_startdate, map_id: @map.id, published: true) - place1 = FactoryBot.create(:place, layer: layer, published: true, startdate: Time.now - 2.day) - place2 = FactoryBot.create(:place, layer: layer, published: true, startdate: Time.now - 4.day) - place3 = FactoryBot.create(:place, layer: layer, published: true, startdate: Time.now - 3.day) + place1 = FactoryBot.create(:place, layer: layer, published: true, startdate_date: Time.now - 2.day ) + place2 = FactoryBot.create(:place, layer: layer, published: true, startdate_date: Time.now - 4.day) + place3 = FactoryBot.create(:place, layer: layer, published: true, startdate_date: Time.now - 3.day) place4_notpublished = FactoryBot.create(:place, layer: layer, published: false) get :show, params: { id: layer.to_param, map_id: @map.id, format: 'json' }, session: valid_session diff --git a/spec/factories/places.rb b/spec/factories/places.rb index a2af1ecf..95d50aec 100644 --- a/spec/factories/places.rb +++ b/spec/factories/places.rb @@ -27,7 +27,17 @@ trait :date_and_time do startdate_date { '2018-04-30' } startdate_time { '11:45' } + enddate_date { '2022-05-30' } + enddate_time { '16:45' } end + trait :start_date_and_time do + startdate_date { '2018-04-30' } + startdate_time { '11:45' } + end + trait :end_date_and_time do + enddate_date { '2022-05-30' } + enddate_time { '16:45' } + end trait :invalid do title { nil } end diff --git a/spec/models/place_spec.rb b/spec/models/place_spec.rb index 1b71451e..a3fb6549 100644 --- a/spec/models/place_spec.rb +++ b/spec/models/place_spec.rb @@ -25,9 +25,44 @@ end end - describe 'Dates' do - it 'returns date' do + + describe 'Fulldates w/before_save handling' do + + it 'updates full date' do + p = FactoryBot.create(:place, startdate: '2018-01-01', enddate: '2018-01-02') + p.startdate_date = '2018-01-01' + p.enddate_date = '2018-12-31' + p.save! + p.reload + expect(p.startdate).to eq('2018-01-01 00:00:00.000') + expect(p.enddate).to eq('2018-12-31 00:00:00.000') + end + + it 'removes full date with nil' do + p = FactoryBot.create(:place, startdate: '2018-01-01', enddate: '2018-01-02') + p.startdate_date = '2018-01-01' + p.enddate_date = nil + p.save! + p.reload + expect(p.startdate).to eq('2018-01-01 00:00:00.000') + expect(p.enddate).to eq(nil) + end + + it 'removes full date with blank' do p = FactoryBot.create(:place, startdate: '2018-01-01', enddate: '2018-01-02') + p.startdate_date = '' + p.enddate_date = '2018-01-02' + p.save! + p.reload + expect(p.startdate).to eq(nil) + expect(p.enddate).to eq('2018-01-02 00:00:00.000') + end + end + + describe 'Dates for UI' do + + it 'returns date' do + p = FactoryBot.create(:place, startdate_date: '2018-01-01', enddate_date: '2018-01-02') expect(p.date).to eq('01.01.18 ‒ 02.01.18') end @@ -124,9 +159,9 @@ it 'is valid ' do m = FactoryBot.create(:map) l = FactoryBot.create(:layer, map: m) - event_1_in_the_middle = FactoryBot.create(:place, layer: l, startdate: '2016-01-01 00:00:00') - event_2_earlier = FactoryBot.create(:place, layer: l, startdate: '2011-01-01 00:00:00') - event_3_latest = FactoryBot.create(:place, layer: l, startdate: '2021-01-01 00:00:00') + event_1_in_the_middle = FactoryBot.create(:place, layer: l, startdate_date: '2016-01-01 00:00:00') + event_2_earlier = FactoryBot.create(:place, layer: l, startdate_date: '2011-01-01 00:00:00') + event_3_latest = FactoryBot.create(:place, layer: l, startdate_date: '2021-01-01 00:00:00') places = l.places expect(places).to eq([event_1_in_the_middle, event_2_earlier, event_3_latest]) @@ -137,9 +172,9 @@ it 'is valid ' do m = FactoryBot.create(:map) l = FactoryBot.create(:layer, map: m) - event_1_in_the_middle = FactoryBot.create(:place, layer: l, startdate: '2016-01-01 00:00:00') - event_2_earlier = FactoryBot.create(:place, layer: l, startdate: '2011-01-01 00:00:00') - event_3_latest = FactoryBot.create(:place, layer: l, startdate: '2021-01-01 00:00:00') + event_1_in_the_middle = FactoryBot.create(:place, layer: l, startdate_date: '2016-01-01 00:00:00') + event_2_earlier = FactoryBot.create(:place, layer: l, startdate_date: '2011-01-01 00:00:00') + event_3_latest = FactoryBot.create(:place, layer: l, startdate_date: '2021-01-01 00:00:00') places = l.places.sorted_by_startdate expect(places).not_to eq([event_1_in_the_middle, event_2_earlier, event_3_latest]) diff --git a/spec/support/api/layer.json b/spec/support/api/layer.json index 8d7b7d63..e9e50226 100644 --- a/spec/support/api/layer.json +++ b/spec/support/api/layer.json @@ -300,7 +300,7 @@ }, "startdate": { "$id": "#/properties/map/properties/layer/items/properties/places/items/properties/startdate", - "type": "string", + "type": ["string", "null"], "title": "The Startdate Schema", "default": "", "examples": [ @@ -310,7 +310,7 @@ }, "enddate": { "$id": "#/properties/map/properties/layer/items/properties/places/items/properties/enddate", - "type": "string", + "type": ["string", "null"], "title": "The Enddate Schema", "default": "", "examples": [ diff --git a/spec/support/api/map.json b/spec/support/api/map.json index 7355080c..f953abc2 100644 --- a/spec/support/api/map.json +++ b/spec/support/api/map.json @@ -390,7 +390,7 @@ }, "startdate":{ "$id":"#/properties/map/properties/layer/items/properties/places/items/properties/startdate", - "type":"string", + "type":["string", "null"], "title":"The Startdate Schema", "default":"", "examples":[ @@ -400,7 +400,7 @@ }, "enddate":{ "$id":"#/properties/map/properties/layer/items/properties/places/items/properties/enddate", - "type":"string", + "type":["string", "null"], "title":"The Enddate Schema", "default":"", "examples":[ diff --git a/spec/support/api/map_allplaces.json b/spec/support/api/map_allplaces.json index dc3b2217..8b133d27 100644 --- a/spec/support/api/map_allplaces.json +++ b/spec/support/api/map_allplaces.json @@ -152,11 +152,11 @@ }, "startdate": { "title": "The startdate Schema", - "type": "string" + "type": ["string", "null"] }, "enddate": { "title": "The enddate Schema", - "type": "string" + "type": ["string", "null"] }, "location": { "title": "The location Schema",