From 3c3a6d82b8f4878eb308341c0087122768545a68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 14 Feb 2022 17:43:24 +0100
Subject: [PATCH] summernote embeds

---
 Gemfile.lock                                  |  2 +-
 app/models/concerns/with_blobs.rb             | 19 ++++++++++++-------
 .../communication/website/posts/show.html.erb |  2 +-
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/Gemfile.lock b/Gemfile.lock
index bae64a178..7f7439af4 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,6 +1,6 @@
 GIT
   remote: https://github.com/noesya/summernote-rails.git
-  revision: e60b3a9e84ea8670ab6cbc5eaef33451ac588ca4
+  revision: 7463656df2b81ff434c8fa8786453b55be129054
   branch: activestorage
   specs:
     summernote-rails (0.8.20.1)
diff --git a/app/models/concerns/with_blobs.rb b/app/models/concerns/with_blobs.rb
index 8a0cd6874..27a729abf 100644
--- a/app/models/concerns/with_blobs.rb
+++ b/app/models/concerns/with_blobs.rb
@@ -13,20 +13,25 @@ module WithBlobs
     blobs_with_ids inherited_blob_ids
   end
 
+  def summernote_embeds
+    summernote_embeds_reflection_names.map { |summernote_reflection_name|
+      public_send(summernote_reflection_name)
+    }.flatten
+  end
+
   protected
 
   def explicit_blob_ids
-    [rich_text_blob_ids]
+    [summernote_blob_ids]
   end
 
   def inherited_blob_ids
     []
   end
 
-  def rich_text_blob_ids
-    rich_text_reflection_names.map { |rich_text_reflection_name|
-      rich_text = public_send(rich_text_reflection_name)
-      rich_text.present? ? rich_text.embeds.blobs.pluck(:id) : []
+  def summernote_blob_ids
+    summernote_embeds_reflection_names.map { |summernote_reflection_name|
+      public_send(summernote_reflection_name).pluck(:blob_id)
     }.flatten
   end
 
@@ -34,7 +39,7 @@ module WithBlobs
     university.active_storage_blobs.where(id: ids.flatten.compact)
   end
 
-  def rich_text_reflection_names
-    @rich_text_reflection_names ||= _reflections.select { |name, reflection| reflection.class_name == "ActionText::RichText" }.keys
+  def summernote_embeds_reflection_names
+    @summernote_embeds_reflection_names ||= _reflections.keys.select { |name| name.ends_with?('_summernote_embeds_attachments') }
   end
 end
diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb
index f9b199f19..e2fb1cdbf 100644
--- a/app/views/admin/communication/website/posts/show.html.erb
+++ b/app/views/admin/communication/website/posts/show.html.erb
@@ -10,7 +10,7 @@
         <div class="card-body">
           <%= render 'admin/application/property/text', object: @post, property: :description %>
           <%= render 'admin/application/property/text', object: @post, property: :text %>
-          <%#= render 'admin/application/property/summernote_embeds', object: @post, property: :medias %>
+          <%= render 'admin/application/property/summernote_embeds', object: @post, property: :medias %>
           <hr>
           <pre><%= @post.text_new.to_html %></pre>
         </div>
-- 
GitLab