From f8904d0b41137d7e24945da86395a9a9433d8d9e Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sun, 15 Jan 2023 11:27:41 +0100
Subject: [PATCH] better

---
 .../admin/research/documents_controller.rb    | 28 -----------
 .../admin/research/publications_controller.rb | 28 +++++++++++
 .../admin/research/researchers_controller.rb  |  4 +-
 app/models/research/document.rb               | 40 ---------------
 app/models/research/publication.rb            | 50 +++++++++++++++++++
 app/models/university/person/with_research.rb | 17 +++----
 .../admin/research/documents/_list.html.erb   | 22 --------
 .../admin/research/documents/index.html.erb   | 23 ---------
 .../admin/research/documents/show.html.erb    | 11 ----
 .../research/publications/_list.html.erb      | 22 ++++++++
 .../research/publications/index.html.erb      |  5 ++
 .../admin/research/publications/show.html.erb | 14 ++++++
 .../admin/research/researchers/show.html.erb  |  9 ++--
 config/locales/research/en.yml                |  6 +--
 config/locales/research/fr.yml                |  6 +--
 config/routes/admin/research.rb               |  2 +-
 ...15092626_rename_document_to_publication.rb | 18 +++++++
 db/schema.rb                                  | 48 ++++++++++--------
 18 files changed, 186 insertions(+), 167 deletions(-)
 delete mode 100644 app/controllers/admin/research/documents_controller.rb
 create mode 100644 app/controllers/admin/research/publications_controller.rb
 delete mode 100644 app/models/research/document.rb
 create mode 100644 app/models/research/publication.rb
 delete mode 100644 app/views/admin/research/documents/_list.html.erb
 delete mode 100644 app/views/admin/research/documents/index.html.erb
 delete mode 100644 app/views/admin/research/documents/show.html.erb
 create mode 100644 app/views/admin/research/publications/_list.html.erb
 create mode 100644 app/views/admin/research/publications/index.html.erb
 create mode 100644 app/views/admin/research/publications/show.html.erb
 create mode 100644 db/migrate/20230115092626_rename_document_to_publication.rb

diff --git a/app/controllers/admin/research/documents_controller.rb b/app/controllers/admin/research/documents_controller.rb
deleted file mode 100644
index db3704062..000000000
--- a/app/controllers/admin/research/documents_controller.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-class Admin::Research::DocumentsController < Admin::Research::ApplicationController
-
-  def index
-    @documents = current_university.research_documents.page(params[:page])
-    breadcrumb
-  end
-
-  def show
-    @document = Research::Document.find params[:id]
-    breadcrumb
-  end
-
-  def update
-    @document = Research::Document.find params[:id]
-    # TODO update from api
-    redirect_to admin_research_document_path(@document)
-  end
-
-  protected
-
-  def breadcrumb
-    super
-    add_breadcrumb Research::Document.model_name.human(count: 2),
-                   admin_research_documents_path
-    breadcrumb_for @document
-  end
-
-end
diff --git a/app/controllers/admin/research/publications_controller.rb b/app/controllers/admin/research/publications_controller.rb
new file mode 100644
index 000000000..26ab47d50
--- /dev/null
+++ b/app/controllers/admin/research/publications_controller.rb
@@ -0,0 +1,28 @@
+class Admin::Research::PublicationsController < Admin::Research::ApplicationController
+
+  def index
+    @publications = Research::Publication.ordered.page(params[:page])
+    breadcrumb
+  end
+
+  def show
+    @publication = Research::Publication.find params[:id]
+    breadcrumb
+  end
+
+  def update
+    @publication = Research::Publication.find params[:id]
+    # TODO update from api
+    redirect_to admin_research_publication_path(@publication)
+  end
+
+  protected
+
+  def breadcrumb
+    super
+    add_breadcrumb Research::Publication.model_name.human(count: 2),
+                   admin_research_publications_path
+    breadcrumb_for @publication
+  end
+
+end
diff --git a/app/controllers/admin/research/researchers_controller.rb b/app/controllers/admin/research/researchers_controller.rb
index 86f580fcf..2a894e3be 100644
--- a/app/controllers/admin/research/researchers_controller.rb
+++ b/app/controllers/admin/research/researchers_controller.rb
@@ -10,7 +10,7 @@ class Admin::Research::ResearchersController < Admin::Research::ApplicationContr
   def show
     load
     if @researcher.hal_identity?
-      @researcher.load_research_documents!
+      @researcher.load_research_publications!
     else
       @possible_hal_authors = @researcher.possible_hal_authors
     end
