From efb3d2fd0eac2ee241886903f44182f5bb3acaf9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 9 Dec 2021 16:08:00 +0100
Subject: [PATCH] slug on education program, and research articles, volumes &
 researchers

---
 app/controllers/admin/education/programs_controller.rb    | 2 +-
 .../admin/research/journal/articles_controller.rb         | 2 +-
 .../admin/research/journal/volumes_controller.rb          | 2 +-
 app/controllers/admin/research/researchers_controller.rb  | 2 +-
 app/models/education/program.rb                           | 1 +
 app/models/research/journal/article.rb                    | 1 +
 app/models/research/journal/volume.rb                     | 1 +
 app/models/research/researcher.rb                         | 1 +
 app/views/admin/education/programs/_form.html.erb         | 6 ++++++
 app/views/admin/research/journal/articles/_form.html.erb  | 6 ++++++
 app/views/admin/research/journal/volumes/_form.html.erb   | 6 ++++++
 app/views/admin/research/researchers/_form.html.erb       | 6 ++++++
 ...209144737_add_slug_to_education_and_research_models.rb | 8 ++++++++
 db/schema.rb                                              | 6 +++++-
 lib/tasks/app.rake                                        | 7 +++++++
 15 files changed, 52 insertions(+), 5 deletions(-)
 create mode 100644 db/migrate/20211209144737_add_slug_to_education_and_research_models.rb

diff --git a/app/controllers/admin/education/programs_controller.rb b/app/controllers/admin/education/programs_controller.rb
index 185fae448..32ef3f170 100644
--- a/app/controllers/admin/education/programs_controller.rb
+++ b/app/controllers/admin/education/programs_controller.rb
@@ -79,7 +79,7 @@ class Admin::Education::ProgramsController < Admin::Education::ApplicationContro
 
   def program_params
     params.require(:education_program)
