From f4568a2289d1289deafb61d5b19d69c8bf83a721 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Wed, 12 Jan 2022 10:38:20 +0100
Subject: [PATCH] no more is author

---
 .../website/authors_controller.rb             |  8 +++++---
 .../admin/research/researchers_controller.rb  |  2 +-
 .../admin/university/people_controller.rb     |  8 ++++----
 .../communication/website/imported/author.rb  |  1 -
 .../communication/website/with_abouts.rb      |  4 +++-
 app/models/university/person.rb               |  2 --
 app/models/university/person/administrator.rb |  1 -
 app/models/university/person/author.rb        |  7 +++----
 app/models/university/person/researcher.rb    |  1 -
 app/models/university/person/teacher.rb       |  1 -
 .../website/posts/_form.html.erb              |  2 +-
 .../websites/show/_authors.html.erb           | 19 -------------------
 .../websites/show/_categories.html.erb        | 19 -------------------
 .../websites/show/_menus.html.erb             | 19 -------------------
 .../admin/university/people/_form.html.erb    |  1 -
 ...remove_is_author_from_university_people.rb |  5 +++++
 db/schema.rb                                  |  3 +--
 17 files changed, 23 insertions(+), 80 deletions(-)
 delete mode 100644 app/views/admin/communication/websites/show/_authors.html.erb
 delete mode 100644 app/views/admin/communication/websites/show/_categories.html.erb
 delete mode 100644 app/views/admin/communication/websites/show/_menus.html.erb
 create mode 100644 db/migrate/20220112085221_remove_is_author_from_university_people.rb

diff --git a/app/controllers/admin/communication/website/authors_controller.rb b/app/controllers/admin/communication/website/authors_controller.rb
index 5f71ddf18..e4b694f4b 100644
--- a/app/controllers/admin/communication/website/authors_controller.rb
+++ b/app/controllers/admin/communication/website/authors_controller.rb
@@ -1,13 +1,15 @@
 class Admin::Communication::Website::AuthorsController < Admin::Communication::Website::ApplicationController
 
   def index
-    @authors = current_university.people.authors.accessible_by(current_ability).ordered.page(params[:page])
+    @authors =  @website.authors.accessible_by(current_ability)
+                                .ordered
+                                .page(params[:page])
     breadcrumb
   end
 
   def show
-    @author = current_university.people.authors.accessible_by(current_ability).find(params[:id])
-    @posts = @author.communication_website_posts.ordered.page(params[:page])
+    @author = @website.authors.accessible_by(current_ability).find(params[:id])
+    @posts = @author.communication_website_posts.where(communication_website_id: @website.id).ordered.page(params[:page])
     breadcrumb
   end
 
diff --git a/app/controllers/admin/research/researchers_controller.rb b/app/controllers/admin/research/researchers_controller.rb
index c8f7efb73..ae994e920 100644
--- a/app/controllers/admin/research/researchers_controller.rb
+++ b/app/controllers/admin/research/researchers_controller.rb
@@ -6,7 +6,7 @@ class Admin::Research::ResearchersController < Admin::Research::ApplicationContr
   end
 
   def show
-    @researcher = current_university.people.authors.accessible_by(current_ability).find(params[:id])
+    @researcher = current_university.people.researchers.accessible_by(current_ability).find(params[:id])
     @articles = @researcher.research_journal_articles.ordered.page(params[:page])
     breadcrumb
   end
diff --git a/app/controllers/admin/university/people_controller.rb b/app/controllers/admin/university/people_controller.rb
index 31e4032de..bbce1fa39 100644
--- a/app/controllers/admin/university/people_controller.rb
+++ b/app/controllers/admin/university/people_controller.rb
@@ -55,9 +55,9 @@ class Admin::University::PeopleController < Admin::University::ApplicationContro
   end
 
   def person_params
-    params.require(:university_person)
-          .permit(:first_name, :last_name, :email, :phone, :biography, :slug, :user_id,
-          :is_author, :is_researcher, :is_teacher, :is_administration)
-          .merge(university_id: current_university.id)
+    params.require(:university_person).permit(
+      :first_name, :last_name, :email, :phone, :biography, :slug, :user_id,
+      :is_researcher, :is_teacher, :is_administration
+    ).merge(university_id: current_university.id)
   end
 end
diff --git a/app/models/communication/website/imported/author.rb b/app/models/communication/website/imported/author.rb
index 79689e427..9feb591cb 100644
--- a/app/models/communication/website/imported/author.rb
+++ b/app/models/communication/website/imported/author.rb
@@ -57,7 +57,6 @@ class Communication::Website::Imported::Author < ApplicationRecord
       self.author = University::Person.new university: university
       self.author.last_name = "Doe" # No title yet
       self.author.first_name = "John" # No title yet
