From bd1dc60a413fbdbf56d497e4fa98f8b0f9b136e4 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sat, 23 Oct 2021 17:40:02 +0200
Subject: [PATCH] volumes

---
 app/models/communication/website/page.rb      |  2 +-
 app/models/communication/website/post.rb      |  2 +-
 .../website => concerns}/with_github.rb       |  4 +--
 app/models/research/journal/article.rb        |  5 ++++
 app/models/research/journal/volume.rb         | 29 ++++++++++---------
 app/models/research/researcher.rb             | 15 +++++-----
 app/services/github.rb                        |  2 +-
 ...to_volumes_and_articles_and_researchers.rb |  7 +++++
 db/schema.rb                                  |  5 +++-
 9 files changed, 44 insertions(+), 27 deletions(-)
 rename app/models/{communication/website => concerns}/with_github.rb (90%)
 create mode 100644 db/migrate/20211023153416_add_github_path_to_volumes_and_articles_and_researchers.rb

diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index b799c4cd5..a1900da43 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -35,7 +35,7 @@
 
 class Communication::Website::Page < ApplicationRecord
   include WithSlug
-  include Communication::Website::WithGithub
+  include WithGithub
 
   belongs_to :university
   belongs_to :website,
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 31e70ca1c..88e15c76f 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -28,7 +28,7 @@
 #
 class Communication::Website::Post < ApplicationRecord
   include WithSlug
-  include Communication::Website::WithGithub
+  include WithGithub
 
   has_rich_text :text
   has_one_attached_deletable :featured_image
diff --git a/app/models/communication/website/with_github.rb b/app/models/concerns/with_github.rb
similarity index 90%
rename from app/models/communication/website/with_github.rb
rename to app/models/concerns/with_github.rb
index f7d243914..e19f825e4 100644
--- a/app/models/communication/website/with_github.rb
+++ b/app/models/concerns/with_github.rb
@@ -1,4 +1,4 @@
-module Communication::Website::WithGithub
+module WithGithub
   extend ActiveSupport::Concern
 
   included do
@@ -34,5 +34,5 @@ module Communication::Website::WithGithub
                     data: to_jekyll
     update_column :github_path, github_path_generated
   end
-  handle_asynchronously :publish_to_github
+  # handle_asynchronously :publish_to_github
 end
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index 20a415c65..896c54bcc 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -4,6 +4,7 @@
 #
 #  id                         :uuid             not null, primary key
 #  abstract                   :text
+#  github_path                :text
 #  keywords                   :text
 #  published_at               :date
 #  references                 :text
@@ -47,6 +48,10 @@ class Research::Journal::Article < ApplicationRecord
     "/assets/articles/#{id}/#{pdf.filename}"
   end
 
+  def website
+    journal.website
+  end
+
   def to_s
     "#{ title }"
   end
diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb
index 86e614af0..ce5f8aa08 100644
--- a/app/models/research/journal/volume.rb
+++ b/app/models/research/journal/volume.rb
@@ -4,6 +4,7 @@
 #
 #  id                  :uuid             not null, primary key
 #  description         :text
+#  github_path         :text
 #  keywords            :text
 #  number              :integer
 #  published_at        :date
@@ -24,12 +25,12 @@
 #  fk_rails_...  (university_id => universities.id)
 #
 class Research::Journal::Volume < ApplicationRecord
+  include WithGithub
+
   belongs_to :university
   belongs_to :journal, foreign_key: :research_journal_id
   has_many :articles, foreign_key: :research_journal_volume_id
 
-  after_commit :publish_to_github
-
   has_one_attached :cover
 
   scope :ordered, -> { order(number: :desc, published_at: :desc) }
@@ -38,25 +39,25 @@ class Research::Journal::Volume < ApplicationRecord
     "/assets/img/volumes/#{id}#{cover.filename.extension_with_delimiter}"
   end
 
+  def website
+    journal.website
+  end
+
   def to_s
     "##{ number } #{ title }"
   end
 
   protected
 
