From 3aef74e6cb74b9d8a56c27059fd39928d53a5aee Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Wed, 15 Dec 2021 17:21:20 +0100
Subject: [PATCH] export

---
 app/models/administration/member.rb           | 82 ++++++++++++++++++-
 app/models/communication/website/category.rb  |  2 +-
 .../communication/website/github_file.rb      |  4 +-
 app/models/communication/website/post.rb      |  1 +
 app/models/concerns/with_github_files.rb      |  2 +-
 .../website/authors/jekyll.html.erb           |  4 +-
 .../website/authors/jekyll_collection.yml.erb |  2 +-
 .../admin/education/teachers/jekyll.html.erb  | 15 ----
 .../teachers/jekyll_collection.yml.erb}       |  8 +-
 ...yll.html.erb => jekyll_collection.yml.erb} |  6 +-
 10 files changed, 96 insertions(+), 30 deletions(-)
 delete mode 100644 app/views/admin/education/teachers/jekyll.html.erb
 rename app/views/admin/{administration/members/jekyll.html.erb => education/teachers/jekyll_collection.yml.erb} (66%)
 rename app/views/admin/research/researchers/{jekyll.html.erb => jekyll_collection.yml.erb} (63%)

diff --git a/app/models/administration/member.rb b/app/models/administration/member.rb
index 997040ab3..857a248c3 100644
--- a/app/models/administration/member.rb
+++ b/app/models/administration/member.rb
@@ -52,6 +52,19 @@ class Administration::Member < ApplicationRecord
                           foreign_key: :education_teacher_id,
                           association_foreign_key: :education_program_id
 
+  has_many :communication_websites,
+           -> { distinct },
+           through: :communication_website_posts,
+           source: :website
+  has_many :research_websites,
+           -> { distinct },
+           through: :research_journal_articles,
+           source: :websites
+  has_many :education_websites,
+           -> { distinct },
+           through: :education_programs,
+           source: :websites
+
   validates_presence_of :first_name, :last_name
   validates_uniqueness_of :email, scope: :university_id, allow_blank: true, if: :will_save_change_to_email?
   validates_format_of :email, with: Devise::email_regexp, allow_blank: true, if: :will_save_change_to_email?
@@ -68,8 +81,73 @@ class Administration::Member < ApplicationRecord
   end
 
   def websites
