From 13c8e804c9754fdfeaf0a74e7835d0c3f78e4498 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Mon, 14 Feb 2022 17:30:01 +0100
Subject: [PATCH] wip pages index

---
 .../website/index_pages_controller.rb         | 49 +++++++++++++++++++
 app/models/communication/website.rb           |  1 +
 app/models/communication/website/home.rb      |  1 +
 .../communication/website/index_page.rb       | 48 ++++++++++++++++++
 app/models/communication/website/page.rb      |  1 +
 .../communication/website/with_index_pages.rb | 11 +++++
 app/models/education/program.rb               | 13 +++++
 app/models/research/journal/article.rb        |  2 +-
 app/models/research/laboratory/axis.rb        |  1 +
 app/models/university/person.rb               |  1 +
 app/models/university/person/administrator.rb |  1 +
 app/models/university/person/author.rb        |  1 +
 app/models/university/person/researcher.rb    |  1 +
 app/models/university/person/teacher.rb       |  1 +
 .../website/index_pages/edit.html.erb         | 47 ++++++++++++++++++
 .../website/index_pages/index.html.erb        | 11 +++++
 .../communication/websites/_sidebar.html.erb  |  5 ++
 config/locales/communication/fr.yml           | 20 ++++++++
 config/routes/admin/communication.rb          |  4 ++
 ...reate_communication_website_index_pages.rb | 21 ++++++++
 db/schema.rb                                  | 38 +++++++++++++-
 test/fixtures/research/laboratory/axes.yml    |  1 +
 test/models/research/laboratory/axis_test.rb  |  1 +
 23 files changed, 277 insertions(+), 3 deletions(-)
 create mode 100644 app/controllers/admin/communication/website/index_pages_controller.rb
 create mode 100644 app/models/communication/website/index_page.rb
 create mode 100644 app/models/communication/website/with_index_pages.rb
 create mode 100644 app/views/admin/communication/website/index_pages/edit.html.erb
 create mode 100644 app/views/admin/communication/website/index_pages/index.html.erb
 create mode 100644 db/migrate/20220214144924_create_communication_website_index_pages.rb

diff --git a/app/controllers/admin/communication/website/index_pages_controller.rb b/app/controllers/admin/communication/website/index_pages_controller.rb
new file mode 100644
index 000000000..8dc1e395c
--- /dev/null
+++ b/app/controllers/admin/communication/website/index_pages_controller.rb
@@ -0,0 +1,49 @@
+class Admin::Communication::Website::IndexPagesController < Admin::Communication::Website::ApplicationController
+  before_action :get_index_page, :ensure_abilities, only: [:edit, :update]
+
+  def index
+    authorize! :read, Communication::Website::IndexPage
+    breadcrumb
+    @kinds = Communication::Website::IndexPage.kinds
+  end
+
+  def edit
+    breadcrumb
+    add_breadcrumb @index_page
+  end
+
+  def update
+    # if @index_page.update_and_sync(index_page_params)
+    if @index_page.update(index_page_params)
+      redirect_to admin_communication_website_indexes_path(@website), notice: t('admin.successfully_updated_html', model: Communication::Website::IndexPage.model_name.human)
+    else
+      breadcrumb
+      add_breadcrumb @index_page
+      render :edit, status: :unprocessable_entity
+    end
+  end
+
+  protected
+
+  def get_index_page
+    @index_page = @website.index_pages.where(kind: params[:kind]).first_or_initialize
+  end
+
+  def ensure_abilities
+    authorize! :update, @index_page
+  end
+
+  def breadcrumb
+    super
+    add_breadcrumb Communication::Website::IndexPage.model_name.human(count: 2), admin_communication_website_indexes_path(@website)
+  end
+
+
+  def index_page_params
+    params.require(:communication_website_index_page)
+          .permit(
+            :title, :description, :text, :featured_image, :featured_image_delete,
+            :featured_image_infos, :featured_image_alt
+          )
+  end
+end
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 02de212c3..f3abb7a6e 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -37,6 +37,7 @@ class Communication::Website < ApplicationRecord
   include WithGitRepository
   include WithHome
   include WithImport
+  include WithIndexPages
   include WithMenuItems
   include WithStructure
 
diff --git a/app/models/communication/website/home.rb b/app/models/communication/website/home.rb
index e35d331f3..00353bdc2 100644
--- a/app/models/communication/website/home.rb
+++ b/app/models/communication/website/home.rb
@@ -6,6 +6,7 @@
 #  description              :text
 #  featured_image_alt       :string
 #  github_path              :text
