From 4464149185eb759526604aa976d35243f8660167 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 28 Oct 2021 15:04:10 +0200
Subject: [PATCH] authors connected to posts via import

---
 .../communication/websites_controller.rb      |  1 +
 .../communication/website/imported/post.rb    | 10 ++++++-
 .../communication/website/imported/website.rb | 15 ++++++----
 .../website/posts/_list.html.erb              |  2 ++
 .../communication/websites/import.html.erb    | 29 +++++++++++++++++++
 config/locales/communication/en.yml           |  3 +-
 config/locales/communication/fr.yml           |  1 +
 ...841_add_infos_to_website_imported_posts.rb |  6 ++++
 db/schema.rb                                  |  6 ++--
 9 files changed, 64 insertions(+), 9 deletions(-)
 create mode 100644 db/migrate/20211028123841_add_infos_to_website_imported_posts.rb

diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index 48bf0cbcd..91676f655 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -22,6 +22,7 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati
     @imported_website = @website.imported_website
     @imported_pages = @imported_website.pages.page params[:pages_page]
     @imported_posts = @imported_website.posts.page params[:posts_page]
+    @imported_authors = @imported_website.authors.page params[:authors_page]
     @imported_categories = @imported_website.categories
     @imported_media = @imported_website.media.includes(file_attachment: :blob ).page params[:media_page]
     @imported_media_total_size = @imported_website.media.joins(file_attachment: :blob).sum(:byte_size)
diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb
index 9920f80b6..5cfb69d22 100644
--- a/app/models/communication/website/imported/post.rb
+++ b/app/models/communication/website/imported/post.rb
@@ -3,6 +3,8 @@
 # Table name: communication_website_imported_posts
 #
 #  id                 :uuid             not null, primary key
+#  author             :string
+#  categories         :jsonb
 #  content            :text
 #  data               :jsonb
 #  excerpt            :text
@@ -60,6 +62,8 @@ class Communication::Website::Imported::Post < ApplicationRecord
     self.title = value['title']['rendered']
     self.excerpt = value['excerpt']['rendered']
     self.content = value['content']['rendered']
+    self.author = value['author']
+    self.categories = value['categories']
     self.created_at = value['date_gmt']
     self.updated_at = value['modified_gmt']
     self.published_at = value['date_gmt']
@@ -82,7 +86,7 @@ class Communication::Website::Imported::Post < ApplicationRecord
       # Continue only if there are remote changes
       # Don't touch if there are local changes (post.updated_at > updated_at)
       # Don't touch if there are no remote changes (post.updated_at == updated_at)
-      return unless updated_at > post.updated_at
+      # return unless updated_at > post.updated_at
     end
     puts "Update post #{post.id}"
     sanitized_title = Wordpress.clean_string self.title.to_s
@@ -94,6 +98,10 @@ class Communication::Website::Imported::Post < ApplicationRecord
     post.updated_at = updated_at
     post.published_at = published_at if published_at
     post.published = true
+
+    imported_author = website.authors.where(identifier: author).first
+    post.author = imported_author.author if imported_author.author
+
     post.save
   end
 
diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb
index a740032f9..d758f8456 100644
--- a/app/models/communication/website/imported/website.rb
+++ b/app/models/communication/website/imported/website.rb
@@ -24,15 +24,20 @@ class Communication::Website::Imported::Website < ApplicationRecord
   belongs_to :website,
              class_name: 'Communication::Website'
   has_many   :authors,
-             class_name: 'Communication::Website::Imported::Author'
+             class_name: 'Communication::Website::Imported::Author',
+             dependent: :destroy
   has_many   :categories,
-             class_name: 'Communication::Website::Imported::Category'
+             class_name: 'Communication::Website::Imported::Category',
+             dependent: :destroy
   has_many   :media,
-             class_name: 'Communication::Website::Imported::Medium'
+             class_name: 'Communication::Website::Imported::Medium',
+             dependent: :destroy
   has_many   :pages,
-             class_name: 'Communication::Website::Imported::Page'
+             class_name: 'Communication::Website::Imported::Page',
+             dependent: :destroy
   has_many   :posts,
-             class_name: 'Communication::Website::Imported::Post'
+             class_name: 'Communication::Website::Imported::Post',
+             dependent: :destroy
 
   def run!
     sync_authors
diff --git a/app/views/admin/communication/website/posts/_list.html.erb b/app/views/admin/communication/website/posts/_list.html.erb
index 615d26fdd..0f4626792 100644
--- a/app/views/admin/communication/website/posts/_list.html.erb
+++ b/app/views/admin/communication/website/posts/_list.html.erb
@@ -3,6 +3,7 @@
     <tr>
       <th><%= Communication::Website::Post.human_attribute_name('title') %></th>
       <th><%= Communication::Website::Post.human_attribute_name('featured_image') %></th>
