From 413d0200707a8c25a08f8da8b5bf0d5c1651842f Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sat, 23 Oct 2021 18:02:28 +0200
Subject: [PATCH] articles and researchers

---
 app/models/research/journal/article.rb | 31 +++++++++++++-------------
 app/models/research/researcher.rb      | 19 +++++++++-------
 app/services/github.rb                 | 19 ++++------------
 3 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index 896c54bcc..6dbbde909 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -32,13 +32,15 @@
 #  fk_rails_...  (updated_by_id => users.id)
 #
 class Research::Journal::Article < ApplicationRecord
+  include WithGithub
+
   belongs_to :university
   belongs_to :journal, foreign_key: :research_journal_id
   belongs_to :volume, foreign_key: :research_journal_volume_id, optional: true
   belongs_to :updated_by, class_name: 'User'
   has_and_belongs_to_many :researchers, class_name: 'Research::Researcher'
 
-  after_commit :publish_to_github
+  after_commit :update_researchers
 
   has_one_attached :pdf
 
@@ -58,22 +60,21 @@ class Research::Journal::Article < ApplicationRecord
 
   protected
 
-  def publish_to_github
-    github.publish  kind: :articles,
-                    file: "#{id}.md",
-                    title: title,
-                    data: ApplicationController.render(
-                      template: 'admin/research/journal/articles/jekyll',
-                      layout: false,
-                      assigns: { article: self }
-                    )
+  def github_path_generated
+    "_articles/#{id}.html"
+  end
+
+  def to_jekyll
+    ApplicationController.render(
+      template: 'admin/research/journal/articles/jekyll',
+      layout: false,
+      assigns: { article: self }
+    )
+  end
+
+  def update_researchers
     researchers.each do |researcher|
       researcher.publish_to_website(journal.website)
     end
-    github.send_file pdf, pdf_path if pdf.attached?
-  end
-
-  def github
-    @github ||= Github.with_site(journal.website)
   end
 end
diff --git a/app/models/research/researcher.rb b/app/models/research/researcher.rb
index 6b85efede..3ff523338 100644
--- a/app/models/research/researcher.rb
+++ b/app/models/research/researcher.rb
@@ -32,14 +32,9 @@ class Research::Researcher < ApplicationRecord
 
   def publish_to_website(website)
     github = Github.new website.access_token, website.repository
-    github.publish  kind: :authors,
-                    file: "#{ id }.md",
-                    title: to_s,
-                    data: ApplicationController.render(
-                      template: 'admin/research/researchers/jekyll',
-                      layout: false,
-                      assigns: { researcher: self }
-                    )
+    github.publish  path: "_authors/#{ id }.md",
+                    data: to_jekyll,
+                    commit: "[Researcher] Save #{to_s}"
   end
 
   def to_s
@@ -48,6 +43,14 @@ class Research::Researcher < ApplicationRecord
 
   protected
 
+  def to_jekyll
+    ApplicationController.render(
+      template: 'admin/research/researchers/jekyll',
+      layout: false,
+      assigns: { researcher: self }
+    )
+  end
+
   def publish_to_github
     websites.each { |website| publish_to_website(website) }
   end
diff --git a/app/services/github.rb b/app/services/github.rb
index 5f029c5d9..ce50a1d6e 100644
--- a/app/services/github.rb
+++ b/app/services/github.rb
@@ -10,22 +10,11 @@ class Github
     @repository = repository
   end
 
-  def publish(kind: nil, # Deprecated
-              file: nil, # Deprecated
-              title: nil, # Deprecated
-              path: nil,
+  def publish(path: nil,
               previous_path: nil,
               commit: nil,
               data:)
-    if path
-      local_path = "#{ tmp_directory }/#{ path }"
-      remote_file = path
-    else
-      # Deprecated
-      local_path = "#{ tmp_directory }/#{ file }"
-      remote_file = "_#{ kind }/#{ file }"
-      commit = "Save #{ title }"
-    end
+    local_path = "#{ tmp_directory }/#{ path }"
     Pathname(local_path).dirname.mkpath
     File.write local_path, data
     return if repository.blank?
@@ -33,10 +22,10 @@ class Github
       move_file previous_path, path
     end
     client.create_contents  repository,
-                            remote_file,
+                            path,
                             commit,
                             file: local_path,
-                            sha: file_sha(remote_file)
+                            sha: file_sha(path)
   rescue
     # byebug
   end
-- 
GitLab