@@ -23,7 +23,7 @@ class Admin::Research::ResearchersController < Admin::Research::ApplicationContr
     @researcher.update_column :hal_doc_identifier, params[:hal_doc_identifier] if params.has_key?(:hal_doc_identifier)
     @researcher.update_column :hal_form_identifier, params[:hal_form_identifier] if params.has_key?(:hal_form_identifier)
     @researcher.update_column :hal_person_identifier, params[:hal_person_identifier] if params.has_key?(:hal_person_identifier)
-    @researcher.load_research_documents!
+    @researcher.load_research_publications!
     redirect_to admin_research_researcher_path(@researcher)
   end
 
diff --git a/app/models/research/document.rb b/app/models/research/document.rb
deleted file mode 100644
index 4b6dcf520..000000000
--- a/app/models/research/document.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-# == Schema Information
-#
-# Table name: research_documents
-#
-#  id                   :uuid             not null, primary key
-#  data                 :jsonb
-#  docid                :string
-#  hal_url              :string
-#  ref                  :string
-#  title                :string
-#  url                  :string
-#  created_at           :datetime         not null
-#  updated_at           :datetime         not null
-#  university_id        :uuid             not null, indexed
-#  university_person_id :uuid             not null, indexed
-#
-# Indexes
-#
-#  index_research_documents_on_university_id         (university_id)
-#  index_research_documents_on_university_person_id  (university_person_id)
-#
-# Foreign Keys
-#
-#  fk_rails_03e727a207  (university_person_id => university_people.id)
-#  fk_rails_ceb4d1344f  (university_id => universities.id)
-#
-class Research::Document < ApplicationRecord
-  include WithUniversity
-
-  belongs_to :person, class_name: 'University::Person', foreign_key: :university_person_id
-
-  def doi_url
-    return unless doi.present?
-    "http://dx.doi.org/#{doi}"
-  end
-
-  def to_s
-    "#{title}"
-  end
-end
diff --git a/app/models/research/publication.rb b/app/models/research/publication.rb
new file mode 100644
index 000000000..b9d50e48a
--- /dev/null
+++ b/app/models/research/publication.rb
@@ -0,0 +1,50 @@
+# == Schema Information
+#
+# Table name: research_publications
+#
+#  id               :uuid             not null, primary key
+#  data             :jsonb
+#  docid            :string
+#  doi              :string
+#  hal_url          :string
+#  publication_date :date
+#  ref              :string
+#  title            :string
+#  url              :string
+#  created_at       :datetime         not null
+#  updated_at       :datetime         not null
+#
+class Research::Publication < ApplicationRecord
+  has_and_belongs_to_many :research_people,
+                          class_name: 'University::Person', 
+                          foreign_key: 'university_person_id',
+                          association_foreign_key: 'research_publication_id'
+  alias :researchers :research_people
+  before_destroy { research_people.clear }
+
+  validates_presence_of :docid
+
+  scope :ordered, -> { order(publication_date: :desc)}
+
+  def self.create_from(doc)
+    publication = where(docid: doc.docid).first_or_create
+    puts "pub-- #{where(docid: doc.docid).count}"
+    publication.title = doc.title_s.first
+    publication.ref = doc.attributes['citationRef_s']
+    publication.hal_url = doc.attributes['uri_s']
+    publication.doi = doc.attributes['doiId_s']
+    publication.publication_date = doc.attributes['publicationDate_tdate']
+    publication.url = doc.attributes['linkExtUrl_s']
+    publication.save
+    publication
+  end
+
+  def doi_url
+    return unless doi.present?
+    "http://dx.doi.org/#{doi}"
+  end
+
+  def to_s
+    "#{title}"
+  end
+end
diff --git a/app/models/university/person/with_research.rb b/app/models/university/person/with_research.rb
index 315119fb8..0bf8fe5e1 100644
--- a/app/models/university/person/with_research.rb
+++ b/app/models/university/person/with_research.rb
@@ -2,27 +2,24 @@ module University::Person::WithResearch
   extend ActiveSupport::Concern
 
   included do
-    has_many :research_documents, class_name: 'Research::Document', foreign_key: :university_person_id
+    has_and_belongs_to_many :research_publications,
+                            class_name: 'Research::Publication', 
+                            foreign_key: 'research_publication_id',
+                            association_foreign_key: 'university_person_id'
   end
 
   def hal_identity?
     hal_form_identifier.present?
   end
 
