From 7dc503a0fa3a2a6e4288743a99b09e435a423544 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 5 Nov 2021 12:07:49 +0100
Subject: [PATCH] publish media batch

---
 .../communication/website/pages_controller.rb |  5 +++++
 .../communication/website/posts_controller.rb |  5 +++++
 .../communication/website/imported/website.rb | 10 ++++++++++
 .../communication/website/with_media.rb       | 20 +++++++++----------
 app/models/concerns/with_github.rb            |  4 ++++
 .../communication/website/pages/show.html.erb |  7 +++++++
 .../communication/website/posts/show.html.erb |  7 +++++++
 config/locales/communication/en.yml           |  1 +
 config/locales/communication/fr.yml           |  1 +
 config/locales/en.yml                         |  1 +
 config/locales/fr.yml                         |  1 +
 config/routes/admin/communication.rb          |  7 ++++++-
 12 files changed, 58 insertions(+), 11 deletions(-)

diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb
index 5f56a8bde..426870e14 100644
--- a/app/controllers/admin/communication/website/pages_controller.rb
+++ b/app/controllers/admin/communication/website/pages_controller.rb
@@ -29,6 +29,11 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web
     breadcrumb
   end
 
+  def publish
+    @page.force_publish!
+    redirect_to admin_communication_website_page_path(@page), notice: t('admin.will_be_published_html', model: @page.to_s)
+  end
+
   def new
     @page.website = @website
     breadcrumb
diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb
index d21f53f8d..c3b7128a7 100644
--- a/app/controllers/admin/communication/website/posts_controller.rb
+++ b/app/controllers/admin/communication/website/posts_controller.rb
@@ -10,6 +10,11 @@ class Admin::Communication::Website::PostsController < Admin::Communication::Web
     breadcrumb
   end
 
+  def publish
+    @post.force_publish!
+    redirect_to admin_communication_website_post_path(@post), notice: t('admin.will_be_published_html', model: @post.to_s)
+  end
+
   def new
     @post.website = @website
     breadcrumb
diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb
index d758f8456..70f15a1b6 100644
--- a/app/models/communication/website/imported/website.rb
+++ b/app/models/communication/website/imported/website.rb
@@ -95,6 +95,11 @@ class Communication::Website::Imported::Website < ApplicationRecord
           github.add_to_batch path: page.github_path_generated,
                               previous_path: page.github_path,
                               data: page.to_jekyll
+          page.active_storage_blobs.each do |blob|
+            blob.analyze unless blob.analyzed?
+            github.add_to_batch path: page.blob_github_path_generated(blob),
+                                data: page.blob_to_jekyll(blob)
+          end
         end
         github.commit_batch '[Page] Batch update from import'
       end
@@ -116,6 +121,11 @@ class Communication::Website::Imported::Website < ApplicationRecord
           github.add_to_batch path: generated_post.github_path_generated,
                               previous_path: generated_post.github_path,
                               data: generated_post.to_jekyll
+          generated_post.active_storage_blobs.each do |blob|
+            blob.analyze unless blob.analyzed?
+            github.add_to_batch path: generated_post.blob_github_path_generated(blob),
+                                data: generated_post.blob_to_jekyll(blob)
+          end
         end
       end
       github.commit_batch '[Post] Batch update from import' if github.valid?
diff --git a/app/models/communication/website/with_media.rb b/app/models/communication/website/with_media.rb
index e25ffb8d2..10c635773 100644
--- a/app/models/communication/website/with_media.rb
+++ b/app/models/communication/website/with_media.rb
@@ -1,13 +1,21 @@
 module Communication::Website::WithMedia
   extend ActiveSupport::Concern
 
-  protected
-
   def active_storage_blobs
     blob_ids = [featured_image&.blob_id, text.embeds.blobs.pluck(:id)].flatten.compact
     university.active_storage_blobs.where(id: blob_ids)
   end
 
+  def blob_to_jekyll(blob)
+    ApplicationController.render(
+      template: 'active_storage/blobs/jekyll',
+      layout: false,
+      assigns: { blob: blob }
+    )
+  end
+
+  protected
+
   def publish_to_github
     super
     active_storage_blobs.each do |blob|
