From 75bdc95e356fc9531e0f580d5e1559176b877814 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Tue, 11 Jan 2022 11:12:39 +0100
Subject: [PATCH] article published & position

---
 .../research/journal/articles_controller.rb   |  4 ++-
 .../research/journal/volumes_controller.rb    |  1 +
 .../admin/research/journals_controller.rb     |  1 +
 app/models/research/journal/article.rb        | 22 ++++++++++--
 .../research/journal/articles/_form.html.erb  |  1 +
 .../research/journal/articles/_list.html.erb  |  6 +++-
 .../research/journal/articles/show.html.erb   |  6 +++-
 .../research/journal/articles/static.html.erb |  2 +-
 .../research/journal/volumes/show.html.erb    | 36 +++++++++++++++++--
 .../admin/research/journals/show.html.erb     |  2 +-
 config/locales/research/en.yml                |  1 +
 config/locales/research/fr.yml                |  1 +
 config/routes/admin/research.rb               |  6 +++-
 ...d_position_to_research_journal_articles.rb |  6 ++++
 db/schema.rb                                  |  4 ++-
 lib/tasks/app.rake                            |  9 +++++
 16 files changed, 97 insertions(+), 11 deletions(-)
 create mode 100644 db/migrate/20220111092324_add_published_and_position_to_research_journal_articles.rb

diff --git a/app/controllers/admin/research/journal/articles_controller.rb b/app/controllers/admin/research/journal/articles_controller.rb
index f42168be2..4cc7c2cda 100644
--- a/app/controllers/admin/research/journal/articles_controller.rb
+++ b/app/controllers/admin/research/journal/articles_controller.rb
@@ -1,6 +1,8 @@
 class Admin::Research::Journal::ArticlesController < Admin::Research::Journal::ApplicationController
   load_and_authorize_resource class: Research::Journal::Article, through: :journal
 
+  include Admin::Reorderable
+
   def index
     breadcrumb
   end
@@ -58,7 +60,7 @@ class Admin::Research::Journal::ArticlesController < Admin::Research::Journal::A
 
   def article_params
     params.require(:research_journal_article)
-          .permit(:title, :slug, :text, :published_at, :abstract, :pdf, :references, :keywords, :research_journal_volume_id, researcher_ids: [])
+          .permit(:title, :slug, :text, :published, :published_at, :abstract, :pdf, :references, :keywords, :research_journal_volume_id, researcher_ids: [])
           .merge(university_id: current_university.id)
   end
 end
diff --git a/app/controllers/admin/research/journal/volumes_controller.rb b/app/controllers/admin/research/journal/volumes_controller.rb
index c470f7e80..a07b2849b 100644
--- a/app/controllers/admin/research/journal/volumes_controller.rb
+++ b/app/controllers/admin/research/journal/volumes_controller.rb
@@ -7,6 +7,7 @@ class Admin::Research::Journal::VolumesController < Admin::Research::Journal::Ap
   end
 
   def show
+    @articles = @volume.articles.ordered
     breadcrumb
   end
 
diff --git a/app/controllers/admin/research/journals_controller.rb b/app/controllers/admin/research/journals_controller.rb
index 343082a6e..4cecdb213 100644
--- a/app/controllers/admin/research/journals_controller.rb
+++ b/app/controllers/admin/research/journals_controller.rb
@@ -10,6 +10,7 @@ class Admin::Research::JournalsController < Admin::Research::ApplicationControll
   end
 
   def show
+    @articles = @journal.articles.order(published_at: :desc, created_at: :desc).limit(10)
     breadcrumb
   end
 
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index e4c27aa30..1ab24435d 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -6,6 +6,8 @@
 #  abstract                   :text
 #  keywords                   :text
 #  old_text                   :text
+#  position                   :integer
+#  published                  :boolean          default(FALSE)
 #  published_at               :date
 #  references                 :text
 #  slug                       :string
@@ -33,6 +35,7 @@
 #
 class Research::Journal::Article < ApplicationRecord
   include WithGit
+  include WithPosition
 
   has_rich_text :text
   has_one_attached :pdf
@@ -47,9 +50,11 @@ class Research::Journal::Article < ApplicationRecord
                           association_foreign_key: :researcher_id
   has_many :websites, -> { distinct }, through: :journal
 
-  validates :title, :published_at, presence: true
+  validates :title, presence: true
 
-  scope :ordered, -> { order(:published_at, :created_at) }
+  before_validation :set_published_at, if: :published_changed?
+
+  scope :published, -> { where(published: true) }
 
   def git_path(website)
     "content/articles/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published_at
@@ -62,4 +67,17 @@ class Research::Journal::Article < ApplicationRecord
   def to_s
     "#{ title }"
   end