-  def load_research_documents!
+  def load_research_publications!
     return unless hal_identity?
     response = HalOpenscience::Document.search  "authIdForm_i:#{hal_form_identifier}",
                                                 fields: ["docid", "title_s", "citationRef_s", "uri_s", "*"],
                                                 limit: 1000
     response.results.each do |doc|
-      document = Research::Document.where(university: university, person: self, docid: doc.docid).first_or_create
-      document.title = doc.title_s[0]
-      document.ref = doc.citationRef_s
-      document.hal_url = doc.uri_s
-      document.doi = doc.attributes['doiId_s']
-      document.publication_date = doc.publicationDate_tdate
-      document.url = doc.attributes['linkExtUrl_s']
-      document.save
+      publication = Research::Publication.create_from doc
+      research_publications << publication unless publication.in?(research_publications)
     end
   end
 
diff --git a/app/views/admin/research/documents/_list.html.erb b/app/views/admin/research/documents/_list.html.erb
deleted file mode 100644
index b3b9e38d2..000000000
--- a/app/views/admin/research/documents/_list.html.erb
+++ /dev/null
@@ -1,22 +0,0 @@
-<div class="table-responsive">
-  <table class="<%= table_classes %>">
-    <thead>
-      <tr>
-        <th><%= Research::Document.human_attribute_name('title') %></th>
-        <th><%= Research::Document.human_attribute_name('url_hal') %></th>
-        <th><%= Research::Document.human_attribute_name('url') %></th>
-        <th><%= Research::Document.human_attribute_name('doi') %></th>
-      </tr>
-    </thead>
-    <tbody>
-      <% documents.each do |document| %>
-        <tr>
-          <td><%= link_to document, admin_research_document_path(document) %></td>
-          <td><%= link_to t('open'), document.hal_url, class: button_classes, target: :_blank if document.hal_url %></td>
-          <td><%= link_to t('open'), document.url, class: button_classes, target: :_blank if document.url %></td>
-          <td><%= link_to t('open'), document.doi_url, class: button_classes, target: :_blank, method: :post if document.doi_url %></td>
-        </tr>
-      <% end %>
-    </tbody>
-  </table>
-</div>
\ No newline at end of file
diff --git a/app/views/admin/research/documents/index.html.erb b/app/views/admin/research/documents/index.html.erb
deleted file mode 100644
index 08b034c26..000000000
--- a/app/views/admin/research/documents/index.html.erb
+++ /dev/null
@@ -1,23 +0,0 @@
-<% content_for :title, Research::Document.model_name.human(count: 2) %>
-
-<div class="table-responsive">
-  <table class="<%= table_classes %>">
-    <thead>
-      <tr>
-        <th><%= Research::Document.human_attribute_name('title') %></th>
-        <th><%= Research::Document.human_attribute_name('url') %></th>
-      </tr>
-    </thead>
-
-    <tbody>
-      <% @documents.each do |document| %>
-        <tr>
-          <td><%= link_to document, admin_research_document_path(document) %></td>
-          <td><%= link_to t('open'), document.url, class: button_classes, target: :_blank %></td>
-        </tr>
-      <% end %>
-    </tbody>
-  </table>
-</div>
-
-<%= paginate @documents, theme: 'bootstrap-5' %>
diff --git a/app/views/admin/research/documents/show.html.erb b/app/views/admin/research/documents/show.html.erb
deleted file mode 100644
index 160d6116c..000000000
--- a/app/views/admin/research/documents/show.html.erb
+++ /dev/null
@@ -1,11 +0,0 @@
-<% content_for :title, @document %>
-
-<%= link_to @document.person, admin_research_researcher_path(@document.person) %>
-
-<p><%= @document.docid %></p>
-
-<div><%= sanitize @document.ref %></div>
-
-<% content_for :action_bar_right do %>
-  <%= link_to t('show'), @document.url, class: button_classes, target: :_blank %>
-<% end %>
\ No newline at end of file
diff --git a/app/views/admin/research/publications/_list.html.erb b/app/views/admin/research/publications/_list.html.erb
new file mode 100644
index 000000000..4ebbe4c55
--- /dev/null
+++ b/app/views/admin/research/publications/_list.html.erb
@@ -0,0 +1,22 @@
+<div class="table-responsive">
+  <table class="<%= table_classes %>">
+    <thead>
+      <tr>
+        <th><%= Research::Publication.human_attribute_name('title') %></th>
+        <th><%= t('research.researchers', count: 2) %></th>
+      </tr>
+    </thead>
+    <tbody>
+      <% publications.each do |publication| %>
+        <tr>
+          <td><%= link_to publication, admin_research_publication_path(publication) %></td>
+          <td>
+            <% publication.researchers.each do |researcher| %>
+              <%= link_to researcher, admin_research_researcher_path(researcher) %>
+            <% end %>
+          </td>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+</div>
\ No newline at end of file
diff --git a/app/views/admin/research/publications/index.html.erb b/app/views/admin/research/publications/index.html.erb
new file mode 100644
index 000000000..164e21085
--- /dev/null
+++ b/app/views/admin/research/publications/index.html.erb
@@ -0,0 +1,5 @@
+<% content_for :title, Research::Publication.model_name.human(count: 2) %>
+
+<%= render 'admin/research/publications/list', publications: @publications %>
+
+<%= paginate @publications, theme: 'bootstrap-5' %>
diff --git a/app/views/admin/research/publications/show.html.erb b/app/views/admin/research/publications/show.html.erb
new file mode 100644
index 000000000..060d9294a
--- /dev/null
+++ b/app/views/admin/research/publications/show.html.erb
@@ -0,0 +1,14 @@
+<% content_for :title, @publication %>
+
+<% @publication.research_people.each do |person| %>
+  <%= link_to person, admin_research_researcher_path(person) %>
+<% end %>
+
+<p><%= @publication.docid %></p>
+
+<div><%= sanitize @publication.ref %></div>
+
+<%= link_to 'url', @publication.url, target: :_blank if @publication.url %>
+<%= link_to 'HAL', @publication.hal_url, target: :_blank if @publication.hal_url %>
+<%= link_to 'DOI', @publication.doi_url, target: :_blank if @publication.doi_url %>
+
diff --git a/app/views/admin/research/researchers/show.html.erb b/app/views/admin/research/researchers/show.html.erb
index 93e4ca731..effbfcf0e 100644
--- a/app/views/admin/research/researchers/show.html.erb
+++ b/app/views/admin/research/researchers/show.html.erb
@@ -18,11 +18,14 @@
 
 <div class="card flex-fill w-100">
   <div class="card-header">
