From d66d4e8faac328bb3d004887e0c6eaecf3301bd5 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Thu, 9 Sep 2021 09:41:20 +0200
Subject: [PATCH] page

---
 app/assets/stylesheets/admin/forms.sass       |  6 ++--
 .../admin/application_controller.rb           |  2 +-
 .../website/application_controller.rb         |  4 +--
 .../communication/website/pages_controller.rb |  2 +-
 .../journal/application_controller.rb         |  4 +--
 .../research/journal/articles_controller.rb   |  2 +-
 .../admin/research/journals_controller.rb     |  8 ++++-
 app/models/communication/website/page.rb      |  1 -
 app/models/research/journal/article.rb        | 15 +++-----
 app/models/research/journal/volume.rb         | 11 +++---
 app/models/research/researcher.rb             | 23 +++++++++++--
 app/services/github.rb                        |  1 +
 .../website/pages/_form.html.erb              | 34 ++++++++++++-------
 .../website/pages/jekyll.html.erb             |  1 +
 .../research/journal/articles/_form.html.erb  |  1 +
 .../research/journal/articles/jekyll.html.erb |  2 +-
 config/locales/communication/fr.yml           |  8 +++++
 ...d_keywords_to_research_journal_articles.rb |  5 +++
 ..._add_text_to_communication_website_page.rb |  5 +++
 db/migrate/20210909072507_fix_pages.rb        |  6 ++++
 db/schema.rb                                  |  6 ++--
 21 files changed, 100 insertions(+), 47 deletions(-)
 create mode 100644 db/migrate/20210909064436_add_keywords_to_research_journal_articles.rb
 create mode 100644 db/migrate/20210909071812_add_text_to_communication_website_page.rb
 create mode 100644 db/migrate/20210909072507_fix_pages.rb

diff --git a/app/assets/stylesheets/admin/forms.sass b/app/assets/stylesheets/admin/forms.sass
index f0f382c33..45a124c71 100644
--- a/app/assets/stylesheets/admin/forms.sass
+++ b/app/assets/stylesheets/admin/forms.sass
@@ -1,4 +1,6 @@
 .simple_form
-  clear: both
+    clear: both
 .form-group
-  margin-top: 10px
+    margin-top: 10px
+::-webkit-calendar-picker-indicator
+    filter: invert(1)
diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb
index 777f3157b..0e72a394e 100644
--- a/app/controllers/admin/application_controller.rb
+++ b/app/controllers/admin/application_controller.rb
@@ -6,7 +6,7 @@ class Admin::ApplicationController < ApplicationController
   protected
 
   def breadcrumb
-    add_breadcrumb 'Tableau de bord', :admin_root_path
+    add_breadcrumb t('dashboard'), :admin_root_path
   end
 
   def breadcrumb_for(object, **options)
diff --git a/app/controllers/admin/communication/website/application_controller.rb b/app/controllers/admin/communication/website/application_controller.rb
index bc230a8e6..ebf2579c9 100644
--- a/app/controllers/admin/communication/website/application_controller.rb
+++ b/app/controllers/admin/communication/website/application_controller.rb
@@ -4,8 +4,8 @@ class Admin::Communication::Website::ApplicationController < Admin::Communicatio
   protected
 
   def breadcrumb
-    super
-    add_breadcrumb Communication::Website.model_name.human(count: 2), admin_communication_websites_path(journal_id: nil)
+    add_breadcrumb t('dashboard'), :admin_root_path
+    add_breadcrumb '...'
     breadcrumb_for @website, website_id: nil
   end
 
diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb
index 9dbc5af3e..f7ddbfa47 100644
--- a/app/controllers/admin/communication/website/pages_controller.rb
+++ b/app/controllers/admin/communication/website/pages_controller.rb
@@ -55,7 +55,7 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web
   def page_params
     params.require(:communication_website_page)
           .permit(:university_id, :communication_website_id, :title,
-            :description, :about_type, :about_id, :slug, :published_at,
+            :description, :text, :about_type, :about_id, :slug, :published,
             :parent_id)
   end
 end
diff --git a/app/controllers/admin/research/journal/application_controller.rb b/app/controllers/admin/research/journal/application_controller.rb
index 7c57ae0be..43064c7e6 100644
--- a/app/controllers/admin/research/journal/application_controller.rb
+++ b/app/controllers/admin/research/journal/application_controller.rb
@@ -4,8 +4,8 @@ class Admin::Research::Journal::ApplicationController < Admin::Research::Applica
   protected
 
   def breadcrumb