@@ -17,12 +25,4 @@ module Communication::Website::WithMedia
                     data: blob_to_jekyll(blob))
     end
   end
-
-  def blob_to_jekyll(blob)
-    ApplicationController.render(
-      template: 'active_storage/blobs/jekyll',
-      layout: false,
-      assigns: { blob: blob }
-    )
-  end
 end
diff --git a/app/models/concerns/with_github.rb b/app/models/concerns/with_github.rb
index ab6366b2d..65fea1ba1 100644
--- a/app/models/concerns/with_github.rb
+++ b/app/models/concerns/with_github.rb
@@ -5,6 +5,10 @@ module WithGithub
     after_save :publish_to_github
   end
 
+  def force_publish!
+    publish_to_github
+  end
+
   def github_content
     @content ||= github.read_file_at github_path
   end
diff --git a/app/views/admin/communication/website/pages/show.html.erb b/app/views/admin/communication/website/pages/show.html.erb
index bd13c0218..6a0bc6cde 100644
--- a/app/views/admin/communication/website/pages/show.html.erb
+++ b/app/views/admin/communication/website/pages/show.html.erb
@@ -80,6 +80,13 @@
   </div>
 </div>
 
+<% content_for :action_bar_left do %>
+  <%= link_to t('communication.website.force_publication'),
+              publish_admin_communication_website_page_path(@page),
+              method: :post,
+              class: button_classes %>
+<% end %>
+
 <% content_for :action_bar_right do %>
   <%= edit_link @page %>
 <% end %>
diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb
index 1a3b94788..3e44d2c44 100644
--- a/app/views/admin/communication/website/posts/show.html.erb
+++ b/app/views/admin/communication/website/posts/show.html.erb
@@ -69,6 +69,13 @@
   </div>
 </div>
 
+<% content_for :action_bar_left do %>
+  <%= link_to t('communication.website.force_publication'),
+              publish_admin_communication_website_post_path(@post),
+              method: :post,
+              class: button_classes %>
+<% end %>
+
 <% content_for :action_bar_right do %>
   <%= edit_link @post %>
 <% end %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 8e4ad0baa..1b28d20a7 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -1,6 +1,7 @@
 en:
   communication:
     website:
+      force_publication: 'Force publication'
       imported:
         from: Imported from
         launch: Launch import
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 197557fd7..8a117d0ff 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -1,6 +1,7 @@
 fr:
   communication:
     website:
+      force_publication: 'Forcer la publication'
       imported:
         from: Importé depuis
         launch: Importer le site
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 7e56b2cc9..683b157c9 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -47,6 +47,7 @@ en:
     users_alerts:
       not_locked_html: '<i>%{model}</i> was not locked.'
       successfully_unlocked_html: "<i>%{model}</i> was successfully unlocked."
+    will_be_published_html: "<i>%{model}</i> will soon be published."
   content: Content
   cookies_consent_choice: Cookies consent choice
   cookies_policy: Cookies policy
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 14a54f555..b36549ec4 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -47,6 +47,7 @@ fr:
     users_alerts:
       not_locked_html: "<i>%{model}</i> n'était pas verrouillé(e)."
       successfully_unlocked_html: "<i>%{model}</i> a bien été déverrouillé(e)."
+    will_be_published_html: "<i>%{model}</i> va bientôt être publié(e)."
   content: Contenu
   cookies_consent_choice: Choix en matière de cookies
   cookies_policy: Politique de cookies
diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb
index 46430d09c..c335393f7 100644
--- a/config/routes/admin/communication.rb
+++ b/config/routes/admin/communication.rb
@@ -10,6 +10,7 @@ namespace :communication do
       end
       member do
         get :children
+        post :publish
       end
     end
     resources :categories, controller: 'website/categories' do
@@ -21,7 +22,11 @@ namespace :communication do
       end
     end
     resources :authors, controller: 'website/authors'
-    resources :posts, controller: 'website/posts'
+    resources :posts, controller: 'website/posts' do
+      member do
+        post :publish
+      end
+    end
     resources :menus, controller: 'website/menus' do
       resources :items, controller: 'website/menu/items', except: :index do
         collection do
-- 
GitLab