Embody the spirit of halloween

This commit is contained in:
Matt Jankowski 2025-08-26 16:05:52 -04:00
parent c2b7b28919
commit 2b58c55a9c
5 changed files with 36 additions and 8 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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) %>

View File

@ -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