+
+  protected
+
+  def last_ordered_element
+    Research::Journal::Article.where(
+      university_id: university_id,
+      research_journal_volume_id: research_journal_volume_id
+    ).ordered.last
+  end
+
+  def set_published_at
+    self.published_at = published? ? Time.zone.now : nil
+  end
 end
diff --git a/app/views/admin/research/journal/articles/_form.html.erb b/app/views/admin/research/journal/articles/_form.html.erb
index 60f8b1943..c52f9d1e7 100644
--- a/app/views/admin/research/journal/articles/_form.html.erb
+++ b/app/views/admin/research/journal/articles/_form.html.erb
@@ -27,6 +27,7 @@
         </div>
         <div class="card-body">
           <%= f.association :volume, collection: @journal.volumes, label: Research::Journal::Volume.model_name.human %>
+          <%= f.input :published %>
           <%= f.input :published_at, html5: true %>
           <%= f.input :keywords, as: :text, input_html: { rows: 2 } %>
           <%= f.association :researchers, collection: current_university.people.researchers.ordered, as: :check_boxes %>
diff --git a/app/views/admin/research/journal/articles/_list.html.erb b/app/views/admin/research/journal/articles/_list.html.erb
index 806c59711..a14b7154c 100644
--- a/app/views/admin/research/journal/articles/_list.html.erb
+++ b/app/views/admin/research/journal/articles/_list.html.erb
@@ -9,7 +9,11 @@
   <tbody>
     <% articles.each do |article| %>
       <tr>
-        <td><%= link_to article, admin_research_journal_article_path(journal_id: article.journal, id: article) %></td>
+        <td>
+          <%= link_to article,
+                      admin_research_journal_article_path(journal_id: article.journal, id: article),
+                      class: "#{'opacity-50' unless article.published?}" %>
+        </td>
         <td><%= article.published_at %></td>
         <td class="text-end">
           <div class="btn-group" role="group">
diff --git a/app/views/admin/research/journal/articles/show.html.erb b/app/views/admin/research/journal/articles/show.html.erb
index 3d1b9e68b..ab0dfb960 100644
--- a/app/views/admin/research/journal/articles/show.html.erb
+++ b/app/views/admin/research/journal/articles/show.html.erb
@@ -30,8 +30,12 @@
           <h3 class="h5"><%= Research::Journal::Article.human_attribute_name('volume') %></h3>
           <p><%= link_to @article.volume, [:admin, @article.volume] %></p>
         <% end %>
+        <h3 class="h5"><%= Research::Journal::Article.human_attribute_name('published') %></h3>
+        <p><%= t @article.published %></p>
+        <% if @article.published? && @article.published_at.present? %>
           <h3 class="h5"><%= Research::Journal::Article.human_attribute_name('published_at') %></h3>
-          <p><%=l @article.published_at.to_time, format: :date_with_explicit_month %></p>
+          <p><%= l @article.published_at.to_time, format: :date_with_explicit_month %></p>
+        <% end %>
         <% unless @article.keywords.blank? %>
           <h3 class="h5"><%= Research::Journal::Article.human_attribute_name('keywords') %></h3>
           <p><%= @article.keywords %></p>
diff --git a/app/views/admin/research/journal/articles/static.html.erb b/app/views/admin/research/journal/articles/static.html.erb
index 1f6f16844..ebeb64a93 100644
--- a/app/views/admin/research/journal/articles/static.html.erb
+++ b/app/views/admin/research/journal/articles/static.html.erb
@@ -6,7 +6,7 @@ description: >
 volumes:
   - "<%= @article.volume.path %>"
 <% end %>
-weight: 1
+weight: <%= @article.position %>
 date: <%= @article.published_at %> UTC
 keywords: >
   <%= @article.keywords %>
diff --git a/app/views/admin/research/journal/volumes/show.html.erb b/app/views/admin/research/journal/volumes/show.html.erb
index 403477a98..da93defb4 100644
--- a/app/views/admin/research/journal/volumes/show.html.erb
+++ b/app/views/admin/research/journal/volumes/show.html.erb
@@ -9,9 +9,41 @@
       <div class="card-body">
         <h3 class="h5"><%= Research::Journal::Volume.human_attribute_name('description') %></h3>
         <p><%= @volume.description %></p>
-        <% if @volume.articles.any? %>
+        <% if @articles.any? %>
           <h3 class="h5 mt-4"><%= Research::Journal::Volume.human_attribute_name('articles') %></h3>
