Skip to content

Commit 9508e3c

Browse files
authored
Validate BetterTogether event times (#1030)
## Summary - require event start time and ensure end time follows - mark start time required in event form - translate end-before-start validation message ## Testing - `bundle exec rubocop` *(fails: command not found)* - `bundle exec brakeman -q -w2` *(fails: command not found)* - `bundle exec bundler-audit --update` *(fails: command not found)* - `bin/codex_style_guard` *(fails: command not found)* - `bin/ci` *(fails: command not found)* ------ https://chatgpt.com/codex/tasks/task_e_689b7c1e48fc8321b2cd711f590678b1
2 parents 261deed + 1add307 commit 9508e3c

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed

app/models/better_together/event.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ class Event < ApplicationRecord
2727
translates :description, backend: :action_text
2828

2929
validates :name, presence: true
30+
validates :starts_at, presence: true
3031
validates :registration_url, format: { with: URI::DEFAULT_PARSER.make_regexp(%w[http https]) }, allow_blank: true,
3132
allow_nil: true
33+
validate :ends_at_after_starts_at
3234

3335
scope :draft, lambda {
3436
start_query = arel_table[:starts_at].eq(nil)
@@ -74,5 +76,14 @@ def to_s
7476
end
7577

7678
configure_attachment_cleanup
79+
80+
private
81+
82+
def ends_at_after_starts_at
83+
return if ends_at.blank? || starts_at.blank?
84+
return if ends_at > starts_at
85+
86+
errors.add(:ends_at, I18n.t('errors.models.ends_at_before_starts_at'))
87+
end
7788
end
7889
end

app/views/better_together/events/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<div class="row">
101101
<!-- Start Datetime Field -->
102102
<div class="col-6 mb-3 pb-3 border-bottom">
103-
<%= form.label :starts_at, t('better_together.events.labels.starts_at') %>
103+
<%= required_label form, :starts_at, class: 'form-label' %>
104104
<%= form.datetime_field :starts_at, include_seconds: false, class: 'form-control' %>
105105
<% if event.errors[:starts_at].any? %>
106106
<div class="invalid-feedback">

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,7 @@ en:
15161516
protected_destroy: "This record is protected and cannot be destroyed."
15171517
address_missing_type: "Address must be either physical, postal, or both."
15181518
host_single: "can only be set for one record"
1519+
ends_at_before_starts_at: "must be after the start time"
15191520
person_block:
15201521
cannot_block_self: "cannot block yourself"
15211522
cannot_block_manager: "cannot be a platform manager"

config/locales/es.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,7 @@ es:
15151515
protected_destroy: "Este registro está protegido y no se puede eliminar."
15161516
address_missing_type: "La dirección debe ser física, postal o ambas."
15171517
host_single: "solo se puede establecer para un registro"
1518+
ends_at_before_starts_at: "debe ser después de la hora de inicio"
15181519
person_block:
15191520
cannot_block_self: "no puedes bloquearte a ti mismo"
15201521
cannot_block_manager: "no puede ser un administrador de la plataforma"

config/locales/fr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ fr:
15371537
protected_destroy: "Cet enregistrement est protégé et ne peut pas être supprimé."
15381538
address_missing_type: "L'adresse doit être physique, postale ou les deux."
15391539
host_single: "ne peut être défini que pour un seul enregistrement"
1540+
ends_at_before_starts_at: "doit être après l'heure de début"
15401541
person_block:
15411542
cannot_block_self: "ne peut pas vous bloquer vous-même"
15421543
cannot_block_manager: "ne peut pas être un gestionnaire de plateforme"

spec/models/better_together/event_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,27 @@
44

55
module BetterTogether
66
RSpec.describe Event, type: :model do
7+
subject(:event) { described_class.new(name: 'Event', starts_at: Time.current) }
8+
79
it 'exists' do
810
expect(described_class).to be
911
end
12+
13+
it 'requires starts_at' do
14+
event.starts_at = nil
15+
expect(event).not_to be_valid
16+
expect(event.errors[:starts_at]).to include("can't be blank")
17+
end
18+
19+
it 'requires ends_at to be after starts_at' do
20+
event.ends_at = event.starts_at - 1.hour
21+
expect(event).not_to be_valid
22+
expect(event.errors[:ends_at]).to include(I18n.t('errors.models.ends_at_before_starts_at'))
23+
end
24+
25+
it 'is valid when ends_at is after starts_at' do
26+
event.ends_at = event.starts_at + 1.hour
27+
expect(event).to be_valid
28+
end
1029
end
1130
end

0 commit comments

Comments
 (0)