-    <h2 class="card-title mb-0 h5"><%= Research::Document.model_name.human(count: 2) %></h2>
+    <h2 class="card-title mb-0 h5">
+      <%= Research::Publication.model_name.human(count: @researcher.research_publications.count) %>
+      (<%= @researcher.research_publications.count %>)
+    </h2>
   </div>
   <% if @researcher.hal_identity? %>
     <div class="table-responsive">
-      <%= render 'admin/research/documents/list', documents: @researcher.research_documents %>
+      <%= render 'admin/research/publications/list', publications: @researcher.research_publications.ordered %>
     </div>
   <% else %>
     <div class="card-body">
@@ -43,8 +46,6 @@
   <% end %>
 </div>
 
-
 <% content_for :action_bar_right do %>
   <%= edit_link @researcher %>
 <% end %>
-
diff --git a/config/locales/research/en.yml b/config/locales/research/en.yml
index 755b4e642..8d19dd547 100644
--- a/config/locales/research/en.yml
+++ b/config/locales/research/en.yml
@@ -4,9 +4,6 @@ en:
       research: Research
   activerecord:
     models:
-      research/document:
-        one: Document
-        other: Documents
       research/journal:
         one: Journal
         other: Journals
@@ -22,6 +19,9 @@ en:
       research/laboratory/axis:
         one: Axis
         other: Axes
+      research/publication:
+        one: Publication
+        other: Publications
       research/thesis:
         one: Thesis
         other: Theses
diff --git a/config/locales/research/fr.yml b/config/locales/research/fr.yml
index 75e62acaf..a02f0ed28 100644
--- a/config/locales/research/fr.yml
+++ b/config/locales/research/fr.yml
@@ -4,9 +4,6 @@ fr:
       research: Recherche
   activerecord:
     models:
-      research/document:
-        one: Document
-        other: Documents
       research/journal:
         one: Revue scientifique
         other: Revues scientifiques
@@ -22,6 +19,9 @@ fr:
       research/laboratory/axis:
         one: Axe
         other: Axes
+      research/publication:
+        one: Publication
+        other: Publications
       research/thesis:
         one: Thèse
         other: Thèses
