From 3b1705a406e791fa598a946abe4d9355af53cba6 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 9 Mar 2026 05:38:00 -0400 Subject: [PATCH] Add `TermsOfService#usable_effective_date` to DRY up view logic (#38095) --- app/models/terms_of_service.rb | 4 ++++ .../show.html.haml | 7 +++++-- .../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, 29 insertions(+), 4 deletions(-) diff --git a/app/models/terms_of_service.rb b/app/models/terms_of_service.rb index 41afaf10d9c..7fb025ea3fd 100644 --- a/app/models/terms_of_service.rb +++ b/app/models/terms_of_service.rb @@ -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 diff --git a/app/views/terms_of_service_interstitial/show.html.haml b/app/views/terms_of_service_interstitial/show.html.haml index c7fb3595f3b..4fed5eba932 100644 --- a/app/views/terms_of_service_interstitial/show.html.haml +++ b/app/views/terms_of_service_interstitial/show.html.haml @@ -6,8 +6,11 @@ .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)) + %p.lead< + - if @terms_of_service.usable_effective_date.past? + = t('terms_of_service_interstitial.past_preamble_html') + - else + = 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