-      self.is_author = true
       self.author.save
     end
     puts "Update author #{author.id}"
diff --git a/app/models/communication/website/with_abouts.rb b/app/models/communication/website/with_abouts.rb
index 2019fd536..c10418382 100644
--- a/app/models/communication/website/with_abouts.rb
+++ b/app/models/communication/website/with_abouts.rb
@@ -19,6 +19,8 @@ module Communication::Website::WithAbouts
                 foreign_key: :communication_website_id,
                 dependent: :destroy
 
+    has_many    :authors, -> { distinct }, through: :posts
+
     has_many    :categories,
                 class_name: 'Communication::Website::Category',
                 foreign_key: :communication_website_id,
@@ -49,7 +51,7 @@ module Communication::Website::WithAbouts
 
   def people
     @people ||= begin
-      people = posts.collect(&:author) + posts.collect(&:author).compact.map(&:author)
+      people = authors + authors.compact.map(&:author)
       if about_school?
         people += programs.collect(&:university_people_through_teachers).flatten
         people += programs.collect(&:university_people_through_teachers).flatten.map(&:teacher)
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index f9c448947..abd7bb54f 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -6,7 +6,6 @@
 #  email             :string
 #  first_name        :string
 #  is_administration :boolean
-#  is_author         :boolean
 #  is_researcher     :boolean
 #  is_teacher        :boolean
 #  last_name         :string
@@ -91,7 +90,6 @@ class University::Person < ApplicationRecord
 
   scope :ordered,         -> { order(:last_name, :first_name) }
   scope :administration, -> { where(is_administration: true) }
-  scope :authors,         -> { where(is_author: true) }
   scope :teachers,        -> { where(is_teacher: true) }
   scope :researchers,     -> { where(is_researcher: true) }
 
diff --git a/app/models/university/person/administrator.rb b/app/models/university/person/administrator.rb
index 6b54869f5..2c16151fb 100644
--- a/app/models/university/person/administrator.rb
+++ b/app/models/university/person/administrator.rb
@@ -6,7 +6,6 @@
 #  email             :string
 #  first_name        :string
 #  is_administration :boolean
-#  is_author         :boolean
 #  is_researcher     :boolean
 #  is_teacher        :boolean
 #  last_name         :string
diff --git a/app/models/university/person/author.rb b/app/models/university/person/author.rb
index 3353e96cb..be788bef9 100644
--- a/app/models/university/person/author.rb
+++ b/app/models/university/person/author.rb
@@ -6,7 +6,6 @@
 #  email             :string
 #  first_name        :string
 #  is_administration :boolean
-#  is_author         :boolean
 #  is_researcher     :boolean
 #  is_teacher        :boolean
 #  last_name         :string
@@ -37,8 +36,8 @@ class University::Person::Author < University::Person
   end
 
   def for_website?(website)
-    is_author && communication_website_posts.published
-                                            .where(communication_website_id: website&.id)
-                                            .any?
+    communication_website_posts.published
+                              .where(communication_website_id: website&.id)
+                              .any?
   end
 end
diff --git a/app/models/university/person/researcher.rb b/app/models/university/person/researcher.rb
index 727ee2c3a..f1b7341ad 100644
--- a/app/models/university/person/researcher.rb
+++ b/app/models/university/person/researcher.rb
@@ -6,7 +6,6 @@
 #  email             :string
 #  first_name        :string
 #  is_administration :boolean
-#  is_author         :boolean
 #  is_researcher     :boolean
 #  is_teacher        :boolean
 #  last_name         :string
diff --git a/app/models/university/person/teacher.rb b/app/models/university/person/teacher.rb
index fe13ebe93..6e5f3a761 100644
--- a/app/models/university/person/teacher.rb
+++ b/app/models/university/person/teacher.rb
@@ -6,7 +6,6 @@
 #  email             :string
 #  first_name        :string
 #  is_administration :boolean
-#  is_author         :boolean
 #  is_researcher     :boolean
 #  is_teacher        :boolean
 #  last_name         :string
diff --git a/app/views/admin/communication/website/posts/_form.html.erb b/app/views/admin/communication/website/posts/_form.html.erb
index c96bd6db1..99427bb40 100644
--- a/app/views/admin/communication/website/posts/_form.html.erb
+++ b/app/views/admin/communication/website/posts/_form.html.erb
@@ -38,7 +38,7 @@
           <%= f.input :published %>
           <%= f.input :published_at, html5: true %>
           <%= f.input :pinned %>
-          <%= f.association :author, collection: current_university.people.authors.ordered %>
+          <%= f.association :author, collection: current_university.people.ordered %>
         </div>
       </div>
       <div class="card flex-fill w-100">