+#  text_new                 :text
 #  created_at               :datetime         not null
 #  updated_at               :datetime         not null
 #  communication_website_id :uuid             not null, indexed
diff --git a/app/models/communication/website/index_page.rb b/app/models/communication/website/index_page.rb
new file mode 100644
index 000000000..e923e672b
--- /dev/null
+++ b/app/models/communication/website/index_page.rb
@@ -0,0 +1,48 @@
+# == Schema Information
+#
+# Table name: communication_website_index_pages
+#
+#  id                       :uuid             not null, primary key
+#  description              :text
+#  featured_image_alt       :string
+#  kind                     :integer
+#  path                     :string
+#  text                     :text
+#  title                    :string
+#  created_at               :datetime         not null
+#  updated_at               :datetime         not null
+#  communication_website_id :uuid             not null, indexed
+#  university_id            :uuid             not null, indexed
+#
+# Indexes
+#
+#  idx_comm_website_index_page_on_communication_website_id   (communication_website_id)
+#  index_communication_website_index_pages_on_university_id  (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_5cd2482227  (communication_website_id => communication_websites.id)
+#  fk_rails_7eb45227ae  (university_id => universities.id)
+#
+class Communication::Website::IndexPage < ApplicationRecord
+
+  enum kind: { home: 0,
+               communication_posts: 10,
+               education_programs: 20,
+               research_articles: 30, research_volumes: 32,
+               persons: 100, administrators: 110, authors: 120, researchers: 130, teachers: 140
+              }
+
+  belongs_to :university
+  belongs_to :website, foreign_key: :communication_website_id
+
+  has_one_attached_deletable :featured_image
+
+  validates :title, :path, presence: true
+
+  def to_s
+    Communication::Website::IndexPage.human_attribute_name("kind_#{kind}")
+  end
+
+
+end
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 4a81253e0..c72e01cc6 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -12,6 +12,7 @@
 #  position                 :integer          default(0), not null
 #  published                :boolean          default(FALSE)
 #  slug                     :string
+#  text_new                 :text
 #  title                    :string
 #  created_at               :datetime         not null
 #  updated_at               :datetime         not null
diff --git a/app/models/communication/website/with_index_pages.rb b/app/models/communication/website/with_index_pages.rb
new file mode 100644
index 000000000..37eb64916
--- /dev/null
+++ b/app/models/communication/website/with_index_pages.rb
@@ -0,0 +1,11 @@
+module Communication::Website::WithIndexPages
+  extend ActiveSupport::Concern
+
+  included do
+    has_many :index_pages,
+             class_name: 'Communication::Website::IndexPage',
+             foreign_key: :communication_website_id,
+             dependent: :destroy
+  end
+
+end
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index 4396afb13..9abd988e9 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -3,16 +3,29 @@
 # Table name: education_programs
 #
 #  id                 :uuid             not null, primary key
+#  accessibility_new  :text
 #  capacity           :integer
+#  contacts_new       :text
+#  content_new        :text
 #  continuing         :boolean
 #  description        :text
+#  duration_new       :text
 #  ects               :integer
+#  evaluation_new     :text
 #  featured_image_alt :string
 #  level              :integer
 #  name               :string
+#  objectives_new     :text
+#  opportunities_new  :text
+#  other_new          :text
 #  path               :string
+#  pedagogy_new       :text
 #  position           :integer          default(0)
+#  prerequisites_new  :text
+#  pricing_new        :text
 #  published          :boolean          default(FALSE)
+#  registration_new   :text
+#  results_new        :text
 #  slug               :string
 #  created_at         :datetime         not null
 #  updated_at         :datetime         not null
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index bfe4270b1..3ff64d109 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -5,12 +5,12 @@
 #  id                         :uuid             not null, primary key
 #  abstract                   :text
 #  keywords                   :text
-#  old_text                   :text
 #  position                   :integer
 #  published                  :boolean          default(FALSE)
 #  published_at               :datetime
 #  references                 :text
 #  slug                       :string
+#  text_new                   :text
 #  title                      :string
 #  created_at                 :datetime         not null
 #  updated_at                 :datetime         not null
