From bfb39ddae4aab3f74279fe35f758697652c04058 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sat, 23 Oct 2021 17:08:27 +0200
Subject: [PATCH] page and posts

---
 app/models/communication/website/page.rb      | 20 ++++++++-----------
 app/models/communication/website/post.rb      | 18 ++---------------
 .../communication/website/with_github.rb      | 19 +++++++++---------
 app/services/github.rb                        | 13 ++++++++----
 .../website/pages/_treebranch.html.erb        |  5 +++--
 .../communication/websites/show.html.erb      | 15 +++++++-------
 6 files changed, 39 insertions(+), 51 deletions(-)

diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 3c00cd34f..b799c4cd5 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -78,19 +78,15 @@ class Communication::Website::Page < ApplicationRecord
     children.each(&:save)
   end
 
-  def github_path
-    "_pages/#{github_file}"
+  def github_path_generated
+    "_pages/#{path}/index.html".gsub('//', '/')
   end
 
-  def publish_to_github
-    github.publish  kind: :pages,
-                    file: "#{ id }.html",
-                    title: to_s,
-                    data: ApplicationController.render(
-                      template: 'admin/communication/website/pages/jekyll',
-                      layout: false,
-                      assigns: { page: self }
-                    )
+  def to_jekyll
+    ApplicationController.render(
+      template: 'admin/communication/website/pages/jekyll',
+      layout: false,
+      assigns: { page: self }
+    )
   end
-  handle_asynchronously :publish_to_github
 end
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index fb5fcf313..31e70ca1c 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -52,29 +52,15 @@ class Communication::Website::Post < ApplicationRecord
 
   protected
 
-  def github_file
-    "#{published_at.year}/#{published_at.month}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
-  end
-
   def github_path_generated
-    "_posts/#{published_at.year}/#{published_at.month}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
+    "_posts/#{published_at.strftime "%Y/%m"}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
   end
 
-  def jekyll
+  def to_jekyll
     ApplicationController.render(
       template: 'admin/communication/website/posts/jekyll',
       layout: false,
       assigns: { post: self }
     )
   end
-
-  def publish_to_github
-    return if published_at.nil?
-    github.publish  path: github_path_generated,
-                    previous_path: github_path,
-                    commit: "[post] Save #{title}",
-                    data: jekyll
-    update_column :github_path, github_path_generated
-  end
-  # handle_asynchronously :publish_to_github
 end
diff --git a/app/models/communication/website/with_github.rb b/app/models/communication/website/with_github.rb
index 51148a60c..f7d243914 100644
--- a/app/models/communication/website/with_github.rb
+++ b/app/models/communication/website/with_github.rb
@@ -13,13 +13,8 @@ module Communication::Website::WithGithub
     @frontmatter ||= FrontMatterParser::Parser.new(:md).call(github_content)
   end
 
-  def github_file
-    "#{ id }.html"
-  end
-
-  # Needs override
   def github_path_generated
-    ''
+    '' # Needs override
   end
 
   protected
@@ -28,10 +23,16 @@ module Communication::Website::WithGithub
     @github ||= Github.with_site(website)
   end
 
-  # Needs override
+  def github_commit_message
+    "[#{self.class.name.demodulize}] Save #{ to_s }"
+  end
+
   def publish_to_github
-    ''
+    github.publish  path: github_path_generated,
+                    previous_path: github_path,
+                    commit: github_commit_message,
+                    data: to_jekyll
+    update_column :github_path, github_path_generated
   end
   handle_asynchronously :publish_to_github
-
 end
diff --git a/app/services/github.rb b/app/services/github.rb
index f441899ec..15962abe4 100644
--- a/app/services/github.rb
+++ b/app/services/github.rb
@@ -24,6 +24,7 @@ class Github
       # Deprecated
       local_path = "#{ tmp_directory }/#{ file }"
       remote_file = "_#{ kind }/#{ file }"
+      commit = "Save #{ title }"
     end
     Pathname(local_path).dirname.mkpath
     File.write local_path, data
@@ -31,7 +32,6 @@ class Github
     if !previous_path.blank? && path != previous_path
       move_file previous_path, path
     end
