From 2696c68d005b67d79a1afddd94d4242f7f68ee3f Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 5 Sep 2023 07:33:18 +0200
Subject: [PATCH] Fix #1225

---
 .../admin/communication/websites/pages_controller.rb     | 7 +++++++
 .../admin/communication/websites/posts_controller.rb     | 9 ++++++++-
 app/helpers/admin/application_helper.rb                  | 8 ++++++++
 .../admin/communication/websites/pages/show.html.erb     | 3 ++-
 .../admin/communication/websites/posts/index.html.erb    | 2 +-
 .../admin/communication/websites/posts/show.html.erb     | 1 +
 config/locales/communication/en.yml                      | 3 +++
 config/locales/communication/fr.yml                      | 3 +++
 config/routes/admin/communication.rb                     | 4 +++-
 9 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/app/controllers/admin/communication/websites/pages_controller.rb b/app/controllers/admin/communication/websites/pages_controller.rb
index 68efb9566..ec1885470 100644
--- a/app/controllers/admin/communication/websites/pages_controller.rb
+++ b/app/controllers/admin/communication/websites/pages_controller.rb
@@ -47,6 +47,13 @@ class Admin::Communication::Websites::PagesController < Admin::Communication::We
     add_breadcrumb(@page, admin_communication_website_page_path(@page))
   end
 
+  def publish
+    @page.published = true
+    @page.save_and_sync
+    redirect_back fallback_location: admin_communication_website_page_path(@page),
+                  notice: t('admin.communication.website.publish.notice')
+  end
+
   def static
     @about = @page
     render layout: false
diff --git a/app/controllers/admin/communication/websites/posts_controller.rb b/app/controllers/admin/communication/websites/posts_controller.rb
index 150442c0a..e16f48cec 100644
--- a/app/controllers/admin/communication/websites/posts_controller.rb
+++ b/app/controllers/admin/communication/websites/posts_controller.rb
@@ -23,7 +23,7 @@ class Admin::Communication::Websites::PostsController < Admin::Communication::We
     breadcrumb
   end
 
-  def publish
+  def publish_batch
     ids = params[:ids] || []
     target_posts = @website.posts.where(id: ids)
     is_published = params[:published] == "true"
@@ -35,6 +35,13 @@ class Admin::Communication::Websites::PostsController < Admin::Communication::We
                   notice: t('communication.website.posts.successful_batch_update')
   end
 
+  def publish
+    @post.published = true
+    @post.save_and_sync
+    redirect_back fallback_location: admin_communication_website_post_path(@post),
+                  notice: t('admin.communication.website.publish.notice')
+  end
+
   def show
     @preview = true
     breadcrumb
diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb
index 07e0aad31..d914917ac 100644
--- a/app/helpers/admin/application_helper.rb
+++ b/app/helpers/admin/application_helper.rb
@@ -55,6 +55,14 @@ module Admin::ApplicationHelper
                   aria-controls=\"preview\">#{ t 'preview.button'}</button>"
   end
 
+  def publish_link(object)
+    return if object.published
+    link_to t('admin.communication.website.publish.button'),
+            [:publish, :admin, object],
+            method: :post,
+            class: button_classes
+  end
+
   def static_link(path)
     return unless current_user.server_admin?
     raw "<a href=\"#{path}\" class=\"btn btn-light btn-xs\">#{t 'static' }</a>"
diff --git a/app/views/admin/communication/websites/pages/show.html.erb b/app/views/admin/communication/websites/pages/show.html.erb
index e47682797..d03a5465f 100644
--- a/app/views/admin/communication/websites/pages/show.html.erb
+++ b/app/views/admin/communication/websites/pages/show.html.erb
@@ -41,5 +41,6 @@
               target: :_blank,
               class: 'btn btn-light btn-xs' if @page.published %>
   <%= preview_link %>
-  <%= link_to t('edit'), edit_admin_communication_website_page_path(@page), class: button_classes %>
+  <%= edit_link @page %>
+  <%= publish_link @page %>
 <% end %>
diff --git a/app/views/admin/communication/websites/posts/index.html.erb b/app/views/admin/communication/websites/posts/index.html.erb
index c6c64458d..1c30eb953 100644
--- a/app/views/admin/communication/websites/posts/index.html.erb
+++ b/app/views/admin/communication/websites/posts/index.html.erb
@@ -14,7 +14,7 @@
   %>
   <%= osuny_panel Communication::Website::Post.model_name.human(count: 2), action: action do %>
     <div data-batch-selectable class="mb-5">
-      <%= form_tag publish_admin_communication_website_posts_path do %>
+      <%= form_tag publish_batch_admin_communication_website_posts_path do %>
         <%= render 'filters', current_path: admin_communication_website_posts_path, filters: @filters %>
         <%= render 'admin/communication/websites/posts/list', posts: @posts, selectable: true %>
         <%= paginate @posts, theme: 'bootstrap-5' %>
diff --git a/app/views/admin/communication/websites/posts/show.html.erb b/app/views/admin/communication/websites/posts/show.html.erb
index 9ba6dc7ad..ce732111f 100644
--- a/app/views/admin/communication/websites/posts/show.html.erb
+++ b/app/views/admin/communication/websites/posts/show.html.erb
@@ -39,4 +39,5 @@
               class: 'btn btn-light btn-xs' if @post.url %>
   <%= preview_link %>
   <%= edit_link @post %>
+  <%= publish_link @post %>
 <% end %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 7695c76d0..21d3ba7cb 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -718,6 +718,9 @@ en:
           published_status: Publication status
         post:
           pinned_status: Pinned status
+        publish: 
+          button: Publish
+          notice: Publication in progress, it should take a few minutes to be online.
   communication:
     authors:
       one: Author
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index aa22bd936..63ea710a4 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -715,6 +715,9 @@ fr:
           published_status: État de publication
         post:
           pinned_status: Mise en avant
+        publish: 
+          button: Publier
+          notice: Publication en cours, cela devrait prendre quelques minutes pour arriver en ligne.
   communication:
     authors:
       one: Auteur·rice
diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb
index ce05293a0..df3e502e7 100644
--- a/config/routes/admin/communication.rb
+++ b/config/routes/admin/communication.rb
@@ -26,6 +26,7 @@ namespace :communication do
         get :preview
         get "translate" => "websites/pages#translate", as: :translate
         post :duplicate
+        post :publish
         post :connect
         post :disconnect
         post 'generate-from-template' => 'websites/pages#generate_from_template', as: :generate
@@ -44,12 +45,13 @@ namespace :communication do
     resources :posts, controller: 'websites/posts', path: '/:lang/posts' do
       collection do
         resources :curations, as: :post_curations, controller: 'websites/posts/curations', only: [:new, :create]
-        post :publish
+        post :publish_batch
       end
       member do
         get :static
         get :preview
         post :duplicate
+        post :publish
       end
     end
     resources :menus, controller: 'websites/menus', path: '/:lang/menus' do
-- 
GitLab