diff --git a/app/models/research/laboratory/axis.rb b/app/models/research/laboratory/axis.rb
index 372f8165f..5556990de 100644
--- a/app/models/research/laboratory/axis.rb
+++ b/app/models/research/laboratory/axis.rb
@@ -7,6 +7,7 @@
 #  name                   :string
 #  position               :integer
 #  short_name             :string
+#  text_new               :text
 #  created_at             :datetime         not null
 #  updated_at             :datetime         not null
 #  research_laboratory_id :uuid             not null, indexed
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 311541fc3..8f25ccd6d 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -3,6 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
+#  biography_new     :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/models/university/person/administrator.rb b/app/models/university/person/administrator.rb
index 422bbd695..45c0cf4c7 100644
--- a/app/models/university/person/administrator.rb
+++ b/app/models/university/person/administrator.rb
@@ -3,6 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
+#  biography_new     :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/models/university/person/author.rb b/app/models/university/person/author.rb
index eee35eb42..1bb44c1eb 100644
--- a/app/models/university/person/author.rb
+++ b/app/models/university/person/author.rb
@@ -3,6 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
+#  biography_new     :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/models/university/person/researcher.rb b/app/models/university/person/researcher.rb
index 165f8868d..207a0919a 100644
--- a/app/models/university/person/researcher.rb
+++ b/app/models/university/person/researcher.rb
@@ -3,6 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
+#  biography_new     :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/models/university/person/teacher.rb b/app/models/university/person/teacher.rb
index 59c75c7b3..18518ba92 100644
--- a/app/models/university/person/teacher.rb
+++ b/app/models/university/person/teacher.rb
@@ -3,6 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
+#  biography_new     :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/views/admin/communication/website/index_pages/edit.html.erb b/app/views/admin/communication/website/index_pages/edit.html.erb
new file mode 100644
index 000000000..44ea023b4
--- /dev/null
+++ b/app/views/admin/communication/website/index_pages/edit.html.erb
@@ -0,0 +1,47 @@
+<% content_for :title, Communication::Website::IndexPage.human_attribute_name("kind_#{@index_page.kind}") %>
+
+<%= render 'admin/communication/websites/sidebar' do %>
+
+  <%= simple_form_for @index_page, url: admin_communication_website_index_path(website_id: @website.id, kind: @index_page.kind) do |f| %>
+    <div class="row">
+      <div class="col-md-8">
+        <div class="card flex-fill w-100">
+          <div class="card-header">
+            <h5 class="card-title mb-0"><%= t('content') %></h5>
+          </div>
+          <div class="card-body">
+            <%= f.input :title %>
+            <%= f.input :path,
+                        input_html: {
+                          class: 'js-slug-input',
+                          data: { source: '#communication_website_index_page_title' }
+                        } %>
+            <%= f.input :description %>
+            <%= f.input :text, as: :rich_text_area %>
+          </div>
+        </div>
+      </div>
+      <div class="col-md-4">
+        <div class="card flex-fill w-100">
+          <div class="card-header">
+            <h5 class="card-title mb-0"><%= t('activerecord.attributes.communication/website/page.featured_image') %></h5>
+          </div>
+          <div class="card-body">
+            <%= f.input :featured_image,
+                        as: :single_deletable_file,
+                        direct_upload: true,
+                        label: false,
+                        input_html: { accept: '.jpg,.jpeg,.png' },
+                        preview: true
+             %>
+             <%= f.input :featured_image_alt %>
+          </div>
+        </div>
+      </div>
+    </div>
+    <% content_for :action_bar_right do %>
+      <%= submit f %>
+    <% end %>
+  <% end %>
+
+<% end %>
diff --git a/app/views/admin/communication/website/index_pages/index.html.erb b/app/views/admin/communication/website/index_pages/index.html.erb
new file mode 100644
index 000000000..c1ede975a
--- /dev/null
+++ b/app/views/admin/communication/website/index_pages/index.html.erb
@@ -0,0 +1,11 @@
+<% content_for :title, t('activerecord.models.communication/website/index_page.other') %>
+
+<%= render 'admin/communication/websites/sidebar' do %>
+
+  <ul>
+  <% @kinds.each do |key, value| %>
+    <li><%= link_to Communication::Website::IndexPage.human_attribute_name("kind_#{key}"), admin_communication_website_index_path(website_id: @website.id, kind: key) %></li>
+  <% end %>
+  </ul>
+
+<% end %>
diff --git a/app/views/admin/communication/websites/_sidebar.html.erb b/app/views/admin/communication/websites/_sidebar.html.erb
index e2c09d29d..07a6d26da 100644
--- a/app/views/admin/communication/websites/_sidebar.html.erb
+++ b/app/views/admin/communication/websites/_sidebar.html.erb
@@ -39,6 +39,11 @@
             path: admin_communication_website_menus_path(website_id: @website),
             ability: can?(:read, Communication::Website::Category)
           },
