From f50841841a3d18d8def693fa55e69a43a533c7c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 8 Feb 2024 14:29:56 +0100
Subject: [PATCH] event add_to_calendar_urls

---
 app/models/communication/website/agenda/event.rb   |  1 +
 .../communication/website/agenda/event/with_cal.rb | 14 ++++++++++++++
 .../websites/agenda/events/_dates_static.html.erb  | 10 +++++-----
 ..._urls_to_communication_website_agenda_events.rb |  7 +++++++
 db/schema.rb                                       |  3 ++-
 5 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 db/migrate/20240208131753_add_add_to_calendar_urls_to_communication_website_agenda_events.rb

diff --git a/app/models/communication/website/agenda/event.rb b/app/models/communication/website/agenda/event.rb
index d30cb3a71..19cca5948 100644
--- a/app/models/communication/website/agenda/event.rb
+++ b/app/models/communication/website/agenda/event.rb
@@ -3,6 +3,7 @@
 # Table name: communication_website_agenda_events
 #
 #  id                       :uuid             not null, primary key
+#  add_to_calendar_urls     :jsonb
 #  featured_image_alt       :text
 #  featured_image_credit    :text
 #  from_day                 :date
diff --git a/app/models/communication/website/agenda/event/with_cal.rb b/app/models/communication/website/agenda/event/with_cal.rb
index b03c58c3e..dfbdc0c08 100644
--- a/app/models/communication/website/agenda/event/with_cal.rb
+++ b/app/models/communication/website/agenda/event/with_cal.rb
@@ -1,6 +1,10 @@
 module Communication::Website::Agenda::Event::WithCal
   extend ActiveSupport::Concern
 
+  included do
+    before_save :set_add_to_calendar_urls
+  end
+
   def cal
     @cal ||= AddToCalendar::URLs.new(
       start_datetime: cal_from_time,
@@ -15,6 +19,16 @@ module Communication::Website::Agenda::Event::WithCal
 
   protected
 
+  def set_add_to_calendar_urls
+    self.add_to_calendar_urls = {
+      google: cal.google_url,
+      yahoo: cal.yahoo_url,
+      office: cal.office365_url,
+      outlook: cal.outlook_com_url,
+      ical: cal.ical_url
+    }
+  end
+
   def cal_from_time
     from_hour.nil?  ? from_day.to_time
                     : date_and_time(from_day, from_hour)
diff --git a/app/views/admin/communication/websites/agenda/events/_dates_static.html.erb b/app/views/admin/communication/websites/agenda/events/_dates_static.html.erb
index 703d8eb35..69547b84a 100644
--- a/app/views/admin/communication/websites/agenda/events/_dates_static.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/_dates_static.html.erb
@@ -26,8 +26,8 @@ indentation = '  ' * depth
 <%= indentation %>    hour: <%= event.to_hour.strftime "%H:%M" %>
 <% end %>
 <%= indentation %>  add_to_calendar:
-<%= indentation %>    google: "<%= event.cal.google_url.html_safe %>"
-<%= indentation %>    yahoo: "<%= event.cal.yahoo_url.html_safe %>"
-<%= indentation %>    office: "<%= event.cal.office365_url.html_safe %>"
-<%= indentation %>    outlook: "<%= event.cal.outlook_com_url.html_safe %>"
-<%= indentation %>    ical: "<%= event.cal.ical_url.html_safe %>"
+<%= indentation %>    google: "<%= event.add_to_calendar_urls['google'].html_safe %>"
+<%= indentation %>    yahoo: "<%= event.add_to_calendar_urls['yahoo'].html_safe %>"
+<%= indentation %>    office: "<%= event.add_to_calendar_urls['office'].html_safe %>"
+<%= indentation %>    outlook: "<%= event.add_to_calendar_urls['outlook'].html_safe %>"
+<%= indentation %>    ical: "<%= event.add_to_calendar_urls['ical'].html_safe %>"
diff --git a/db/migrate/20240208131753_add_add_to_calendar_urls_to_communication_website_agenda_events.rb b/db/migrate/20240208131753_add_add_to_calendar_urls_to_communication_website_agenda_events.rb
new file mode 100644
index 000000000..cfa277e17
--- /dev/null
+++ b/db/migrate/20240208131753_add_add_to_calendar_urls_to_communication_website_agenda_events.rb
@@ -0,0 +1,7 @@
+class AddAddToCalendarUrlsToCommunicationWebsiteAgendaEvents < ActiveRecord::Migration[7.1]
+  def change
+    add_column :communication_website_agenda_events, :add_to_calendar_urls, :jsonb
+    Communication::Website::Agenda::Event.reset_column_information
+    Communication::Website::Agenda::Event.find_each(&:save)
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 45971753f..83d57cc2c 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema[7.1].define(version: 2024_02_07_085338) do
+ActiveRecord::Schema[7.1].define(version: 2024_02_08_131753) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -311,6 +311,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_07_085338) do
     t.string "slug"
     t.string "subtitle"
     t.string "time_zone"
+    t.jsonb "add_to_calendar_urls"
     t.index ["communication_website_id"], name: "index_agenda_events_on_communication_website_id"
     t.index ["language_id"], name: "index_communication_website_agenda_events_on_language_id"
     t.index ["original_id"], name: "index_communication_website_agenda_events_on_original_id"
-- 
GitLab