-    super
-    add_breadcrumb Research::Journal.model_name.human(count: 2), admin_research_journals_path(journal_id: nil)
+    add_breadcrumb t('dashboard'), :admin_root_path
+    add_breadcrumb '...'
     breadcrumb_for @journal, journal_id: nil
   end
 
diff --git a/app/controllers/admin/research/journal/articles_controller.rb b/app/controllers/admin/research/journal/articles_controller.rb
index 51cc02d2e..563dac6ab 100644
--- a/app/controllers/admin/research/journal/articles_controller.rb
+++ b/app/controllers/admin/research/journal/articles_controller.rb
@@ -56,6 +56,6 @@ class Admin::Research::Journal::ArticlesController < Admin::Research::Journal::A
   end
 
   def article_params
-    params.require(:research_journal_article).permit(:title, :text, :published_at, :abstract, :references, :research_journal_volume_id, researcher_ids: [])
+    params.require(:research_journal_article).permit(:title, :text, :published_at, :abstract, :references, :keywords, :research_journal_volume_id, researcher_ids: [])
   end
 end
diff --git a/app/controllers/admin/research/journals_controller.rb b/app/controllers/admin/research/journals_controller.rb
index 1f6061bd1..0ded5136e 100644
--- a/app/controllers/admin/research/journals_controller.rb
+++ b/app/controllers/admin/research/journals_controller.rb
@@ -1,4 +1,4 @@
-class Admin::Research::JournalsController < Admin::Research::Journal::ApplicationController
+class Admin::Research::JournalsController < Admin::Research::ApplicationController
   load_and_authorize_resource class: Research::Journal
 
   def index
@@ -45,6 +45,12 @@ class Admin::Research::JournalsController < Admin::Research::Journal::Applicatio
 
   protected
 
+  def breadcrumb
+    super
+    add_breadcrumb Research::Journal.model_name.human(count: 2), admin_research_journals_path(journal_id: nil)
+    breadcrumb_for @journal
+  end
+
   def journal_params
     params.require(:research_journal).permit(:title, :description, :access_token, :repository)
   end
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 75be99418..16bc9c870 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -70,7 +70,6 @@ class Communication::Website::Page < ApplicationRecord
   end
 
   def publish_to_github
-    return if website&.repository.blank?
     github.publish  kind: :pages,
                     file: "#{ id }.html",
                     title: to_s,
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index f3627ed76..063cc29b8 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -45,8 +45,6 @@ class Research::Journal::Article < ApplicationRecord
   protected
 
   def publish_to_github
-    return if journal.website&.repository.blank?
-    github = Github.new journal.website.access_token, journal.website.repository
     github.publish  kind: :articles,
                     file: "#{id}.md",
                     title: title,
@@ -56,14 +54,11 @@ class Research::Journal::Article < ApplicationRecord
                       assigns: { article: self }
                     )
     researchers.each do |researcher|
-      github.publish  kind: :researchers,
-                      file: "#{ researcher.id }.md",
-                      title: researcher.to_s,
-                      data: ApplicationController.render(
-                        template: 'admin/research/researchers/jekyll',
-                        layout: false,
-                        assigns: { researcher: researcher }
-                      )
+      researcher.publish_to_website(journal.website)
     end
   end
+
+  def github
+    @github ||= Github.with_site(journal.website)
+  end
 end
diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb
index bf21a98f0..d3f62db40 100644
--- a/app/models/research/journal/volume.rb
+++ b/app/models/research/journal/volume.rb
@@ -38,13 +38,6 @@ class Research::Journal::Volume < ApplicationRecord
   protected
 
   def publish_to_github
-    return if journal.website&.repository.blank?
-    github = Github.new journal.website.access_token, journal.website.repository
-    data = ApplicationController.render(
-      template: 'admin/research/journal/volumes/jekyll',
-      layout: false,
-      assigns: { volume: self }
-    )
     github.publish  kind: :volumes,
                     file: "#{id}.md",
                     title: title,
@@ -54,4 +47,8 @@ class Research::Journal::Volume < ApplicationRecord
                       assigns: { volume: self }
                     )
   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 bd6217039..e1fe55a2f 100644