diff --git a/app/views/admin/communication/websites/show/_authors.html.erb b/app/views/admin/communication/websites/show/_authors.html.erb
deleted file mode 100644
index 0953a1a41..000000000
--- a/app/views/admin/communication/websites/show/_authors.html.erb
+++ /dev/null
@@ -1,19 +0,0 @@
-<div class="card mb-5">
-  <div class="card-header">
-    <div class="float-end">
-      <%= link_to t('create'),
-                  new_admin_communication_website_author_path(website_id: @website),
-                  class: button_classes %>
-    </div>
-    <h2 class="card-title">
-      <%= link_to admin_communication_website_authors_path(website_id: @website) do %>
-        <%= t('communication.authors', count: 2) %>
-        <small>
-          -
-          <%= t('communication.website.see_all', number: @website.authors.count) %>
-        </small>
-      <% end %>
-    </h2>
-  </div>
-  <%= render 'admin/communication/website/authors/list', authors: @website.authors.ordered.limit(10) %>
-</div>
diff --git a/app/views/admin/communication/websites/show/_categories.html.erb b/app/views/admin/communication/websites/show/_categories.html.erb
deleted file mode 100644
index e4bcfbbf4..000000000
--- a/app/views/admin/communication/websites/show/_categories.html.erb
+++ /dev/null
@@ -1,19 +0,0 @@
-<div class="card mb-5">
-  <div class="card-header">
-    <div class="float-end">
-      <%= link_to t('create'),
-                  new_admin_communication_website_category_path(website_id: @website),
-                  class: button_classes if can?(:create, Communication::Website::Category) %>
-    </div>
-    <h2 class="card-title">
-      <%= link_to admin_communication_website_categories_path(website_id: @website) do %>
-        <%= Communication::Website::Category.model_name.human(count: 2) %>
-        <small>
-          -
-          <%= t('communication.website.see_all', number: @website.categories.count) %>
-        </small>
-      <% end %>
-    </h2>
-  </div>
-  <%= render 'admin/communication/website/categories/list', categories: @website.categories.root.ordered %>
-</div>
diff --git a/app/views/admin/communication/websites/show/_menus.html.erb b/app/views/admin/communication/websites/show/_menus.html.erb
deleted file mode 100644
index c5bec2217..000000000
--- a/app/views/admin/communication/websites/show/_menus.html.erb
+++ /dev/null
@@ -1,19 +0,0 @@
-<div class="card mb-5">
-  <div class="card-header">
-    <div class="float-end">
-      <%= link_to t('create'),
-                  new_admin_communication_website_menu_path(website_id: @website),
-                  class: button_classes if can?(:create, Communication::Website::Menu) %>
-    </div>
-    <h2 class="card-title">
-      <%= link_to admin_communication_website_menus_path(website_id: @website) do %>
-        <%= Communication::Website::Menu.model_name.human(count: 2) %>
-        <small>
-          -
-          <%= t('communication.website.see_all', number: @website.menus.count) %>
-        </small>
-      <% end %>
-    </h2>
-  </div>
-  <%= render 'admin/communication/website/menus/list', menus: @website.menus.ordered %>
-</div>
diff --git a/app/views/admin/university/people/_form.html.erb b/app/views/admin/university/people/_form.html.erb
index 2a083df90..2881ea5b6 100644
--- a/app/views/admin/university/people/_form.html.erb
+++ b/app/views/admin/university/people/_form.html.erb
@@ -46,7 +46,6 @@
           <h5 class="card-title mb-0"><%= University::Person.human_attribute_name('abilities') %></h5>
         </div>
         <div class="card-body">
-          <%= f.input :is_author %>
           <%= f.input :is_teacher %>
           <%= f.input :is_researcher %>
           <%= f.input :is_administration %>
diff --git a/db/migrate/20220112085221_remove_is_author_from_university_people.rb b/db/migrate/20220112085221_remove_is_author_from_university_people.rb
new file mode 100644
index 000000000..96b1d644e
--- /dev/null
+++ b/db/migrate/20220112085221_remove_is_author_from_university_people.rb
@@ -0,0 +1,5 @@
+class RemoveIsAuthorFromUniversityPeople < ActiveRecord::Migration[6.1]
+  def change
+    remove_column :university_people, :is_author, :boolean
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 50acbfe29..44cbf87b6 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: 2022_01_11_101410) do
+ActiveRecord::Schema.define(version: 2022_01_12_085221) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -515,7 +515,6 @@ ActiveRecord::Schema.define(version: 2022_01_11_101410) do
     t.string "last_name"
     t.string "first_name"
     t.string "slug"
-    t.boolean "is_author"
     t.boolean "is_researcher"
     t.boolean "is_teacher"
     t.boolean "is_administration"
-- 
GitLab