From 2b58c55a9cb4f028c7dfa39e5e52c1921ddf33bb Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 26 Aug 2025 16:05:52 -0400 Subject: [PATCH] Embody the spirit of halloween --- app/models/terms_of_service.rb | 16 ++++++++++++---- .../show.html.haml | 6 ++++-- .../terms_of_service_changed.html.haml | 2 +- .../terms_of_service_changed.text.erb | 2 +- spec/models/terms_of_service_spec.rb | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/app/models/terms_of_service.rb b/app/models/terms_of_service.rb index 41afaf10d9c..8a51940858f 100644 --- a/app/models/terms_of_service.rb +++ b/app/models/terms_of_service.rb @@ -23,7 +23,7 @@ class TermsOfService < ApplicationRecord validates :changelog, :effective_date, presence: true, if: -> { published? } validates :effective_date, uniqueness: true - validate :effective_date_cannot_be_in_the_past + validate :effective_date_cannot_be_in_the_past, if: :effective_date? NOTIFICATION_ACTIVITY_CUTOFF = 1.year.freeze @@ -31,6 +31,10 @@ class TermsOfService < ApplicationRecord live.first || upcoming.first # For the case when none of the published terms have become effective yet end + def usable_effective_date + effective_date || Time.zone.today + end + def published? published_at.present? end @@ -66,10 +70,14 @@ class TermsOfService < ApplicationRecord private def effective_date_cannot_be_in_the_past - return if effective_date.blank? + errors.add(:effective_date, :too_soon, date: minimum_allowed_effective_date) if effective_date_too_early? + end - min_date = TermsOfService.live.pick(:effective_date) || Time.zone.today + def effective_date_too_early? + effective_date < minimum_allowed_effective_date + end - errors.add(:effective_date, :too_soon, date: min_date) if effective_date < min_date + def minimum_allowed_effective_date + self.class.live.pick(:effective_date) || Time.zone.today end end diff --git a/app/views/terms_of_service_interstitial/show.html.haml b/app/views/terms_of_service_interstitial/show.html.haml index c7fb3595f3b..37903fbbf77 100644 --- a/app/views/terms_of_service_interstitial/show.html.haml +++ b/app/views/terms_of_service_interstitial/show.html.haml @@ -6,8 +6,10 @@ .simple_form %h1.title= t('terms_of_service_interstitial.title', domain: site_hostname) - - effective_date = @terms_of_service.effective_date || Time.zone.today - %p.lead= effective_date.past? ? t('terms_of_service_interstitial.past_preamble_html') : t('terms_of_service_interstitial.future_preamble_html', date: l(effective_date)) + - if @terms_of_service.usable_effective_date.past? + %p.lead= t('terms_of_service_interstitial.past_preamble_html') + - else + %p.lead= t('terms_of_service_interstitial.future_preamble_html', date: l(@terms_of_service.usable_effective_date)) %p.lead= t('user_mailer.terms_of_service_changed.agreement', domain: site_hostname) diff --git a/app/views/user_mailer/terms_of_service_changed.html.haml b/app/views/user_mailer/terms_of_service_changed.html.haml index 2e34eb49901..efa49117439 100644 --- a/app/views/user_mailer/terms_of_service_changed.html.haml +++ b/app/views/user_mailer/terms_of_service_changed.html.haml @@ -9,7 +9,7 @@ %table.email-inner-card-table{ cellspacing: 0, cellpadding: 0, border: 0, role: 'presentation' } %tr %td.email-inner-card-td.email-prose - %p= t('user_mailer.terms_of_service_changed.description_html', path: terms_of_service_version_url(date: @terms_of_service.effective_date), domain: site_hostname, date: l(@terms_of_service.effective_date || Time.zone.today)) + %p= t('user_mailer.terms_of_service_changed.description_html', path: terms_of_service_version_url(date: @terms_of_service.effective_date), domain: site_hostname, date: l(@terms_of_service.usable_effective_date)) %p %strong= t('user_mailer.terms_of_service_changed.changelog') = markdown(@terms_of_service.changelog) diff --git a/app/views/user_mailer/terms_of_service_changed.text.erb b/app/views/user_mailer/terms_of_service_changed.text.erb index ccf332ce897..933be1122c1 100644 --- a/app/views/user_mailer/terms_of_service_changed.text.erb +++ b/app/views/user_mailer/terms_of_service_changed.text.erb @@ -2,7 +2,7 @@ === -<%= t('user_mailer.terms_of_service_changed.description', domain: site_hostname, date: l(@terms_of_service.effective_date || Time.zone.today)) %> +<%= t('user_mailer.terms_of_service_changed.description', domain: site_hostname, date: l(@terms_of_service.usable_effective_date)) %> => <%= terms_of_service_version_url(date: @terms_of_service.effective_date) %> diff --git a/spec/models/terms_of_service_spec.rb b/spec/models/terms_of_service_spec.rb index 16cd5dd2ebf..99a7caa6131 100644 --- a/spec/models/terms_of_service_spec.rb +++ b/spec/models/terms_of_service_spec.rb @@ -108,6 +108,24 @@ RSpec.describe TermsOfService do end end + describe '#usable_effective_date' do + subject { terms_of_service.usable_effective_date } + + let(:terms_of_service) { Fabricate.build(:terms_of_service, effective_date:) } + + context 'when effective_date value is set' do + let(:effective_date) { 5.days.ago } + + it { is_expected.to eq(effective_date.to_date) } + end + + context 'when effective_date value is not set' do + let(:effective_date) { nil } + + it { is_expected.to eq(Time.zone.today) } + end + end + describe '::current' do context 'when no terms exist' do it 'returns nil' do