From 6ef53b9756acd2fe638f579501cd95ccfd2482da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 3 Apr 2023 16:02:59 +0200
Subject: [PATCH] references, git files, for website

---
 app/models/communication/block.rb            |  3 --
 app/models/communication/website.rb          |  6 +++-
 app/models/communication/website/category.rb |  8 +++--
 app/models/communication/website/menu.rb     |  4 +++
 app/models/communication/website/page.rb     |  9 ++++--
 app/models/communication/website/post.rb     |  7 +++--
 app/models/concerns/with_connections.rb      |  5 +++
 app/models/concerns/with_git.rb              | 33 +++++++-------------
 app/models/concerns/with_git_files.rb        | 23 ++++++++++++++
 app/models/education/diploma.rb              |  4 +++
 app/models/education/program.rb              |  4 +++
 app/models/education/school.rb               |  5 ++-
 app/models/research/journal.rb               |  4 +++
 app/models/research/journal/paper/kind.rb    |  4 +++
 app/models/research/journal/volume.rb        |  4 +++
 app/models/university/organization.rb        |  4 +++
 app/models/university/person.rb              |  4 +++
 db/schema.rb                                 | 22 -------------
 test/fixtures/education/schools.yml          |  1 -
 19 files changed, 97 insertions(+), 57 deletions(-)
 create mode 100644 app/models/concerns/with_git_files.rb

diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb
index 4046234af..bc8294d0e 100644
--- a/app/models/communication/block.rb
+++ b/app/models/communication/block.rb
@@ -12,19 +12,16 @@
 #  created_at    :datetime         not null
 #  updated_at    :datetime         not null
 #  about_id      :uuid             indexed => [about_type]
-#  heading_id    :uuid             indexed
 #  university_id :uuid             not null, indexed
 #
 # Indexes
 #
-#  index_communication_blocks_on_heading_id     (heading_id)
 #  index_communication_blocks_on_university_id  (university_id)
 #  index_communication_website_blocks_on_about  (about_type,about_id)
 #
 # Foreign Keys
 #
 #  fk_rails_18291ef65f  (university_id => universities.id)
-#  fk_rails_90ac986fab  (heading_id => communication_block_headings.id)
 #
 class Communication::Block < ApplicationRecord
   include Accessible
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 17b93564d..2af72ac55 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -91,12 +91,16 @@ class Communication::Website < ApplicationRecord
 
   def dependencies
     pages +
-    posts + 
+    posts +
     categories +
     menus +
     [about]
   end
 
+  def references
+    []
+  end
+
   def best_language_for(iso_code)
     # We look for the language by the ISO code in the websites languages.
     # If not found, we fallback to the default language.
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index c14427eee..612276685 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -98,9 +98,11 @@ class Communication::Website::Category < ApplicationRecord
   def dependencies
     active_storage_blobs +
     blocks +
-    children +
-    posts +
-    [parent]
+    children
+  end
+
+  def references
+    posts + [parent]
   end
 
   def update_children_paths
diff --git a/app/models/communication/website/menu.rb b/app/models/communication/website/menu.rb
index 51b9aedab..87041f6d6 100644
--- a/app/models/communication/website/menu.rb
+++ b/app/models/communication/website/menu.rb
@@ -57,6 +57,10 @@ class Communication::Website::Menu < ApplicationRecord
     items
   end
 
+  def references
+    []
+  end
+
   def translate_additional_data!(translation)
     items.root.ordered.each { |item| translate_menu_item!(item, translation) }
   end
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 85ad0a6e0..2c3478afb 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -84,7 +84,7 @@ class Communication::Website::Page < ApplicationRecord
   has_many   :translations,
              class_name: 'Communication::Website::Page',
              foreign_key: :original_id
-             
+
   validates :title, presence: true
 
   scope :recent, -> { order(updated_at: :desc).limit(5) }
@@ -93,7 +93,7 @@ class Communication::Website::Page < ApplicationRecord
   def template_static
     "admin/communication/websites/pages/static"
   end
-  
+
   def menu_items
     Communication::Website::Menu::Item.where(website: website, kind: :page, about: self)
   end
@@ -101,10 +101,13 @@ class Communication::Website::Page < ApplicationRecord
   def dependencies
     active_storage_blobs +
     blocks +
-    menu_items +
     children
   end
 
+  def references
+    menu_items
+  end
+
   def to_s
     "#{title}"
   end
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index c833e666d..fe8fabdbd 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -119,7 +119,7 @@ class Communication::Website::Post < ApplicationRecord
   def template_static
     "admin/communication/websites/posts/static"
   end
-  
+
   def menu_items
     Communication::Website::Menu::Item.where(website: website, kind: :post, about: self)
   end