+          {
+            title: Communication::Website::IndexPage.model_name.human(count: 2),
+            path: admin_communication_website_indexes_path(website_id: @website),
+            ability: can?(:read, Communication::Website::IndexPage)
+          },
           {
             title: Communication::Website::Structure.model_name.human,
             path: admin_communication_website_structure_path(website_id: @website),
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index c59adc6e0..e3601da89 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -16,6 +16,9 @@ fr:
       communication/website/imported/website:
         one: Site importé
         other: Sites importés
+      communication/website/index_page:
+        one: Page d'index
+        other: Pages d'index
       communication/website/menu:
         one: Menu
         other: Menus
@@ -54,6 +57,23 @@ fr:
         text: Texte
         featured_image: Image à la une
         featured_image_alt: Texte alternatif
+      communication/website/index_page:
+        description: Description (SEO)
+        featured_image: Image à la une
+        featured_image_alt: Texte alternatif
+        kind_home: Accueil
+        kind_communication_posts: Actualités
+        kind_education_programs: Formations
+        kind_research_articles: Articles
+        kind_research_volumes: Volumes
+        kind_persons: Équipe
+        kind_administrators: Équipe administrative
+        kind_authors: Équipe éditoriale
+        kind_researchers: Équipe de recherche
+        kind_teachers: Équipe pédagogique
+        slug: Slug
+        text: Texte
+        title: Titre
       communication/website/imported/category:
         original: Original
       communication/website/imported/medium:
diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb
index 705d9fb28..5325fc283 100644
--- a/config/routes/admin/communication.rb
+++ b/config/routes/admin/communication.rb
@@ -38,5 +38,9 @@ namespace :communication do
     end
     get 'structure' => 'website/structure#edit'
     patch 'structure' => 'website/structure#update'
+
+    get 'indexes' => 'website/index_pages#index'
+    get 'indexes/:kind' => 'website/index_pages#edit', as: :index
+    patch 'indexes/:kind' => 'website/index_pages#update'
   end
 end
diff --git a/db/migrate/20220214144924_create_communication_website_index_pages.rb b/db/migrate/20220214144924_create_communication_website_index_pages.rb
new file mode 100644
index 000000000..d009994cf
--- /dev/null
+++ b/db/migrate/20220214144924_create_communication_website_index_pages.rb
@@ -0,0 +1,21 @@
+class CreateCommunicationWebsiteIndexPages < ActiveRecord::Migration[6.1]
+  def change
+    create_table :communication_website_index_pages, id: :uuid do |t|
+      t.references :university, null: false, foreign_key: true, type: :uuid
+      t.references :communication_website,
+          null: false,
+          foreign_key: { to_table: :communication_websites },
+          type: :uuid,
+          index: { name: 'idx_comm_website_index_page_on_communication_website_id' }
+
+      t.string :title
+      t.string :path
+      t.text :description
+      t.text :text
+      t.string :featured_image_alt
+      t.integer :kind
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index f18cf9d8e..3a9315c5c 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_02_10_105040) do
+ActiveRecord::Schema.define(version: 2022_02_14_144924) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -126,6 +126,7 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
     t.text "github_path"
     t.string "featured_image_alt"
     t.text "description"
+    t.text "text_new"
     t.index ["communication_website_id"], name: "idx_comm_website_homes_on_communication_website_id"
     t.index ["university_id"], name: "index_communication_website_homes_on_university_id"
   end
@@ -238,6 +239,21 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
     t.index ["website_id"], name: "index_communication_website_imported_websites_on_website_id"
   end
 
+  create_table "communication_website_index_pages", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+    t.uuid "university_id", null: false
+    t.uuid "communication_website_id", null: false
+    t.string "title"
+    t.string "path"
+    t.text "description"
+    t.text "text"
+    t.string "featured_image_alt"
+    t.integer "kind"
+    t.datetime "created_at", precision: 6, null: false
+    t.datetime "updated_at", precision: 6, null: false
+    t.index ["communication_website_id"], name: "idx_comm_website_index_page_on_communication_website_id"
+    t.index ["university_id"], name: "index_communication_website_index_pages_on_university_id"
+  end
+
   create_table "communication_website_menu_items", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "website_id", null: false