-          .permit(:name, :level, :capacity, :ects, :continuing,
+          .permit(:name, :slug, :level, :capacity, :ects, :continuing,
             :prerequisites, :objectives, :duration, :registration, :pedagogy,
             :evaluation, :accessibility, :pricing, :contacts, :opportunities, :other, :parent_id, school_ids: [], teacher_ids: [])
   end
diff --git a/app/controllers/admin/research/journal/articles_controller.rb b/app/controllers/admin/research/journal/articles_controller.rb
index 198690235..15922af61 100644
--- a/app/controllers/admin/research/journal/articles_controller.rb
+++ b/app/controllers/admin/research/journal/articles_controller.rb
@@ -58,7 +58,7 @@ class Admin::Research::Journal::ArticlesController < Admin::Research::Journal::A
 
   def article_params
     params.require(:research_journal_article)
-          .permit(:title, :text, :published_at, :abstract, :pdf, :references, :keywords, :research_journal_volume_id, researcher_ids: [])
+          .permit(:title, :slug, :text, :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 3090f9168..ed2b0699c 100644
--- a/app/controllers/admin/research/journal/volumes_controller.rb
+++ b/app/controllers/admin/research/journal/volumes_controller.rb
@@ -54,7 +54,7 @@ class Admin::Research::Journal::VolumesController < Admin::Research::Journal::Ap
 
   def volume_params
     params.require(:research_journal_volume)
-          .permit(:title, :number, :cover, :cover_infos, :cover_delete, :keywords, :published_at, :description)
+          .permit(:title, :slug, :number, :cover, :cover_infos, :cover_delete, :keywords, :published_at, :description)
           .merge(university_id: current_university.id)
   end
 end
diff --git a/app/controllers/admin/research/researchers_controller.rb b/app/controllers/admin/research/researchers_controller.rb
index 2bff223c0..47253a651 100644
--- a/app/controllers/admin/research/researchers_controller.rb
+++ b/app/controllers/admin/research/researchers_controller.rb
@@ -55,7 +55,7 @@ class Admin::Research::ResearchersController < Admin::Research::ApplicationContr
 
   def researcher_params
     params.require(:research_researcher)
-          .permit(:first_name, :last_name, :biography, :user_id)
+          .permit(:first_name, :last_name, :slug, :biography, :user_id)
           .merge(university_id: current_university.id)
   end
 end
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index bd0029f95..9d46a22ae 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -9,6 +9,7 @@
 #  level         :integer
 #  name          :string
 #  position      :integer          default(0)
+#  slug          :string
 #  created_at    :datetime         not null
 #  updated_at    :datetime         not null
 #  parent_id     :uuid
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index bde49caaa..5d28a34f0 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -8,6 +8,7 @@
 #  old_text                   :text
 #  published_at               :date
 #  references                 :text
+#  slug                       :string
 #  title                      :string
 #  created_at                 :datetime         not null
 #  updated_at                 :datetime         not null
diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb
index 676a48bfa..f2dc30611 100644
--- a/app/models/research/journal/volume.rb
+++ b/app/models/research/journal/volume.rb
@@ -7,6 +7,7 @@
 #  keywords            :text
 #  number              :integer
 #  published_at        :date
+#  slug                :string
 #  title               :string
 #  created_at          :datetime         not null
 #  updated_at          :datetime         not null
diff --git a/app/models/research/researcher.rb b/app/models/research/researcher.rb
index 941699607..582522b86 100644
--- a/app/models/research/researcher.rb
+++ b/app/models/research/researcher.rb
@@ -5,6 +5,7 @@
 #  id            :uuid             not null, primary key
 #  first_name    :string
 #  last_name     :string
+#  slug          :string
 #  created_at    :datetime         not null
 #  updated_at    :datetime         not null
 #  university_id :uuid
diff --git a/app/views/admin/education/programs/_form.html.erb b/app/views/admin/education/programs/_form.html.erb
index cdc869930..595a8b4ab 100644
--- a/app/views/admin/education/programs/_form.html.erb
+++ b/app/views/admin/education/programs/_form.html.erb
@@ -7,6 +7,12 @@
         </div>
         <div class="card-body">
           <%= f.input :name %>
+          <%= f.input :slug,
+                      as: :string,
+                      input_html: program.persisted? ? {} : {
+                        class: 'js-slug-input',
+                        data: { source: '#education_program_name' }
+                      } %>
           <%= f.input :level, include_blank: false, label_method: -> (p) { I18n.t(p[1], scope: 'enums.education/program.level') } %>
           <%= f.input :capacity %>
           <%= f.input :ects %>
diff --git a/app/views/admin/research/journal/articles/_form.html.erb b/app/views/admin/research/journal/articles/_form.html.erb
index 6e15aaefd..a37138e11 100644
--- a/app/views/admin/research/journal/articles/_form.html.erb
+++ b/app/views/admin/research/journal/articles/_form.html.erb
@@ -7,6 +7,12 @@
         </div>
         <div class="card-body">
           <%= f.input :title, as: :text, input_html: { rows: 3 } %>
+          <%= f.input :slug,
+                      as: :string,
+                      input_html: article.persisted? ? {} : {
+                        class: 'js-slug-input',
+                        data: { source: '#research_journal_article_title' }
+                      } %>
           <%= f.input :abstract, as: :text, input_html: { rows: 8 } %>
           <%= f.input :pdf %>
           <%= f.input :text, as: :rich_text_area %>
diff --git a/app/views/admin/research/journal/volumes/_form.html.erb b/app/views/admin/research/journal/volumes/_form.html.erb
index 0523acd78..2ebadb099 100644
--- a/app/views/admin/research/journal/volumes/_form.html.erb
+++ b/app/views/admin/research/journal/volumes/_form.html.erb
@@ -7,6 +7,12 @@
         </div>
         <div class="card-body">
           <%= f.input :title %>
+          <%= f.input :slug,
+                      as: :string,
+                      input_html: volume.persisted? ? {} : {
+                        class: 'js-slug-input',
+                        data: { source: '#research_journal_volume_title' }
+                      } %>
           <%= f.input :cover,
                       as: :single_deletable_file,
                       input_html: { accept: '.jpg,.jpeg,.png' },
diff --git a/app/views/admin/research/researchers/_form.html.erb b/app/views/admin/research/researchers/_form.html.erb
index 5cc8e6c65..aecb1a416 100644
--- a/app/views/admin/research/researchers/_form.html.erb
+++ b/app/views/admin/research/researchers/_form.html.erb
@@ -5,6 +5,12 @@
       <div class="card-body">
         <%= f.input :first_name %>
         <%= f.input :last_name %>
+        <%= f.input :slug,
+                    as: :string,
+                    input_html: researcher.persisted? ? {} : {
+                      class: 'js-slug-input',
+                      data: { source: '#research_researcher_first_name, #research_researcher_last_name' }
+                    } %>
         <%= f.association :user, collection: current_university.users.ordered %>
       </div>
     </div>
diff --git a/db/migrate/20211209144737_add_slug_to_education_and_research_models.rb b/db/migrate/20211209144737_add_slug_to_education_and_research_models.rb
new file mode 100644
index 000000000..8072ebc07
--- /dev/null
+++ b/db/migrate/20211209144737_add_slug_to_education_and_research_models.rb
@@ -0,0 +1,8 @@
+class AddSlugToEducationAndResearchModels < ActiveRecord::Migration[6.1]
+  def change
+    add_column :education_programs, :slug, :string
+    add_column :research_journal_articles, :slug, :string
+    add_column :research_journal_volumes, :slug, :string
+    add_column :research_researchers, :slug, :string
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 3d52f5d0b..27a6a46e3 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_12_09_110929) do
+ActiveRecord::Schema.define(version: 2021_12_09_144737) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -364,6 +364,7 @@ ActiveRecord::Schema.define(version: 2021_12_09_110929) do
     t.datetime "updated_at", precision: 6, null: false
     t.uuid "parent_id"
     t.integer "position", default: 0
+    t.string "slug"
     t.index ["parent_id"], name: "index_education_programs_on_parent_id"
     t.index ["university_id"], name: "index_education_programs_on_university_id"
   end
@@ -429,6 +430,7 @@ ActiveRecord::Schema.define(version: 2021_12_09_110929) do
     t.text "abstract"
     t.text "references"
     t.text "keywords"
+    t.string "slug"
     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"
@@ -452,6 +454,7 @@ ActiveRecord::Schema.define(version: 2021_12_09_110929) do
     t.datetime "updated_at", precision: 6, null: false
     t.text "description"
     t.text "keywords"
+    t.string "slug"
     t.index ["research_journal_id"], name: "index_research_journal_volumes_on_research_journal_id"
     t.index ["university_id"], name: "index_research_journal_volumes_on_university_id"
   end
@@ -475,6 +478,7 @@ ActiveRecord::Schema.define(version: 2021_12_09_110929) do
     t.datetime "created_at", precision: 6, null: false
     t.datetime "updated_at", precision: 6, null: false
     t.uuid "university_id"
+    t.string "slug"
     t.index ["university_id"], name: "idx_researcher_university"
     t.index ["user_id"], name: "index_research_researchers_on_user_id"
   end
diff --git a/lib/tasks/app.rake b/lib/tasks/app.rake
index 3766374c1..402a331c2 100644
--- a/lib/tasks/app.rake
+++ b/lib/tasks/app.rake
@@ -17,12 +17,18 @@ namespace :app do
       website.update_column(:url, "https://#{website.url}") unless website.url.blank? || website.url.starts_with?('https://')
     }
 