@@ -127,10 +127,13 @@ class Communication::Website::Post < ApplicationRecord
   def dependencies
     active_storage_blobs +
     blocks +
-    menu_items +
     categories
   end
 
+  def references
+    menu_items
+  end
+
   def url
     return unless published
     return if website.url.blank?
diff --git a/app/models/concerns/with_connections.rb b/app/models/concerns/with_connections.rb
index d4dd9030d..6a6dbd4f5 100644
--- a/app/models/concerns/with_connections.rb
+++ b/app/models/concerns/with_connections.rb
@@ -4,6 +4,7 @@ module WithConnections
 
   included do
     include WithDependencies
+    include WithGitFiles
     include WithReferences
 
     has_many :connections, as: :indirect_object, class_name: 'Communication::Website::Connection'
@@ -15,6 +16,10 @@ module WithConnections
     after_touch :sync_connections
   end
 
+  def for_website?(website)
+    Communication::Website::Connection.in_website(website).for_object(self).exists?
+  end
+
   def direct_sources
     connections.collect &:direct_source
   end
diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb
index 426e36925..01e36762e 100644
--- a/app/models/concerns/with_git.rb
+++ b/app/models/concerns/with_git.rb
@@ -1,28 +1,11 @@
 module WithGit
   extend ActiveSupport::Concern
-  
+
   included do
-    # WithGit a besoin de WithDependencies et WithReferences
+    # WithGit a besoin de ces 3 concerns
     include WithDependencies
+    include WithGitFiles
     include WithReferences
-
-    has_many  :git_files,
-              class_name: "Communication::Website::GitFile",
-              as: :about,
-              dependent: :destroy
-  end
-
-  def git_path(website)
-    raise NotImplementedError
-  end
-
-  def git_path_content_prefix(website)
-    # Handle language-less objects
-    # TODO I18n: Right now, we use the language of the object, fallbacking on the language of the website. In the end, we'll only use the language of the object
-    path = "content/"
-    path_language = respond_to?(:language_id) && language_id.present? ? language : website.default_language
-    path += "#{path_language.iso_code}/"
-    path
   end
 
   def save_and_sync
@@ -75,7 +58,15 @@ module WithGit
       website.git_repository.sync!
     end
   end
-  
+
+  def for_website?(website)
+    if is_a? Communication::Website
+      website.id == id
+    else
+      website.id == website_id
+    end
+  end
+
   protected
 
   def websites_for_self
diff --git a/app/models/concerns/with_git_files.rb b/app/models/concerns/with_git_files.rb
new file mode 100644
index 000000000..1e5ad1268
--- /dev/null
+++ b/app/models/concerns/with_git_files.rb
@@ -0,0 +1,23 @@
+module WithGitFiles
+  extend ActiveSupport::Concern
+
+  included do
+    has_many  :git_files,
+              class_name: "Communication::Website::GitFile",
+              as: :about,
+              dependent: :destroy
+  end
+
+  def git_path(website)
+    raise NotImplementedError
+  end
+
+  def git_path_content_prefix(website)
+    # Handle language-less objects
+    # TODO I18n: Right now, we use the language of the object, fallbacking on the language of the website. In the end, we'll only use the language of the object
+    path = "content/"
+    path_language = respond_to?(:language_id) && language_id.present? ? language : website.default_language
+    path += "#{path_language.iso_code}/"
+    path
+  end
+end
diff --git a/app/models/education/diploma.rb b/app/models/education/diploma.rb
index f73e09b99..81151c816 100644
--- a/app/models/education/diploma.rb
+++ b/app/models/education/diploma.rb
@@ -51,6 +51,10 @@ class Education::Diploma < ApplicationRecord
     "#{git_path_content_prefix(website)}diplomas/#{slug}/_index.html" if for_website?(website)
   end
 
+  def references
+    []
+  end
+
   def to_s
     "#{name}"
   end
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index 557571d15..3f50ce62b 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -149,6 +149,10 @@ class Education::Program < ApplicationRecord
     [diploma]
   end
 
+  def references
+    []
+  end
+
   def references
     siblings +
     descendants
diff --git a/app/models/education/school.rb b/app/models/education/school.rb
index 93d7561d1..114d925ef 100644
--- a/app/models/education/school.rb
+++ b/app/models/education/school.rb
@@ -10,7 +10,6 @@
 #  longitude     :float
 #  name          :string
 #  phone         :string
-#  url           :string
 #  zipcode       :string
 #  created_at    :datetime         not null
 #  updated_at    :datetime         not null
@@ -77,6 +76,10 @@ class Education::School < ApplicationRecord
     administrators.map(&:administrator)
   end
 