diff --git a/config/routes/admin/research.rb b/config/routes/admin/research.rb
index f79b5f886..507867bf4 100644
--- a/config/routes/admin/research.rb
+++ b/config/routes/admin/research.rb
@@ -1,6 +1,6 @@
 namespace :research do
   resources :researchers, only: [:index, :show, :update]
-  resources :documents, only: [:index, :show, :update]
+  resources :publications, only: [:index, :show, :update]
   resources :journals do
     resources :volumes, controller: 'journals/volumes'
     resources :papers, controller: 'journals/papers' do
diff --git a/db/migrate/20230115092626_rename_document_to_publication.rb b/db/migrate/20230115092626_rename_document_to_publication.rb
new file mode 100644
index 000000000..b5e43f7a9
--- /dev/null
+++ b/db/migrate/20230115092626_rename_document_to_publication.rb
@@ -0,0 +1,18 @@
+class RenameDocumentToPublication < ActiveRecord::Migration[7.0]
+  def change
+    rename_table :research_documents, :research_publications
+
+    remove_column :research_publications, :university_id, :uuid
+    remove_column :research_publications, :university_person_id, :uuid
+
+    create_join_table :research_publications, :university_people, column_options: {type: :uuid} do |t|
+      t.index [:research_publication_id, :university_person_id], name: 'index_publication_person'
+      t.index [:university_person_id, :research_publication_id], name: 'index_person_publication'
+    end
+
+    create_join_table :research_publications, :research_laboratories, column_options: {type: :uuid} do |t|
+      t.index [:research_publication_id, :research_laboratory_id], name: 'index_publication_laboratory'
+      t.index [:research_laboratory_id, :research_publication_id], name: 'index_laboratory_publication'
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index f06b8ac3c..3a0b5aadd 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[7.0].define(version: 2023_01_14_124308) do
+ActiveRecord::Schema[7.0].define(version: 2023_01_15_092626) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -565,23 +565,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_14_124308) do
     t.string "summernote_locale"
   end
 
-  create_table "research_documents", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
-    t.uuid "university_id", null: false
-    t.uuid "university_person_id", null: false
-    t.string "docid"
-    t.jsonb "data"
-    t.string "title"
-    t.string "url"
-    t.string "ref"
-    t.datetime "created_at", null: false
-    t.datetime "updated_at", null: false
-    t.string "hal_url"
-    t.date "publication_date"
-    t.string "doi"
-    t.index ["university_id"], name: "index_research_documents_on_university_id"
-    t.index ["university_person_id"], name: "index_research_documents_on_university_person_id"
-  end
-
   create_table "research_journal_papers", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
     t.string "title"
     t.datetime "published_at", precision: nil
@@ -656,6 +639,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_14_124308) do
     t.index ["university_id"], name: "index_research_laboratories_on_university_id"
   end
 
+  create_table "research_laboratories_publications", id: false, force: :cascade do |t|
+    t.uuid "research_publication_id", null: false
+    t.uuid "research_laboratory_id", null: false
+    t.index ["research_laboratory_id", "research_publication_id"], name: "index_laboratory_publication"
+    t.index ["research_publication_id", "research_laboratory_id"], name: "index_publication_laboratory"
+  end
+
   create_table "research_laboratory_axes", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "research_laboratory_id", null: false
@@ -670,6 +660,26 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_14_124308) do
     t.index ["university_id"], name: "index_research_laboratory_axes_on_university_id"
   end
 
+  create_table "research_publications", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+    t.string "docid"
+    t.jsonb "data"
+    t.string "title"
+    t.string "url"
+    t.string "ref"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.string "hal_url"
+    t.date "publication_date"
+    t.string "doi"
+  end
+
+  create_table "research_publications_university_people", id: false, force: :cascade do |t|
+    t.uuid "research_publication_id", null: false
+    t.uuid "university_person_id", null: false
+    t.index ["research_publication_id", "university_person_id"], name: "index_publication_person"
+    t.index ["university_person_id", "research_publication_id"], name: "index_person_publication"
+  end
+
   create_table "research_theses", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "research_laboratory_id", null: false
@@ -920,8 +930,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_14_124308) do
   add_foreign_key "education_schools", "universities"
   add_foreign_key "imports", "universities"
   add_foreign_key "imports", "users"
-  add_foreign_key "research_documents", "universities"
-  add_foreign_key "research_documents", "university_people"
   add_foreign_key "research_journal_papers", "research_journal_volumes"
   add_foreign_key "research_journal_papers", "research_journals"
   add_foreign_key "research_journal_papers", "universities"
-- 
GitLab