+      <th><%= Communication::Website::Post.human_attribute_name('author') %></th>
       <th><%= Communication::Website::Post.human_attribute_name('published_at') %></th>
       <th></th>
     </tr>
@@ -13,6 +14,7 @@
         <td><%= link_to post, admin_communication_website_post_path(website_id: post.website.id, id: post.id) %></td>
         <td><%= image_tag post.featured_image.representation(resize: 'x100'),
                           height: 50 if post.featured_image.attached? && post.featured_image.representable? %></td>
+        <td><%= link_to post.author, admin_communication_website_author_path(website_id: post.website.id, id: post.author.id) if post.author %></td>
         <td><small><%= l post.published_at, format: :date_with_hour if post.published_at %></small></td>
         <td class="text-end">
           <div class="btn-group" role="group">
diff --git a/app/views/admin/communication/websites/import.html.erb b/app/views/admin/communication/websites/import.html.erb
index 0510ac65e..ca2c0ae55 100644
--- a/app/views/admin/communication/websites/import.html.erb
+++ b/app/views/admin/communication/websites/import.html.erb
@@ -104,6 +104,35 @@
   </div>
 </div>
 
+<div class="card mt-5">
+  <div class="card-header">
+    <h2><%= @imported_authors.total_count %> authors</h2>
+  </div>
+  <table class="<%= table_classes %>">
+    <thead>
+      <tr>
+        <th><%= Communication::Website::Author.human_attribute_name('name') %></th>
+        <th class="text-end" width="150">&nbsp;</th>
+      </tr>
+    </thead>
+    <tbody>
+      <% @imported_authors.each do |author| %>
+        <tr>
+          <td><%= author.name %></td>
+          <td class="text-end">
+            <%= link_to t('show'),
+                          admin_communication_website_author_path(website_id: author.author.website.id, id: author.author.id),
+                          class: button_classes %>
+          </td>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+  <div class="card-footer">
+    <%= paginate @imported_authors, param_name: :authors_page, theme: 'bootstrap-5' %>
+  </div>
+</div>
+
 <div class="card mt-5">
   <div class="card-header">
     <h2><%= @imported_categories.count %> categories</h2>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 1ed44364d..ef08b6006 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -80,11 +80,12 @@ en:
         slug: Slug
         website: Website
       communication/website/post:
-        title: Title
+        author: Author
         description: Description (SEO)
         featured_image: Featured image
         slug: Slug
         text: Text
+        title: Title
         published: Published ?
         published_at: Publication date
         website: Website
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 7123f1095..56f05e12d 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -79,6 +79,7 @@ fr:
         title: Titre
         website: Site Web
       communication/website/post:
+        author: Auteur·rice
         categories: Categories
         description: Description (SEO)
         featured_image: Image à la une
diff --git a/db/migrate/20211028123841_add_infos_to_website_imported_posts.rb b/db/migrate/20211028123841_add_infos_to_website_imported_posts.rb
new file mode 100644
index 000000000..369cb9c85
--- /dev/null
+++ b/db/migrate/20211028123841_add_infos_to_website_imported_posts.rb
@@ -0,0 +1,6 @@
+class AddInfosToWebsiteImportedPosts < ActiveRecord::Migration[6.1]
+  def change
+    add_column :communication_website_imported_posts, :author, :string
+    add_column :communication_website_imported_posts, :categories, :jsonb
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 451db244a..f89c01b42 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_28_120556) do
+ActiveRecord::Schema.define(version: 2021_10_28_123841) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -204,6 +204,8 @@ ActiveRecord::Schema.define(version: 2021_10_28_120556) do
     t.text "slug"
     t.jsonb "data"
     t.uuid "featured_medium_id"
+    t.string "author"
+    t.jsonb "categories"
     t.index ["featured_medium_id"], name: "idx_communication_website_imported_posts_on_featured_medium_id"
     t.index ["post_id"], name: "index_communication_website_imported_posts_on_post_id"
     t.index ["university_id"], name: "index_communication_website_imported_posts_on_university_id"
@@ -340,7 +342,7 @@ ActiveRecord::Schema.define(version: 2021_10_28_120556) do
     t.uuid "research_journal_id", null: false
     t.uuid "research_journal_volume_id"
     t.datetime "created_at", precision: 6, null: false
-    t.date "updated_at", null: false
+    t.datetime "updated_at", precision: 6, null: false
     t.uuid "updated_by_id"
     t.text "abstract"
     t.text "references"
-- 
GitLab