Skip to content

Commit

Permalink
Allow start/enddate deletion, some rspecs (refs #307)
Browse files Browse the repository at this point in the history
  • Loading branch information
ut committed Apr 8, 2024
1 parent c9669fb commit cafa27d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 17 deletions.
5 changes: 5 additions & 0 deletions app/models/place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/public/layers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/places.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 43 additions & 8 deletions spec/models/place_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])
Expand All @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions spec/support/api/layer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions spec/support/api/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":[
Expand All @@ -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":[
Expand Down
4 changes: 2 additions & 2 deletions spec/support/api/map_allplaces.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit cafa27d

Please sign in to comment.