From 3e415e243537b95d2ff170275ad4aa1db7e701c7 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sat, 4 Jun 2022 09:04:55 +0200
Subject: [PATCH] elements

---
 app/models/communication/block/template/base.rb    | 11 +++++++++--
 app/models/communication/block/template/gallery.rb |  5 +----
 app/models/communication/block/template/page.rb    |  4 ----
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/app/models/communication/block/template/base.rb b/app/models/communication/block/template/base.rb
index 8c8ee497e..f061e4b36 100644
--- a/app/models/communication/block/template/base.rb
+++ b/app/models/communication/block/template/base.rb
@@ -1,5 +1,7 @@
 class Communication::Block::Template::Base
-  class_attribute :components_descriptions, :layouts
+  class_attribute :components_descriptions,
+                  :layouts,
+                  :element_class
 
   attr_reader :block
 
@@ -29,6 +31,10 @@ class Communication::Block::Template::Base
     self.layouts = list
   end
 
+  def self.has_elements(element_class)
+    self.element_class = element_class
+  end
+
   def self.has_component(property, kind)
     self.components_descriptions ||= []
     self.components_descriptions << { name: property, type: kind }
@@ -92,7 +98,8 @@ class Communication::Block::Template::Base
   end
 
   def default_element
-    nil
+    return if self.class.element_class.nil?
+    self.class.element_class.new block
   end
 
   def elements
diff --git a/app/models/communication/block/template/gallery.rb b/app/models/communication/block/template/gallery.rb
index 38f353f75..8df447517 100644
--- a/app/models/communication/block/template/gallery.rb
+++ b/app/models/communication/block/template/gallery.rb
@@ -1,10 +1,7 @@
 class Communication::Block::Template::Gallery < Communication::Block::Template::Base
 
   has_layouts [:grid, :carousel]
-
-  def default_element
-    Communication::Block::Template::Gallery::Image.new block
-  end
+  has_elements Communication::Block::Template::Gallery::Image
 
   def build_git_dependencies
     add_dependency active_storage_blobs
diff --git a/app/models/communication/block/template/page.rb b/app/models/communication/block/template/page.rb
index 023ccda46..521f841f9 100644
--- a/app/models/communication/block/template/page.rb
+++ b/app/models/communication/block/template/page.rb
@@ -20,10 +20,6 @@ class Communication::Block::Template::Page < Communication::Block::Template::Bas
     @main_page ||= page(data['page_id'])
   end
 
-  def layout
-    data['layout'] || 'grid'
-  end
-
   def show_main_description
     data['show_main_description'] || false
   end
-- 
GitLab