From 90d5759c23f6b6c2043756b5fa1e6bc8d4d2f134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 6 Sep 2022 18:20:12 +0200 Subject: [PATCH] Show the precise ending time unless it's midnight The reason why we were displaying the ending date as "one second before the actual ending" was that, when seeing that a phase ends at a date like "2000-12-31 00:00", we might end up thinking that the phase will finished at the midnight between December 31st and January the 1st, while it actually ends at the midnight between December the 30th and December the 31st. This is particularly important because we use a date field to select the date of a phase and if select December the 31st, it will be stored in the database as "2000-12-31 00:00". So, instead, in this case we display "2000-12-30 23:59", which is less confusing. But now we're going to add support for setting a time on polls, which means a certain poll might end at 15:30. In this case, displaying that it ends at 15:29 doesn't make much sense. --- app/components/admin/duration_component.rb | 6 +++++- .../components/admin/duration_component_spec.rb | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/components/admin/duration_component.rb b/app/components/admin/duration_component.rb index 417e8edd3..5a8f49cd6 100644 --- a/app/components/admin/duration_component.rb +++ b/app/components/admin/duration_component.rb @@ -16,6 +16,10 @@ class Admin::DurationComponent < ApplicationComponent end def end_time - durable.ends_at - 1.second if durable.ends_at.present? + if durable.ends_at.present? && durable.ends_at == durable.ends_at.beginning_of_day + durable.ends_at - 1.second + else + durable.ends_at + end end end diff --git a/spec/components/admin/duration_component_spec.rb b/spec/components/admin/duration_component_spec.rb index 9780df837..6271a3d31 100644 --- a/spec/components/admin/duration_component_spec.rb +++ b/spec/components/admin/duration_component_spec.rb @@ -9,9 +9,20 @@ describe Admin::DurationComponent do render_inline Admin::DurationComponent.new(durable) - expect(page.text).to eq "2015-08-01 12:00 - 2016-09-30 16:29" + expect(page.text).to eq "2015-08-01 12:00 - 2016-09-30 16:30" expect(page).to have_css "time", exact_text: "2015-08-01 12:00" - expect(page).to have_css "time", exact_text: "2016-09-30 16:29" + expect(page).to have_css "time", exact_text: "2016-09-30 16:30" + end + + it "shows the moment before the end date when it ends at midnight" do + durable = double( + starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), + ends_at: Time.zone.local(2016, 9, 30, 00, 00, 00) + ) + + render_inline Admin::DurationComponent.new(durable) + + expect(page.text).to eq "2015-08-01 12:00 - 2016-09-29 23:59" end it "shows the start date when no end date is defined" do @@ -27,6 +38,6 @@ describe Admin::DurationComponent do render_inline Admin::DurationComponent.new(durable) - expect(page.text).to eq "- 2016-09-30 16:29" + expect(page.text).to eq "- 2016-09-30 16:30" end end