-  def publish_to_github
-    github.publish  kind: :volumes,
-                    file: "#{id}.md",
-                    title: title,
-                    data: ApplicationController.render(
-                      template: 'admin/research/journal/volumes/jekyll',
-                      layout: false,
-                      assigns: { volume: self }
-                    )
-    github.send_file cover, cover_path if cover.attached?
+  def github_path_generated
+    "_volumes/#{id}.html"
   end
 
-  def github
-    @github ||= Github.with_site(journal.website)
+  def to_jekyll
+    ApplicationController.render(
+      template: 'admin/research/journal/volumes/jekyll',
+      layout: false,
+      assigns: { volume: self }
+    )
   end
 end
diff --git a/app/models/research/researcher.rb b/app/models/research/researcher.rb
index 8504f16ae..6b85efede 100644
--- a/app/models/research/researcher.rb
+++ b/app/models/research/researcher.rb
@@ -2,13 +2,14 @@
 #
 # Table name: research_researchers
 #
-#  id         :uuid             not null, primary key
-#  biography  :text
-#  first_name :string
-#  last_name  :string
-#  created_at :datetime         not null
-#  updated_at :datetime         not null
-#  user_id    :uuid
+#  id          :uuid             not null, primary key
+#  biography   :text
+#  first_name  :string
+#  github_path :text
+#  last_name   :string
+#  created_at  :datetime         not null
+#  updated_at  :datetime         not null
+#  user_id     :uuid
 #
 # Indexes
 #
diff --git a/app/services/github.rb b/app/services/github.rb
index 15962abe4..5f029c5d9 100644
--- a/app/services/github.rb
+++ b/app/services/github.rb
@@ -2,7 +2,7 @@ class Github
   attr_reader :access_token, :repository
 
   def self.with_site(site)
-    new site.access_token, site.repository
+    new site&.access_token, site&.repository
   end
 
   def initialize(access_token, repository)
diff --git a/db/migrate/20211023153416_add_github_path_to_volumes_and_articles_and_researchers.rb b/db/migrate/20211023153416_add_github_path_to_volumes_and_articles_and_researchers.rb
new file mode 100644
index 000000000..970835899
--- /dev/null
+++ b/db/migrate/20211023153416_add_github_path_to_volumes_and_articles_and_researchers.rb
@@ -0,0 +1,7 @@
+class AddGithubPathToVolumesAndArticlesAndResearchers < ActiveRecord::Migration[6.1]
+  def change
+    add_column :research_journal_articles, :github_path, :text
+    add_column :research_journal_volumes, :github_path, :text
+    add_column :research_researchers, :github_path, :text
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index a4a758579..3449b82fb 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2021_10_23_063050) do
+ActiveRecord::Schema.define(version: 2021_10_23_153416) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -272,6 +272,7 @@ ActiveRecord::Schema.define(version: 2021_10_23_063050) do
     t.text "abstract"
     t.text "references"
     t.text "keywords"
+    t.text "github_path"
     t.index ["research_journal_id"], name: "index_research_journal_articles_on_research_journal_id"
     t.index ["research_journal_volume_id"], name: "index_research_journal_articles_on_research_journal_volume_id"
     t.index ["university_id"], name: "index_research_journal_articles_on_university_id"
@@ -295,6 +296,7 @@ ActiveRecord::Schema.define(version: 2021_10_23_063050) do
     t.datetime "updated_at", precision: 6, null: false
     t.text "description"
     t.text "keywords"
+    t.text "github_path"
     t.index ["research_journal_id"], name: "index_research_journal_volumes_on_research_journal_id"
     t.index ["university_id"], name: "index_research_journal_volumes_on_university_id"
   end
@@ -318,6 +320,7 @@ ActiveRecord::Schema.define(version: 2021_10_23_063050) do
     t.uuid "user_id"
     t.datetime "created_at", precision: 6, null: false
     t.datetime "updated_at", precision: 6, null: false
+    t.text "github_path"
     t.index ["user_id"], name: "index_research_researchers_on_user_id"
   end
 
-- 
GitLab