+  def references
+    []
+  end
+
   #####################
   # Aboutable methods #
   #####################
diff --git a/app/models/research/journal.rb b/app/models/research/journal.rb
index c997d59ba..dcc0352ab 100644
--- a/app/models/research/journal.rb
+++ b/app/models/research/journal.rb
@@ -62,6 +62,10 @@ class Research::Journal < ApplicationRecord
     researchers.map(&:researcher)
   end
 
+  def references
+    []
+  end
+
   #####################
   # Aboutable methods #
   #####################
diff --git a/app/models/research/journal/paper/kind.rb b/app/models/research/journal/paper/kind.rb
index a30acaf33..ff7783d35 100644
--- a/app/models/research/journal/paper/kind.rb
+++ b/app/models/research/journal/paper/kind.rb
@@ -31,6 +31,10 @@ class Research::Journal::Paper::Kind < ApplicationRecord
 
   scope :ordered, -> { order(:title) }
 
+  def references
+    []
+  end
+
   def to_s
     "#{title}"
   end
diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb
index bdba30633..532965c2a 100644
--- a/app/models/research/journal/volume.rb
+++ b/app/models/research/journal/volume.rb
@@ -62,6 +62,10 @@ class Research::Journal::Volume < ApplicationRecord
     active_storage_blobs
   end
 
+  def references
+    []
+  end
+
   def path
     "/#{published_at&.year}-#{slug}" if published_at
   end
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index 4f99e796a..46341be2a 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -110,6 +110,10 @@ class University::Organization < ApplicationRecord
     blocks
   end
 
+  def references
+    []
+  end
+
   def git_path(website)
     "#{git_path_content_prefix(website)}organizations/#{slug}.html" if for_website?(website)
   end
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index d70ae5e34..cce0b109b 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -204,6 +204,10 @@ class University::Person < ApplicationRecord
     # person.author.active_storage_blobs => []
   end
 
+  def references
+    []
+  end
+
   def administrator
     @administrator ||= University::Person::Administrator.find(id)
   end
diff --git a/db/schema.rb b/db/schema.rb
index cb37a3739..520d78406 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -78,22 +78,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_03_101306) do
     t.index ["criterion_id"], name: "index_administration_qualiopi_indicators_on_criterion_id"
   end
 
-  create_table "communication_block_headings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
-    t.uuid "university_id", null: false
-    t.string "about_type", null: false
-    t.uuid "about_id", null: false
-    t.string "title"
-    t.integer "level", default: 2
-    t.uuid "parent_id"
-    t.integer "position"
-    t.string "slug"
-    t.datetime "created_at", null: false
-    t.datetime "updated_at", null: false
-    t.index ["about_type", "about_id"], name: "index_communication_block_headings_on_about"
-    t.index ["parent_id"], name: "index_communication_block_headings_on_parent_id"
-    t.index ["university_id"], name: "index_communication_block_headings_on_university_id"
-  end
-
   create_table "communication_blocks", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.string "about_type"
@@ -105,9 +89,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_03_101306) do
     t.datetime "updated_at", null: false
     t.string "title"
     t.boolean "published", default: true
-    t.uuid "heading_id"
     t.index ["about_type", "about_id"], name: "index_communication_website_blocks_on_about"
-    t.index ["heading_id"], name: "index_communication_blocks_on_heading_id"
     t.index ["university_id"], name: "index_communication_blocks_on_university_id"
   end
 
@@ -676,7 +658,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_03_101306) do
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.string "phone"
-    t.string "url"
     t.index ["university_id"], name: "index_education_schools_on_university_id"
   end
 
@@ -1094,9 +1075,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_03_101306) do
   add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
   add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
   add_foreign_key "administration_qualiopi_indicators", "administration_qualiopi_criterions", column: "criterion_id"
-  add_foreign_key "communication_block_headings", "communication_block_headings", column: "parent_id"
-  add_foreign_key "communication_block_headings", "universities"
-  add_foreign_key "communication_blocks", "communication_block_headings", column: "heading_id"
   add_foreign_key "communication_blocks", "universities"
   add_foreign_key "communication_extranet_connections", "communication_extranets", column: "extranet_id"
   add_foreign_key "communication_extranet_connections", "universities"
diff --git a/test/fixtures/education/schools.yml b/test/fixtures/education/schools.yml
index fce634f27..50828e015 100644
--- a/test/fixtures/education/schools.yml
+++ b/test/fixtures/education/schools.yml
@@ -10,7 +10,6 @@
 #  longitude     :float
 #  name          :string
 #  phone         :string
-#  url           :string
 #  zipcode       :string
 #  created_at    :datetime         not null
 #  updated_at    :datetime         not null
-- 
GitLab