-    commit ||= "Save #{ title }"
     client.create_contents  repository,
                             remote_file,
                             commit,
@@ -93,20 +93,25 @@ class Github
     file = find_in_tree from
     return if file.nil?
     content = [{
+      path: from,
+      mode: file[:mode],
+      type: file[:type],
+      sha: nil
+    },
+    {
       path: to,
       mode: file[:mode],
       type: file[:type],
       sha: file[:sha]
     }]
-    begin
     new_tree = client.create_tree repository, content, base_tree: tree[:sha]
     message = "Move #{from} to #{to}"
     commit = client.create_commit repository, message, new_tree[:sha], branch_sha
     [:main, :master].each do |branch|
       client.update_branch repository, branch, commit[:sha]
     end
-    rescue
-    end
+  rescue
+    ''
   end
 
   def file_sha(path)
diff --git a/app/views/admin/communication/website/pages/_treebranch.html.erb b/app/views/admin/communication/website/pages/_treebranch.html.erb
index 704798d94..881d991a0 100644
--- a/app/views/admin/communication/website/pages/_treebranch.html.erb
+++ b/app/views/admin/communication/website/pages/_treebranch.html.erb
@@ -4,8 +4,9 @@
     <div class="d-flex align-items-center treeview__label border-bottom p-1">
       <%= link_to children_admin_communication_website_page_path(website_id: page.website.id, id: page.id),
                   class: 'js-treeview-openzone d-inline-block p-2 ps-0', style: 'width: 22px', remote: true do %>
-        <span class="open_btn"><i class="fas fa-folder"></i></span>
-        <span class="close_btn"><i class="fas fa-folder-open"></i></span>
+        <% icon_style = page.has_children? ? 'fas' : 'far' %>
+        <span class="open_btn"><i class="<%= icon_style %> fa-folder"></i></span>
+        <span class="close_btn"><i class="<%= icon_style %> fa-folder-open"></i></span>
       <% end %>
       <%= link_to page, admin_communication_website_page_path(website_id: page.website.id, id: page.id) %>
       <span class="move_btn py-2 ps-2"><i class="fas fa-sort"></i></span>
diff --git a/app/views/admin/communication/websites/show.html.erb b/app/views/admin/communication/websites/show.html.erb
index 50107a846..4b9d3c502 100644
--- a/app/views/admin/communication/websites/show.html.erb
+++ b/app/views/admin/communication/websites/show.html.erb
@@ -12,28 +12,27 @@
 <div class="card mt-5">
   <div class="card-header">
     <div class="float-end">
-      <%= link_to t('activerecord.models.communication/website/post.all'),
-                  admin_communication_website_posts_path(website_id: @website),
-                  class: 'me-3' %>
       <%= link_to t('create'),
                   new_admin_communication_website_post_path(website_id: @website),
                   class: button_classes %>
     </div>
-    <h2 class="card-title"><%= Communication::Website::Post.model_name.human(count: 2) %></h2>
+    <h2 class="card-title">
+      <%= link_to Communication::Website::Post.model_name.human(count: 2),
+                  admin_communication_website_posts_path(website_id: @website) %></h2>
   </div>
   <%= render 'admin/communication/website/posts/list', posts: @website.posts.recent %>
 </div>
 <div class="card mt-5">
   <div class="card-header">
     <div class="float-end">
-      <%= link_to t('activerecord.models.communication/website/page.all'),
-                  admin_communication_website_pages_path(website_id: @website),
-                  class: 'me-3' %>
       <%= link_to t('create'),
                   new_admin_communication_website_page_path(website_id: @website),
                   class: button_classes %>
     </div>
-    <h2 class="card-title"><%= Communication::Website::Page.model_name.human(count: 2) %></h2>
+    <h2 class="card-title">
+      <%= link_to Communication::Website::Page.model_name.human(count: 2),
+                  admin_communication_website_pages_path(website_id: @website) %>
+    </h2>
   </div>
   <%= render 'admin/communication/website/pages/list', pages: @website.pages.recent %>
 </div>
-- 
GitLab