+    Education::Program.where(slug: [nil, '']).find_each { |program| program.update_column(:slug, program.name.parameterize) }
+    Research::Journal::Article.where(slug: [nil, '']).find_each { |article| article.update_column(:slug, article.title.parameterize) }
+    Research::Journal::Volume.where(slug: [nil, '']).find_each { |volume| volume.update_column(:slug, volume.title.parameterize) }
+    Research::Researcher.where(slug: [nil, '']).find_each { |researcher| researcher.update_column(:slug, "#{researcher.first_name} #{researcher.last_name}".parameterize) }
+
     [
       Communication::Website::Author, Communication::Website::Category,
       Communication::Website::Home, Communication::Website::Menu,
       Communication::Website::Page, Communication::Website::Post
     ].each do |model|
       model.includes(:website).find_each do |object|
+        next unless Github.with_website(object.website).valid?
         object.github_manifest.each do |manifest_item|
           Communication::Website::GithubFile.where(website: object.website, about: object, manifest_identifier: manifest_item[:identifier]).first_or_create do |github_file|
             github_file.github_path = object.github_path if manifest_item[:identifier] == 'primary'
@@ -37,6 +43,7 @@ namespace :app do
     ].each do |model|
       model.includes(:websites).find_each do |object|
         object.websites.each do |website|
+          next unless Github.with_website(website).valid?
           object.github_manifest.each do |manifest_item|
             Communication::Website::GithubFile.where(website: website, about: object, manifest_identifier: manifest_item[:identifier]).first_or_create do |github_file|
               github_file.github_path = object.github_path_generated if manifest_item[:identifier] == 'primary'
-- 
GitLab