From cb64f57e7a9b29e00569fc933a78cb11b90eb256 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 3 Jun 2022 07:25:45 +0200
Subject: [PATCH] introspective helpers

---
 app/helpers/admin/blocks_helper.rb            | 26 +++++++++++++++++++
 .../communication/block/component/base.rb     |  4 +++
 .../blocks/templates/chapter/_edit.html.erb   |  6 ++---
 .../templates/chapter/_preview.html.erb       |  6 ++---
 .../blocks/templates/chapter/_static.html.erb |  6 ++---
 .../blocks/templates/video/_edit.html.erb     |  4 +--
 .../blocks/templates/video/_preview.html.erb  |  4 +--
 .../blocks/templates/video/_static.html.erb   |  4 +--
 8 files changed, 45 insertions(+), 15 deletions(-)
 create mode 100644 app/helpers/admin/blocks_helper.rb

diff --git a/app/helpers/admin/blocks_helper.rb b/app/helpers/admin/blocks_helper.rb
new file mode 100644
index 000000000..387ff3e01
--- /dev/null
+++ b/app/helpers/admin/blocks_helper.rb
@@ -0,0 +1,26 @@
+module Admin::BlocksHelper
+
+  def block_component_edit(property, **options)
+    component = block_component property
+    partial = "admin/communication/blocks/components/#{component.kind}/edit"
+    render partial, property: property, **options
+  end
+
+  def block_component_preview(property, **options)
+    component = block_component property
+    partial = "admin/communication/blocks/components/#{component.kind}/preview"
+    render partial, property: property, **options
+  end
+
+  def block_component_static(property, **options)
+    component = block_component property
+    partial = "admin/communication/blocks/components/#{component.kind}/static"
+    render partial, property: property, **options
+  end
+
+  protected
+
+  def block_component(property)
+    @block.template.public_send "#{property}_component"
+  end
+end
diff --git a/app/models/communication/block/component/base.rb b/app/models/communication/block/component/base.rb
index 290e1cb2d..cd1d13e91 100644
--- a/app/models/communication/block/component/base.rb
+++ b/app/models/communication/block/component/base.rb
@@ -13,4 +13,8 @@ class Communication::Block::Component::Base
   def data=(value)
     @data = value
   end
+
+  def kind
+    self.class.name.demodulize.underscore
+  end
 end
diff --git a/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb b/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb
index cd0aa4f8d..c455ecd9b 100644
--- a/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb
@@ -1,9 +1,9 @@
 <div class="row mb-4">
   <div class="col-md-6">
-    <%= render 'admin/communication/blocks/components/rich_text/edit', property: :text %>
-    <%= render 'admin/communication/blocks/components/rich_text/edit', property: :notes %>
+    <%= block_component_edit :text %>
+    <%= block_component_edit :notes %>
   </div>
   <div class="col-md-6">
-    <%= render 'admin/communication/blocks/components/image/edit', property: :image %>
+    <%= block_component_edit :image %>
   </div>
 </div>
diff --git a/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb b/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb
index 79f20d919..9a02f6ae9 100644
--- a/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb
+++ b/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb
@@ -1,3 +1,3 @@
-<%= render 'admin/communication/blocks/components/image/preview', property: :image %>
-<%= render 'admin/communication/blocks/components/rich_text/preview', property: :text %>
-<%= render 'admin/communication/blocks/components/rich_text/preview', property: :notes %>
+<%= block_component_preview :image %>
+<%= block_component_preview :notes %>
+<%= block_component_preview :text %>
diff --git a/app/views/admin/communication/blocks/templates/chapter/_static.html.erb b/app/views/admin/communication/blocks/templates/chapter/_static.html.erb
index 666bc43a7..fc5903037 100644
--- a/app/views/admin/communication/blocks/templates/chapter/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/chapter/_static.html.erb
@@ -1,3 +1,3 @@
-<%= render 'admin/communication/blocks/components/rich_text/static', property: :text %>
-<%= render 'admin/communication/blocks/components/rich_text/static', property: :notes %>
-<%= render 'admin/communication/blocks/components/image/static', property: :image %>
+<%= block_component_static :text %>
+<%= block_component_static :notes %>
+<%= block_component_static :image %>
diff --git a/app/views/admin/communication/blocks/templates/video/_edit.html.erb b/app/views/admin/communication/blocks/templates/video/_edit.html.erb
index 7940e1660..48bd2e421 100644
--- a/app/views/admin/communication/blocks/templates/video/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/video/_edit.html.erb
@@ -1,8 +1,8 @@
 <div class="row mb-4">
   <div class="col-md-6">
-    <%= render 'admin/communication/blocks/components/string/edit', property: :url %>
+    <%= block_component_edit :url %>
   </div>
   <div class="col-md-6">
-    <%= render 'admin/communication/blocks/components/text/edit', property: :transcription, rows: 10 %>
+    <%= block_component_edit :transcription, rows: 10 %>
   </div>
 </div>
diff --git a/app/views/admin/communication/blocks/templates/video/_preview.html.erb b/app/views/admin/communication/blocks/templates/video/_preview.html.erb
index d39427a0d..1421cdcef 100644
--- a/app/views/admin/communication/blocks/templates/video/_preview.html.erb
+++ b/app/views/admin/communication/blocks/templates/video/_preview.html.erb
@@ -1,3 +1,3 @@
 <%# TODO iframe video %>
-<%= render 'admin/communication/blocks/components/string/preview', property: :url %>
-<%= render 'admin/communication/blocks/components/text/preview', property: :transcription %>
+<%= block_component_preview :url %>
+<%= block_component_preview :transcription %>
diff --git a/app/views/admin/communication/blocks/templates/video/_static.html.erb b/app/views/admin/communication/blocks/templates/video/_static.html.erb
index 1e134e6ae..ddc52312d 100644
--- a/app/views/admin/communication/blocks/templates/video/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/video/_static.html.erb
@@ -1,2 +1,2 @@
-<%= render 'admin/communication/blocks/components/string/static', property: :url %>
-<%= render 'admin/communication/blocks/components/text/static', property: :transcription %>
+<%= block_component_static :url %>
+<%= block_component_static :transcription %>
-- 
GitLab