Skip to content

Commit

Permalink
Merge pull request #312 from a-thousand-channels/ut/307-date-form-han…
Browse files Browse the repository at this point in the history
…dling-improved

Date handling in Places form (#307)
  • Loading branch information
ut authored Mar 11, 2024
2 parents 682543c + a873a11 commit f36f9b5
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ GEM
rack (>= 0.4)
rack-attack (6.7.0)
rack (>= 1.0, < 4)
rack-cors (2.0.1)
rack-cors (2.0.2)
rack (>= 2.0.0)
rack-test (2.1.0)
rack (>= 1.3)
Expand Down
4 changes: 2 additions & 2 deletions app/assets/stylesheets/1_basics.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ span.confirmation {
}

span.name, label {
color: #888;
color: #775;
font-size: 90%;
text-transform: uppercase;
text-transform: initial;
font-weight: bold;
padding-right: 10px;
line-height: 1.2;
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def smart_date_display(startdate, enddate)
if startdate.strftime('%H:%M') == '00:00'
if startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '01.01'
"#{startdate.strftime('%Y')}#{enddate.strftime('%Y')}"
elsif (startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '31.12') && (startdate.strftime('%Y') == enddate.strftime('%Y'))
startdate.strftime('%Y').to_s
else
"#{startdate.strftime('%d.%m.%y')}#{enddate.strftime('%d.%m.%y')}"
end
Expand Down
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
4 changes: 3 additions & 1 deletion app/views/places/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
.small-10.cell
= f.input :title
.grid-x.grid-padding-x
.small-12.cell
= f.input :subtitle
%hr
.grid-x.grid-padding-x
Expand All @@ -125,7 +128,6 @@
.large-6.medium-6.small-5.cell
= f.input :enddate_time, as: :time, html5: true, :label => 'End time', :input_html => {:size => 5, class: 'input-time'}
%hr
= f.input :subtitle
= tinymce
= f.input :teaser, :label => 'Teaser', :input_html => {:rows => 8, class: 'large-text tinymce' }
= f.input :text, :label => 'Text', :input_html => {:rows => 16, class: 'large-text tinymce'}
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,6 +27,16 @@
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 }
Expand Down
55 changes: 45 additions & 10 deletions spec/models/place_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,41 @@
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 range' 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 All @@ -36,11 +68,14 @@
expect(p.date).to eq('01.01.18, 20:30')
end

it 'returns full startdate without time' do
it 'returns year (with only startdate given)' do
p = FactoryBot.create(:place, startdate_date: '2018-01-01', startdate_time: '', enddate: '')
expect(p.date).to eq('2018')
end

it 'returns year' do
p = FactoryBot.create(:place, startdate_date: '2018-01-01', startdate_time: '', enddate: '2018-12-31')
expect(p.date).to eq('2018')
end
it 'returns full date range with time' do
p = FactoryBot.create(:place, startdate_date: '2018-01-01', startdate_time: '12:00', enddate_date: '2018-01-01', enddate_time: '18:00')
expect(p.date).to eq('01.01.18, 12:00 ‒ 18:00')
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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
'127.0.0.1',
'chromedriver.storage.googleapis.com',
'googlechromelabs.github.io',
'storage.googleapis.com',
'edgedl.me.gvt1.com'
])

# special setup to make feature tests run on ubuntu 20.4 LTS
if ENV['UBUNTU']
puts 'Running Rspecs on Ubuntu'
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 f36f9b5

Please sign in to comment.