From 364a5c96cc41812d4d6e3b1e76822adf076b3d96 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sun, 6 Aug 2023 15:15:30 +0200
Subject: [PATCH] refactor

---
 .../communication/block/template/license.rb   |  1 +
 app/services/licenses/creative_commons.rb     | 44 ++++++++++++++++++-
 .../blocks/templates/license/_edit.html.erb   |  6 +++
 .../templates/license/_snippet.html.erb       |  5 ++-
 .../blocks/templates/license/_static.html.erb | 13 +++---
 .../creative_commons/_full_name.html.erb      | 11 -----
 .../license/creative_commons/_icons.html.erb  |  8 ++++
 .../license/creative_commons/_name.html.erb   | 11 -----
 config/locales/communication/en.yml           |  2 +
 config/locales/communication/fr.yml           |  2 +
 10 files changed, 74 insertions(+), 29 deletions(-)
 delete mode 100644 app/views/admin/communication/blocks/templates/license/creative_commons/_full_name.html.erb
 delete mode 100644 app/views/admin/communication/blocks/templates/license/creative_commons/_name.html.erb

diff --git a/app/models/communication/block/template/license.rb b/app/models/communication/block/template/license.rb
index 6e0bd8605..a34d1f2fb 100644
--- a/app/models/communication/block/template/license.rb
+++ b/app/models/communication/block/template/license.rb
@@ -1,4 +1,5 @@
 class Communication::Block::Template::License < Communication::Block::Template::Base
+  has_component :description, :rich_text
   has_component :type, :option, options: [:creative_commons]
   has_component :creative_commons_attribution, :option, options: [:false, :true]
   has_component :creative_commons_commercial_use, :option, options: [:true, :false]
diff --git a/app/services/licenses/creative_commons.rb b/app/services/licenses/creative_commons.rb
index 9efc25c8d..d7e818923 100644
--- a/app/services/licenses/creative_commons.rb
+++ b/app/services/licenses/creative_commons.rb
@@ -42,10 +42,52 @@ class Licenses::CreativeCommons
         end
         @url += '/4.0/'
       else
-        @url = 'http://creativecommons.org/publicdomain/zero/1.0/'
+        @url = 'https://creativecommons.org/publicdomain/zero/1.0/'
       end
     end
     @url
   end
 
+  def short_name
+    unless @short_name
+      if attribution
+        @short_name = 'CC BY'
+        @short_name += '-NC' if !commercial_use
+        @short_name += '-ND' if !derivatives
+        @short_name += '-SA' if derivatives && !sharing
+        @short_name += ' 4.0'
+      else
+        @short_name = 'CC0 1.0 Universal'
+      end
+    end
+    @short_name
+  end
+
+  def long_name
+    unless @long_name
+      if attribution
+        @long_name = 'Attribution'
+        @long_name += '-NonCommercial' if !commercial_use
+        @long_name += '-NoDerivatives' if !derivatives
+        @long_name += '-ShareAlike' if derivatives && !sharing
+        @long_name += ' 4.0 International'
+      else
+        @long_name = 'CC0 1.0 Universal'
+      end
+    end
+    @long_name
+  end
+
+  def name
+    short_name
+  end
+
+  def to_s
+    "#{name}"
+  end
+
+  def icons
+    ['cc']
+  end
+
 end
\ No newline at end of file
diff --git a/app/views/admin/communication/blocks/templates/license/_edit.html.erb b/app/views/admin/communication/blocks/templates/license/_edit.html.erb
index 5bf393e89..0aa9ff2d1 100644
--- a/app/views/admin/communication/blocks/templates/license/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/license/_edit.html.erb
@@ -1,3 +1,9 @@
+<div class="row pure__row--small">
+  <div class="col-xl-6">
+    <%= block_component_edit block, :description %>
+  </div>
+</div>
+
 <%= block_component_edit block, :type %>
 
 <div v-show="data.type == 'creative_commons'">
diff --git a/app/views/admin/communication/blocks/templates/license/_snippet.html.erb b/app/views/admin/communication/blocks/templates/license/_snippet.html.erb
index b4b2d2def..2a92220ef 100644
--- a/app/views/admin/communication/blocks/templates/license/_snippet.html.erb
+++ b/app/views/admin/communication/blocks/templates/license/_snippet.html.erb
@@ -1,2 +1,5 @@
+<%
+cc = Licenses::CreativeCommons.create_from_block(block) 
+%>
 <%= render 'admin/communication/blocks/templates/license/creative_commons/icons', block: block %>
-<%= render 'admin/communication/blocks/templates/license/creative_commons/name', block: block %>
\ No newline at end of file
+<%= cc.short_name %>
\ No newline at end of file
diff --git a/app/views/admin/communication/blocks/templates/license/_static.html.erb b/app/views/admin/communication/blocks/templates/license/_static.html.erb
index 37a5aa2e0..74f0c1042 100644
--- a/app/views/admin/communication/blocks/templates/license/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/license/_static.html.erb
@@ -1,12 +1,15 @@
+<%
+cc = Licenses::CreativeCommons.create_from_block(block)
+%>
 <%= block_component_static block, :type -%>
       creative_commons:
         attribution: <%= block.template.creative_commons_attribution %>
         commercial_use: <%= block.template.creative_commons_commercial_use %>
         derivatives: <%= block.template.creative_commons_derivatives %>
         sharing: <%= block.template.creative_commons_sharing %>