--- a/app/models/research/researcher.rb
+++ b/app/models/research/researcher.rb
@@ -21,8 +21,25 @@
 class Research::Researcher < ApplicationRecord
   belongs_to :user, optional: true
   has_and_belongs_to_many :articles, class_name: 'Research::Journal::Article'
+  has_many :journals, through: :articles
 
-  after_save :update_articles
+  after_save :publish_to_github
+
+  def websites
+    @websites ||= journals.collect(&:website).uniq.compact
+  end
+
+  def publish_to_website(website)
+    github = Github.new website.access_token, website.repository
+    github.publish  kind: :researchers,
+                    file: "#{ id }.md",
+                    title: to_s,
+                    data: ApplicationController.render(
+                      template: 'admin/research/researchers/jekyll',
+                      layout: false,
+                      assigns: { researcher: self }
+                    )
+  end
 
   def to_s
     "#{ first_name } #{ last_name }"
@@ -30,7 +47,7 @@ class Research::Researcher < ApplicationRecord
 
   protected
 
-  def update_articles
-    articles.find_each &:save
+  def publish_to_github
+    websites.each { |website| publish_to_website(website) }
   end
 end
diff --git a/app/services/github.rb b/app/services/github.rb
index 7edb4597e..c777f22ce 100644
--- a/app/services/github.rb
+++ b/app/services/github.rb
@@ -23,6 +23,7 @@ class Github
       sha = nil
     end
     commit_message ||= "[#{kind}] Save #{ title }"
+    return unless repository?
     client.create_contents  repository,
                             remote_file,
                             commit_message,
diff --git a/app/views/admin/communication/website/pages/_form.html.erb b/app/views/admin/communication/website/pages/_form.html.erb
index 687ca8ffd..42653ea62 100644
--- a/app/views/admin/communication/website/pages/_form.html.erb
+++ b/app/views/admin/communication/website/pages/_form.html.erb
@@ -1,17 +1,25 @@
 <%= simple_form_for [:admin, page] do |f| %>
-  <div class="row">
-    <div class="col-md-8">
-      <%= f.input :title %>
-      <%= f.input :slug %>
-      <%= f.input :description %>
-    </div>
-    <div class="col-md-4">
-      <%= f.input :published_at, html5: true %>
-      <%= f.association :parent, collection: page.website.pages.where.not(id: page) %>
-      <%= f.association :website, include_blank: false %>
+<div class="row">
+  <div class="col-md-8">
+    <%= f.input :title %>
+    <%= f.input :description %>
+    <%= f.input :text, input_html: { rows: 20 } %>
+  </div>
+  <div class="col-md-4">
+    <div class="card flex-fill w-100">
+      <div class="card-header">
+        <h5 class="card-title mb-0">Metadata</h5>
+      </div>
+      <div class="card-body">
+        <%= f.input :slug %>
+        <%= f.input :published %>
+        <%= f.association :parent, collection: page.website.pages.where.not(id: page) %>
+        <%= f.association :website, include_blank: false %>
+      </div>
     </div>
   </div>
-  <% content_for :buttons do %>
-    <%= submit f %>
-  <% end %>
+</div>
+<% content_for :buttons do %>
+  <%= submit f %>
+<% end %>
 <% end %>
diff --git a/app/views/admin/communication/website/pages/jekyll.html.erb b/app/views/admin/communication/website/pages/jekyll.html.erb
index d4e12cef2..e130d6d2e 100644
--- a/app/views/admin/communication/website/pages/jekyll.html.erb
+++ b/app/views/admin/communication/website/pages/jekyll.html.erb
@@ -3,5 +3,6 @@ title: "<%= @page.title %>"
 permalink: "<%= @page.path %>"
 parent: "<%= @page.parent_id %>"
 description: "<%= @page.description %>"
+text: "<%= @page.text %>"
 ---
 <%= @page.content_without_frontmatter.html_safe %>
diff --git a/app/views/admin/research/journal/articles/_form.html.erb b/app/views/admin/research/journal/articles/_form.html.erb
index 92e78c5ed..073a5f10d 100644
--- a/app/views/admin/research/journal/articles/_form.html.erb
+++ b/app/views/admin/research/journal/articles/_form.html.erb
@@ -8,6 +8,7 @@
     </div>
     <div class="col-md-8">
       <%= f.input :abstract, as: :text, input_html: { rows: 6 } %>