@@ -288,6 +304,7 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
     t.text "github_path"
     t.uuid "related_category_id"
     t.string "featured_image_alt"
+    t.text "text_new"
     t.index ["about_type", "about_id"], name: "index_communication_website_pages_on_about"
     t.index ["communication_website_id"], name: "index_communication_website_pages_on_communication_website_id"
     t.index ["parent_id"], name: "index_communication_website_pages_on_parent_id"
@@ -407,6 +424,19 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
     t.text "description"
     t.boolean "published", default: false
     t.string "featured_image_alt"
+    t.text "accessibility_new"
+    t.text "contacts_new"
+    t.text "duration_new"
+    t.text "evaluation_new"
+    t.text "objectives_new"
+    t.text "opportunities_new"
+    t.text "other_new"
+    t.text "pedagogy_new"
+    t.text "prerequisites_new"
+    t.text "pricing_new"
+    t.text "registration_new"
+    t.text "content_new"
+    t.text "results_new"
     t.index ["parent_id"], name: "index_education_programs_on_parent_id"
     t.index ["university_id"], name: "index_education_programs_on_university_id"
   end
@@ -448,7 +478,6 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
 
   create_table "research_journal_articles", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.string "title"
-    t.text "old_text"
     t.datetime "published_at"
     t.uuid "university_id", null: false
     t.uuid "research_journal_id", null: false
@@ -462,6 +491,7 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
     t.string "slug"
     t.boolean "published", default: false
     t.integer "position"
+    t.text "text_new"
     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"
@@ -525,6 +555,7 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
     t.datetime "created_at", precision: 6, null: false
     t.datetime "updated_at", precision: 6, null: false
     t.string "short_name"
+    t.text "text_new"
     t.index ["research_laboratory_id"], name: "index_research_laboratory_axes_on_research_laboratory_id"
     t.index ["university_id"], name: "index_research_laboratory_axes_on_university_id"
   end
@@ -578,6 +609,7 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
     t.text "description"
     t.boolean "habilitation", default: false
     t.boolean "tenure", default: false
+    t.text "biography_new"
     t.index ["university_id"], name: "index_university_people_on_university_id"
     t.index ["user_id"], name: "index_university_people_on_user_id"
   end
@@ -680,6 +712,8 @@ ActiveRecord::Schema.define(version: 2022_02_10_105040) do
   add_foreign_key "communication_website_imported_posts", "universities"
   add_foreign_key "communication_website_imported_websites", "communication_websites", column: "website_id"
   add_foreign_key "communication_website_imported_websites", "universities"
+  add_foreign_key "communication_website_index_pages", "communication_websites"
+  add_foreign_key "communication_website_index_pages", "universities"
   add_foreign_key "communication_website_menu_items", "communication_website_menu_items", column: "parent_id"
   add_foreign_key "communication_website_menu_items", "communication_website_menus", column: "menu_id"
   add_foreign_key "communication_website_menu_items", "communication_websites", column: "website_id"
diff --git a/test/fixtures/research/laboratory/axes.yml b/test/fixtures/research/laboratory/axes.yml
index 51a668b66..1ff6715cc 100644
--- a/test/fixtures/research/laboratory/axes.yml
+++ b/test/fixtures/research/laboratory/axes.yml
@@ -7,6 +7,7 @@
 #  name                   :string
 #  position               :integer
 #  short_name             :string
+#  text_new               :text
 #  created_at             :datetime         not null
 #  updated_at             :datetime         not null
 #  research_laboratory_id :uuid             not null, indexed
diff --git a/test/models/research/laboratory/axis_test.rb b/test/models/research/laboratory/axis_test.rb
index 909fb0dd8..0ed57cec5 100644
--- a/test/models/research/laboratory/axis_test.rb
+++ b/test/models/research/laboratory/axis_test.rb
@@ -7,6 +7,7 @@
 #  name                   :string
 #  position               :integer
 #  short_name             :string
+#  text_new               :text
 #  created_at             :datetime         not null
 #  updated_at             :datetime         not null
 #  research_laboratory_id :uuid             not null, indexed
-- 
GitLab