-        name: >-
-          <%= render 'admin/communication/blocks/templates/license/creative_commons/name', block: block -%>
-        full_name: >-
-          <%= render 'admin/communication/blocks/templates/license/creative_commons/full_name', block: block -%>
+        short_name: >-
+          <%= cc.short_name %>
+        long_name: >-
+          <%= cc.long_name %>
         url: >-
-          <%= render 'admin/communication/blocks/templates/license/creative_commons/url', block: block -%>
\ No newline at end of file
+          <%= cc.url -%>
\ No newline at end of file
diff --git a/app/views/admin/communication/blocks/templates/license/creative_commons/_full_name.html.erb b/app/views/admin/communication/blocks/templates/license/creative_commons/_full_name.html.erb
deleted file mode 100644
index ef7b71261..000000000
--- a/app/views/admin/communication/blocks/templates/license/creative_commons/_full_name.html.erb
+++ /dev/null
@@ -1,11 +0,0 @@
-<%
-attribution     ||= block.template.creative_commons_attribution
-commercial_use  ||= block.template.creative_commons_commercial_use
-derivatives     ||= block.template.creative_commons_derivatives
-sharing         ||= block.template.creative_commons_sharing
-%>
-<% if attribution == 'false' %>CC0 1.0 Universal
-<% else %>Attribution<%  if commercial_use == 'false' %>-NonCommercial<% end %><% 
-        if derivatives == 'false' %>-NoDerivatives<% end %><%
-        if derivatives == 'true' && sharing == 'false' %>-ShareAlike<% end %> 4.0 International
-<% end %>
\ No newline at end of file
diff --git a/app/views/admin/communication/blocks/templates/license/creative_commons/_icons.html.erb b/app/views/admin/communication/blocks/templates/license/creative_commons/_icons.html.erb
index f97afee00..88862e92d 100644
--- a/app/views/admin/communication/blocks/templates/license/creative_commons/_icons.html.erb
+++ b/app/views/admin/communication/blocks/templates/license/creative_commons/_icons.html.erb
@@ -4,7 +4,15 @@ attribution     ||= block.template.creative_commons_attribution
 commercial_use  ||= block.template.creative_commons_commercial_use
 derivatives     ||= block.template.creative_commons_derivatives
 sharing         ||= block.template.creative_commons_sharing
+
+cc = Licenses::CreativeCommons.create_from_block(block) 
+
 %>
+<% cc.icons.each do |icon| %>
+  <%= image_tag "communication/blocks/templates/license/creative_commons/icons/#{icon}.svg",
+                width: icon_width, 
+                class: 'me-1' %>
+<% end %>
 <%= image_tag 'communication/blocks/templates/license/creative_commons/icons/cc.svg', width: icon_width, class: 'me-1' %>
 <% if attribution == 'false' %>
   <%= image_tag 'communication/blocks/templates/license/creative_commons/icons/zero.svg', width: icon_width, class: 'me-1' %>
diff --git a/app/views/admin/communication/blocks/templates/license/creative_commons/_name.html.erb b/app/views/admin/communication/blocks/templates/license/creative_commons/_name.html.erb
deleted file mode 100644
index 45443cd03..000000000
--- a/app/views/admin/communication/blocks/templates/license/creative_commons/_name.html.erb
+++ /dev/null
@@ -1,11 +0,0 @@
-<%
-attribution     ||= block.template.creative_commons_attribution
-commercial_use  ||= block.template.creative_commons_commercial_use
-derivatives     ||= block.template.creative_commons_derivatives
-sharing         ||= block.template.creative_commons_sharing
-%>
-<% if attribution == 'false' %>CC0 1.0 Universal
-<% else %>CC BY<%  if commercial_use == 'false' %>-NC<% end %><% 
-        if derivatives == 'false' %>-ND<% end %><%
-        if derivatives == 'true' && sharing == 'false' %>-SA<% end %> 4.0
-<% end %>
\ No newline at end of file
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index a5b6f5603..a19f90c17 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -498,6 +498,8 @@ en:
                 options:
                   true: Yes. Others can share adaptations of my work under any terms.
                   false: No. Others must use the same CC license if they adapt my work.
+              description:
+                label: Description
           organization_chart:
             description: People and their jobs.
             edit:
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 59558034f..39da2cfc2 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -495,6 +495,8 @@ fr:
                 options:
                   true: Oui. D'autres personnes peuvent partager des adaptations de mon œuvre dans n'importe quelles conditions.
                   false: Non. Les autres doivent utiliser la même licence CC s'ils adaptent mon œuvre.
+              description:
+                label: Description
           organization_chart:
             description: Une liste de personnes, avec photo, fonction et lien vers la page de la personne.
             edit:
-- 
GitLab