From 07c09ef0555f2f47f093ef64ea143011db708ffa Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 8 Sep 2023 15:47:03 +0200
Subject: [PATCH] Fix
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Pierre-André Boissinot <pierreandre.boissinot@noesya.coop>
---
 app/models/communication/website/agenda/event.rb   | 14 +++++++++++++-
 .../websites/agenda/events/static.html.erb         |  2 +-
 config/locales/communication/en.yml                |  6 ++++++
 config/locales/communication/fr.yml                |  6 ++++++
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/app/models/communication/website/agenda/event.rb b/app/models/communication/website/agenda/event.rb
index aca1072d5..d1f9e19fd 100644
--- a/app/models/communication/website/agenda/event.rb
+++ b/app/models/communication/website/agenda/event.rb
@@ -60,7 +60,8 @@ class Communication::Website::Agenda::Event < ApplicationRecord
   scope :ordered, -> { order(from_day: :desc, from_hour: :desc) }
   scope :recent, -> { ordered.limit(5) }
 
-  validates_presence_of :from_day
+  validates_presence_of :from_day, :title
+  validate :to_day_after_from_day, :to_hour_after_from_hour_on_same_day
 
   STATUS_FUTURE = 'future'
   STATUS_PRESENT = 'present'
@@ -116,4 +117,15 @@ class Communication::Website::Agenda::Event < ApplicationRecord
   def to_s
     "#{title}"
   end
+
+  protected
+
+  def to_day_after_from_day
+    errors.add(:to_day, :too_soon) if to_day.present? && to_day < from_day
+  end
+
+  def to_hour_after_from_hour_on_same_day
+    return if from_day != to_day
+    errors.add(:to_hour, :too_soon) if to_hour.present? && from_hour.present? && to_hour < from_hour
+  end
 end
diff --git a/app/views/admin/communication/websites/agenda/events/static.html.erb b/app/views/admin/communication/websites/agenda/events/static.html.erb
index 691753777..932c37d15 100644
--- a/app/views/admin/communication/websites/agenda/events/static.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/static.html.erb
@@ -12,7 +12,7 @@ dates:
     hour: <%= @about.from_hour.strftime "%H:%M" %>
 <% end %>
   to:
-    day: <%= @about.to_day || @about.from_day%>
+    day: <%= @about.to_day || @about.from_day %>
 <% if @about.to_hour %>
     hour: <%= @about.to_hour.strftime "%H:%M" %>
 <% end %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 3153f83b4..fcced216b 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -233,6 +233,12 @@ en:
             languages:
               must_include_default: must include at least the default language
               too_short: must include at least one
+        communication/website/agenda/event:
+          attributes:
+            to_day:
+              too_soon: must be after "from day"
+            to_hour:
+              too_soon: must be after "from hour"
   admin:
     communication:
       blocks:
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 518e1c623..83f21d2b6 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -233,6 +233,12 @@ fr:
             languages:
               must_include_default: doivent contenir la langue par défaut
               too_short: doivent en comporter une minimum
+        communication/website/agenda/event:
+          attributes:
+            to_day:
+              too_soon: doit être après jour de début
+            to_hour:
+              too_soon: doit être après l'heure de début
   admin:
     communication:
       blocks:
-- 
GitLab