+      <%= f.input :keywords, as: :text, input_html: { rows: 2 } %>
       <%= f.input :text, input_html: { rows: 20 } %>
       <%= f.input :references, as: :text, input_html: { rows: 10 } %>
     </div>
diff --git a/app/views/admin/research/journal/articles/jekyll.html.erb b/app/views/admin/research/journal/articles/jekyll.html.erb
index 084ca62a9..9b9825573 100644
--- a/app/views/admin/research/journal/articles/jekyll.html.erb
+++ b/app/views/admin/research/journal/articles/jekyll.html.erb
@@ -3,12 +3,12 @@ title: "<%= @article.title %>"
 volume: "<%= @article.volume.id %>"
 publication: "<%= @article.published_at %>"
 abstract: "<%= @article.abstract %>"
+keywords: "<%= @article.keywords %>"
 description: "<%= @article.abstract %>"
 references: "<%= @article.references %>"
 authors:
 <% @article.researchers.each do |researcher| %>
 - "<%= researcher.id %>"
 <% end %>
-tags:
 ---
 <%= simple_format @article.text %>
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index b87fa7777..479a20a20 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -18,3 +18,11 @@ fr:
         about_: Aucun sujet (site indépendant)
         about_Research::Journal: Site de revue scientifique
         about_School: Site d'école
+      communication/website/page:
+        published: Publié ?
+        parent: Page parente
+        website: Site Web
+  simple_form:
+    hints:
+      communication/website/page:
+        slug: test
diff --git a/db/migrate/20210909064436_add_keywords_to_research_journal_articles.rb b/db/migrate/20210909064436_add_keywords_to_research_journal_articles.rb
new file mode 100644
index 000000000..256d2c647
--- /dev/null
+++ b/db/migrate/20210909064436_add_keywords_to_research_journal_articles.rb
@@ -0,0 +1,5 @@
+class AddKeywordsToResearchJournalArticles < ActiveRecord::Migration[6.1]
+  def change
+    add_column :research_journal_articles, :keywords, :text
+  end
+end
diff --git a/db/migrate/20210909071812_add_text_to_communication_website_page.rb b/db/migrate/20210909071812_add_text_to_communication_website_page.rb
new file mode 100644
index 000000000..3e5e4df4f
--- /dev/null
+++ b/db/migrate/20210909071812_add_text_to_communication_website_page.rb
@@ -0,0 +1,5 @@
+class AddTextToCommunicationWebsitePage < ActiveRecord::Migration[6.1]
+  def change
+    add_column :communication_website_pages, :text, :text
+  end
+end
diff --git a/db/migrate/20210909072507_fix_pages.rb b/db/migrate/20210909072507_fix_pages.rb
new file mode 100644
index 000000000..13801ec1b
--- /dev/null
+++ b/db/migrate/20210909072507_fix_pages.rb
@@ -0,0 +1,6 @@
+class FixPages < ActiveRecord::Migration[6.1]
+  def change
+    add_column :communication_website_pages, :published, :boolean, default: false
+    remove_column :communication_website_pages, :published_at
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 89830e39f..774a1738d 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_09_08_094427) do
+ActiveRecord::Schema.define(version: 2021_09_09_072507) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -45,13 +45,14 @@ ActiveRecord::Schema.define(version: 2021_09_08_094427) do
     t.text "description"
     t.string "slug"
     t.text "path"
-    t.datetime "published_at"
     t.uuid "parent_id"
     t.integer "position", default: 0, null: false
     t.string "about_type"
     t.uuid "about_id"
     t.datetime "created_at", precision: 6, null: false
     t.datetime "updated_at", precision: 6, null: false
+    t.text "text"
+    t.boolean "published", default: false
     t.index ["about_type", "about_id"], name: "index_communication_website_pages_on_about"
     t.index ["communication_website_id"], name: "index_communication_website_pages_on_communication_website_id"
     t.index ["parent_id"], name: "index_communication_website_pages_on_parent_id"
@@ -105,6 +106,7 @@ ActiveRecord::Schema.define(version: 2021_09_08_094427) do
     t.uuid "updated_by_id"
     t.text "abstract"
     t.text "references"
+    t.text "keywords"
     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"
-- 
GitLab