-          <%= render 'admin/research/journal/articles/list', articles: @volume.articles %>
+          <table class="table table-sortable">
+            <thead>
+              <tr>
+                <th><%= Research::Journal::Article.model_name.human %></th>
+                <th><%= Research::Journal::Article.human_attribute_name('published_at') %></th>
+                <th></th>
+              </tr>
+            </thead>
+            <tbody data-reorder-url="<%= reorder_admin_research_journal_articles_path(journal_id: @journal.id) %>">
+              <% @articles.each do |article| %>
+                <tr class="handle" data-id="<%= article.id %>">
+                  <td>
+                    <%= link_to article,
+                                admin_research_journal_article_path(journal_id: article.journal, id: article),
+                                class: "#{'opacity-50' unless article.published?}" %>
+                  </td>
+                  <td><%= article.published_at %></td>
+                  <td class="text-end">
+                    <div class="btn-group" role="group">
+                      <%= link_to t('edit'),
+                                  edit_admin_research_journal_article_path(journal_id: article.journal, id: article),
+                                  class: button_classes %>
+                      <%= link_to t('delete'),
+                                  admin_research_journal_article_path(journal_id: article.journal, id: article),
+                                  method: :delete,
+                                  data: { confirm: t('please_confirm') },
+                                  class: button_classes_danger %>
+                    </div>
+                  </td>
+                </tr>
+              <% end %>
+            </tbody>
+          </table>
         <% end %>
       </div>
     </div>
diff --git a/app/views/admin/research/journals/show.html.erb b/app/views/admin/research/journals/show.html.erb
index 19fe41780..ffee7a259 100644
--- a/app/views/admin/research/journals/show.html.erb
+++ b/app/views/admin/research/journals/show.html.erb
@@ -50,7 +50,7 @@
       <%= link_to Research::Journal::Article.model_name.human(count: 2),
                   admin_research_journal_articles_path(journal_id: @journal) %></h2>
   </div>
-  <%= render 'admin/research/journal/articles/list', articles: @journal.articles.ordered.limit(10) %>
+  <%= render 'admin/research/journal/articles/list', articles: @articles %>
 </div>
 
 <% content_for :action_bar_right do %>
diff --git a/config/locales/research/en.yml b/config/locales/research/en.yml
index 795cc6f97..e733e35ad 100644
--- a/config/locales/research/en.yml
+++ b/config/locales/research/en.yml
@@ -21,6 +21,7 @@ en:
         abstract: Abstract
         keywords: Keywords
         pdf: Article PDF
+        published: Published?
         published_at: Published at
         references: References
         researchers: Authors
diff --git a/config/locales/research/fr.yml b/config/locales/research/fr.yml
index df48aabfb..f8ca8b6a2 100644
--- a/config/locales/research/fr.yml
+++ b/config/locales/research/fr.yml
@@ -21,6 +21,7 @@ fr:
         abstract: Extrait
         keywords: Mots clés
         pdf: PDF de l'article
+        published: Publié ?
         published_at: Publié le
         references: Références
         researchers: Auteu·rs·rices
diff --git a/config/routes/admin/research.rb b/config/routes/admin/research.rb
index 678f7e597..255b3c3e4 100644
--- a/config/routes/admin/research.rb
+++ b/config/routes/admin/research.rb
@@ -2,6 +2,10 @@ namespace :research do
   resources :researchers, only: [:index, :show]
   resources :journals do
     resources :volumes, controller: 'journal/volumes'
-    resources :articles, controller: 'journal/articles'
+    resources :articles, controller: 'journal/articles' do
+      collection do
+        post :reorder
+      end
+    end
   end
 end
diff --git a/db/migrate/20220111092324_add_published_and_position_to_research_journal_articles.rb b/db/migrate/20220111092324_add_published_and_position_to_research_journal_articles.rb
new file mode 100644
index 000000000..b6adc118d
--- /dev/null
+++ b/db/migrate/20220111092324_add_published_and_position_to_research_journal_articles.rb
@@ -0,0 +1,6 @@
+class AddPublishedAndPositionToResearchJournalArticles < ActiveRecord::Migration[6.1]
+  def change
+    add_column :research_journal_articles, :published, :boolean, default: false
+    add_column :research_journal_articles, :position, :integer
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 9a2421980..ce33a4428 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_10_162001) do
+ActiveRecord::Schema.define(version: 2022_01_11_092324) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -450,6 +450,8 @@ ActiveRecord::Schema.define(version: 2022_01_10_162001) do
     t.text "references"
     t.text "keywords"
     t.string "slug"
+    t.boolean "published", default: false
+    t.integer "position"
     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"
diff --git a/lib/tasks/app.rake b/lib/tasks/app.rake
index 5e728c671..4ba4f91fd 100644
--- a/lib/tasks/app.rake
+++ b/lib/tasks/app.rake
@@ -29,6 +29,15 @@ namespace :app do
     Communication::Website::Post.find_each do |post|
       post.categories = post.categories.select { |category| category.children.none? { |child| post.categories.include?(child) } }
     end
+
+    Research::Journal::Article.where(position: nil).order(:published_at, :created_at).group_by(&:research_journal_volume_id).each do |_, articles|
+      articles.each_with_index do |article, index|
+        article.update_columns({
+          published: article.published_at.present?,
+          position: index + 1
+        })
+      end
+    end
   end
 
   namespace :db do
-- 
GitLab