-    []
-    # TODO
+    Communication::Website.where(id: [
+      communication_website_ids,
+      research_website_ids,
+      education_website_ids
+    ].flatten.uniq)
+  end
+
+  def github_manifest
+    manifest = []
+    manifest.concat(author_github_manifest_items) if is_author?
+    manifest.concat(researcher_github_manifest_items) if is_researcher?
+    manifest.concat(teacher_github_manifest_items) if is_teacher?
+    manifest
+  end
+
+  def author_github_manifest_items
+    [
+      {
+        identifier: "author",
+        generated_path: -> (github_file) { "#{github_file.website.authors_github_directory}/#{slug}.yml" },
+        data: -> (github_file) { ApplicationController.render(
+          template: "admin/communication/website/authors/jekyll",
+          layout: false,
+          assigns: { author: self, github_file: github_file }
+        ) }
+      },
+      {
+        identifier: "author_collection_item",
+        generated_path: -> (github_file) { "_data/authors/#{slug}.yml" },
+        data: -> (github_file) { ApplicationController.render(
+          template: "admin/communication/website/authors/jekyll_collection",
+          formats: [:yml],
+          layout: false,
+          assigns: { author: self, github_file: github_file }
+        ) }
+      }
+    ]
+  end
+
+  def researcher_github_manifest_items
+    [
+      {
+        identifier: "researcher_collection_item",
+        generated_path: -> (github_file) { "_data/researchers/#{slug}.yml" },
+        data: -> (github_file) { ApplicationController.render(
+          template: "admin/research/researchers/jekyll_collection",
+          formats: [:yml],
+          layout: false,
+          assigns: { researcher: self, github_file: github_file }
+        ) }
+      }
+    ]
+  end
+
+  def teacher_github_manifest_items
+    [
+      {
+        identifier: "teacher_collection_item",
+        generated_path: -> (github_file) { "_data/teachers/#{slug}.yml" },
+        data: -> (github_file) { ApplicationController.render(
+          template: "admin/education/teachers/jekyll_collection",
+          formats: [:yml],
+          layout: false,
+          assigns: { teacher: self, github_file: github_file }
+        ) }
+      }
+    ]
   end
 
 end
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 667eca0de..1b08ce315 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -89,7 +89,7 @@ class Communication::Website::Category < ApplicationRecord
   def github_manifest
     super << {
       identifier: "collection_item",
-      generated_path: -> { "_data/categories/#{slug}.yml" },
+      generated_path: -> (github_file) { "_data/categories/#{slug}.yml" },
       data: -> (github_file) { ApplicationController.render(
         template: "admin/communication/website/categories/jekyll_collection",
         formats: [:yml],
diff --git a/app/models/communication/website/github_file.rb b/app/models/communication/website/github_file.rb
index 2abf6433c..00745fe4b 100644
--- a/app/models/communication/website/github_file.rb
+++ b/app/models/communication/website/github_file.rb
@@ -30,7 +30,7 @@ class Communication::Website::GithubFile < ApplicationRecord
     return unless valid_for_publication? && github.valid?
     add_to_batch(github)
     if github.commit_batch(github_commit_message)
-      update_column :github_path, manifest_data[:generated_path].call
+      update_column :github_path, manifest_data[:generated_path].call(self)
     end
   end
   handle_asynchronously :publish, queue: 'default'
@@ -82,7 +82,7 @@ class Communication::Website::GithubFile < ApplicationRecord
 
   def github_params
     {
-      path: manifest_data[:generated_path].call,
+      path: manifest_data[:generated_path].call(self),
       previous_path: github_path,
       data: manifest_data[:data].call(self)
     }
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 2f31471eb..e5e54b9a8 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -41,6 +41,7 @@ class Communication::Website::Post < ApplicationRecord
           dependent: :destroy
   belongs_to :university
   belongs_to :website,
+             class_name: 'Communication::Website',
              foreign_key: :communication_website_id
   belongs_to :author,
              class_name: 'Administration::Member',
diff --git a/app/models/concerns/with_github_files.rb b/app/models/concerns/with_github_files.rb
index e42292cca..750f34cdd 100644
--- a/app/models/concerns/with_github_files.rb
+++ b/app/models/concerns/with_github_files.rb
@@ -28,7 +28,7 @@ module WithGithubFiles
     [
       {
         identifier: "primary",
-        generated_path: -> { github_path_generated },
+        generated_path: -> (github_file) { github_path_generated },
         data: -> (github_file) { to_jekyll(github_file) },
         has_media: true
       }
diff --git a/app/views/admin/communication/website/authors/jekyll.html.erb b/app/views/admin/communication/website/authors/jekyll.html.erb
index c0f06a33a..e563627b9 100644
--- a/app/views/admin/communication/website/authors/jekyll.html.erb
+++ b/app/views/admin/communication/website/authors/jekyll.html.erb
@@ -2,9 +2,11 @@
 title: "<%= @author.to_s %>"
 slug: "<%= @author.slug %>"
 identifier: "<%= @author.id %>"
-permalink: "/<%= @author.website.authors_github_directory %>/<%= @author.slug %>"
+permalink: "/<%= @github_file.website.authors_github_directory %>/<%= @author.slug %>"
 first_name: "<%= @author.first_name %>"
 last_name: "<%= @author.last_name %>"
+phone: "<%= @author.phone %>"
+email: "<%= @author.email %>"
 biography: >
   <%= prepare_for_github @author.biography, @author.university %>
 pagination:
diff --git a/app/views/admin/communication/website/authors/jekyll_collection.yml.erb b/app/views/admin/communication/website/authors/jekyll_collection.yml.erb
index f4582d8a1..76518b727 100644
--- a/app/views/admin/communication/website/authors/jekyll_collection.yml.erb
+++ b/app/views/admin/communication/website/authors/jekyll_collection.yml.erb
@@ -1,3 +1,3 @@
 name: "<%= @author.to_s %>"
 identifier: "<%= @author.id %>"
-link: "/<%= @author.website.authors_github_directory %>/<%= @author.slug %>"
+link: "/<%= @github_file.website.authors_github_directory %>/<%= @author.slug %>"
diff --git a/app/views/admin/education/teachers/jekyll.html.erb b/app/views/admin/education/teachers/jekyll.html.erb
deleted file mode 100644
index 2f03ceced..000000000
--- a/app/views/admin/education/teachers/jekyll.html.erb
+++ /dev/null
@@ -1,15 +0,0 @@
----
-title: "<%= @teacher.to_s %>"
-identifier: "<%= @teacher.id %>"
-first_name: "<%= @teacher.first_name %>"
-last_name: "<%= @teacher.last_name %>"
-slug: "<%= @teacher.slug %>"
-<% if @teacher.programs.any? %>
-programs:
-  <% @teacher.programs.each do |program| %>
-  - "<%= program.id %>"
-  <% end %>
-<% end %>
-biography: >
-  <%= prepare_for_github @teacher.biography, @teacher.university %>
----
diff --git a/app/views/admin/administration/members/jekyll.html.erb b/app/views/admin/education/teachers/jekyll_collection.yml.erb
similarity index 66%
rename from app/views/admin/administration/members/jekyll.html.erb
rename to app/views/admin/education/teachers/jekyll_collection.yml.erb
index 2f03ceced..954df1c55 100644
--- a/app/views/admin/administration/members/jekyll.html.erb
+++ b/app/views/admin/education/teachers/jekyll_collection.yml.erb
@@ -1,15 +1,15 @@
----
 title: "<%= @teacher.to_s %>"
 identifier: "<%= @teacher.id %>"
 first_name: "<%= @teacher.first_name %>"
 last_name: "<%= @teacher.last_name %>"
+phone: "<%= @teacher.phone %>"
+email: "<%= @teacher.email %>"
 slug: "<%= @teacher.slug %>"
-<% if @teacher.programs.any? %>
+<% if @teacher.education_programs.any? %>
 programs:
-  <% @teacher.programs.each do |program| %>
+  <% @teacher.education_programs.each do |program| %>
   - "<%= program.id %>"
   <% end %>
 <% end %>
 biography: >
   <%= prepare_for_github @teacher.biography, @teacher.university %>
----
diff --git a/app/views/admin/research/researchers/jekyll.html.erb b/app/views/admin/research/researchers/jekyll_collection.yml.erb
similarity index 63%
rename from app/views/admin/research/researchers/jekyll.html.erb
rename to app/views/admin/research/researchers/jekyll_collection.yml.erb
index 698fec862..3a539eebb 100644
--- a/app/views/admin/research/researchers/jekyll.html.erb
+++ b/app/views/admin/research/researchers/jekyll_collection.yml.erb
@@ -1,7 +1,7 @@
----
-title: "<%= "#{ @researcher }"%>"
+title: "<%= @researcher.to_s %>"
 first_name: "<%= @researcher.first_name %>"
 last_name: "<%= @researcher.last_name %>"
+phone: "<%= @researcher.phone %>"
+email: "<%= @researcher.email %>"
 biography: >
   <%= prepare_for_github @researcher.biography, @researcher.university %>
----
-- 
GitLab