From ff858992f616d5e0a4a81a18d61dfa4f507bcc27 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 28 Oct 2021 12:17:53 +0200
Subject: [PATCH] author connected to posts

---
 .../admin/communication/website/posts_controller.rb         | 2 +-
 app/models/communication/website/post.rb                    | 6 ++++++
 app/views/admin/communication/website/authors/show.html.erb | 2 +-
 app/views/admin/communication/website/posts/_form.html.erb  | 1 +
 app/views/admin/communication/website/posts/show.html.erb   | 6 ++++++
 config/locales/communication/fr.yml                         | 4 ++--
 config/locales/fr.yml                                       | 4 ++--
 ...211028100510_add_author_to_communication_website_post.rb | 6 ++++++
 db/schema.rb                                                | 5 ++++-
 9 files changed, 29 insertions(+), 7 deletions(-)
 create mode 100644 db/migrate/20211028100510_add_author_to_communication_website_post.rb

diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb
index 1358b60c5..d6a24c155 100644
--- a/app/controllers/admin/communication/website/posts_controller.rb
+++ b/app/controllers/admin/communication/website/posts_controller.rb
@@ -57,6 +57,6 @@ class Admin::Communication::Website::PostsController < Admin::Communication::Web
 
   def post_params
     params.require(:communication_website_post)
-          .permit(:university_id, :website_id, :title, :description, :text, :published, :published_at, :featured_image, :featured_image_delete, :featured_image_infos, :slug, category_ids: [])
+          .permit(:university_id, :website_id, :title, :description, :text, :published, :published_at, :featured_image, :featured_image_delete, :featured_image_infos, :slug, :author_id, category_ids: [])
   end
 end
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 42fd81f8c..83961a465 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -13,16 +13,19 @@
 #  title                    :string
 #  created_at               :datetime         not null
 #  updated_at               :datetime         not null
+#  author_id                :uuid
 #  communication_website_id :uuid             not null
 #  university_id            :uuid             not null
 #
 # Indexes
 #
+#  index_communication_website_posts_on_author_id                 (author_id)
 #  index_communication_website_posts_on_communication_website_id  (communication_website_id)
 #  index_communication_website_posts_on_university_id             (university_id)
 #
 # Foreign Keys
 #
+#  fk_rails_...  (author_id => communication_website_authors.id)
 #  fk_rails_...  (communication_website_id => communication_websites.id)
 #  fk_rails_...  (university_id => universities.id)
 #
@@ -36,6 +39,9 @@ class Communication::Website::Post < ApplicationRecord
   belongs_to :university
   belongs_to :website,
              foreign_key: :communication_website_id
+  belongs_to :author,
+             class_name: 'Communication::Website::Author',
+             optional: true
   has_and_belongs_to_many :categories,
                           class_name: 'Communication::Website::Category',
                           join_table: 'communication_website_categories_posts',
diff --git a/app/views/admin/communication/website/authors/show.html.erb b/app/views/admin/communication/website/authors/show.html.erb
index c8e0111b5..def927cf9 100644
--- a/app/views/admin/communication/website/authors/show.html.erb
+++ b/app/views/admin/communication/website/authors/show.html.erb
@@ -11,7 +11,7 @@
         <p>
           <strong><%= Communication::Website::Author.human_attribute_name('biography') %></strong>
         </p>
-        <%= sanitize @author.biography %>
+        <%= @author.biography %>
       </div>
     </div>
   </div>
diff --git a/app/views/admin/communication/website/posts/_form.html.erb b/app/views/admin/communication/website/posts/_form.html.erb
index 46666ac96..e221e8ec4 100644
--- a/app/views/admin/communication/website/posts/_form.html.erb
+++ b/app/views/admin/communication/website/posts/_form.html.erb
@@ -21,6 +21,7 @@
           <%= f.input :slug, as: :string if post.persisted? %>
           <%= f.input :published %>
           <%= f.input :published_at, html5: true %>
+          <%= f.association :author, collection: @website.authors.ordered if @website.authors.any? %>
           <%= f.association :categories, as: :check_boxes if @website.categories.any? %>
         </div>
       </div>
diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb
index 2a95763dd..54c7a4d3a 100644
--- a/app/views/admin/communication/website/posts/show.html.erb
+++ b/app/views/admin/communication/website/posts/show.html.erb
@@ -25,6 +25,12 @@
       </div>
       <table class="<%= table_classes %>">
         <tbody>
+          <% if @post.author %>
+            <tr>
+              <td width="150"><%= Communication::Website::Post.human_attribute_name('author') %></td>
+              <td><%= link_to_if can?(:read, @post.author), @post.author, admin_communication_website_author_path(@post.author) %></td>
+            </tr>
+          <% end %>
           <tr>
             <td width="150"><%= Communication::Website::Post.human_attribute_name('slug') %></td>
             <td><%= @post.slug %></td>
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 0febf9dec..7123f1095 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -26,8 +26,8 @@ fr:
         one: Site Web
         other: Sites Web
       communication/website/author:
-        one: Auteur
-        other: Auteurs
+        one: Auteur·rice
+        other: Auteur·rice·s
       communication/website/category:
         one: Catégorie
         other: Catégories
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index b34e7ed94..fb5fca43c 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -34,8 +34,8 @@ fr:
         one: Université
         other: Universités
       user:
-        one: Utilisateur
-        other: Utilisateurs
+        one: Utilisateur·rice
+        other: Utilisateur·rice·s
   admin:
     attachment_not_available: Impossible d'accéder à l'élément
     dashboard: Tableau de bord
diff --git a/db/migrate/20211028100510_add_author_to_communication_website_post.rb b/db/migrate/20211028100510_add_author_to_communication_website_post.rb
new file mode 100644
index 000000000..085d9d2a6
--- /dev/null
+++ b/db/migrate/20211028100510_add_author_to_communication_website_post.rb
@@ -0,0 +1,6 @@
+class AddAuthorToCommunicationWebsitePost < ActiveRecord::Migration[6.1]
+  def change
+    add_reference :communication_website_posts, :author, foreign_key: { to_table: :communication_website_authors }, type: :uuid
+    
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 880bc7757..cce82cf82 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_090402) do
+ActiveRecord::Schema.define(version: 2021_10_28_100510) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -239,6 +239,8 @@ ActiveRecord::Schema.define(version: 2021_10_28_090402) do
     t.text "slug"
     t.text "path"
     t.text "github_path"
+    t.uuid "author_id"
+    t.index ["author_id"], name: "index_communication_website_posts_on_author_id"
     t.index ["communication_website_id"], name: "index_communication_website_posts_on_communication_website_id"
     t.index ["university_id"], name: "index_communication_website_posts_on_university_id"
   end
@@ -464,6 +466,7 @@ ActiveRecord::Schema.define(version: 2021_10_28_090402) do
   add_foreign_key "communication_website_pages", "communication_website_pages", column: "parent_id"
   add_foreign_key "communication_website_pages", "communication_websites"
   add_foreign_key "communication_website_pages", "universities"
+  add_foreign_key "communication_website_posts", "communication_website_authors", column: "author_id"
   add_foreign_key "communication_website_posts", "communication_websites"
   add_foreign_key "communication_website_posts", "universities"
   add_foreign_key "communication_websites", "universities"
-- 
GitLab