From d036e008d5c83b2ed1b01a259214be19207e59bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 15 Dec 2022 17:17:17 +0100
Subject: [PATCH] configs

---
 app/models/communication/website.rb           |  2 +-
 .../website/configs/default_languages.rb      | 46 +++++++++++++++++++
 .../{permalinks.rb => default_permalinks.rb}  |  6 +--
 .../website/configs/development_config.rb     | 46 +++++++++++++++++++
 .../{base_url.rb => production_config.rb}     |  6 +--
 app/models/communication/website/menu.rb      |  5 +-
 .../communication/website/page/with_kind.rb   |  2 +-
 .../communication/website/with_configs.rb     | 16 +++++--
 .../configs/base_urls/static.html.erb         |  1 -
 .../configs/default_languages/static.html.erb |  7 +++
 .../static.html.erb                           |  0
 .../development_config/static.html.erb        |  4 ++
 .../configs/production_config/static.html.erb |  6 +++
 13 files changed, 133 insertions(+), 14 deletions(-)
 create mode 100644 app/models/communication/website/configs/default_languages.rb
 rename app/models/communication/website/configs/{permalinks.rb => default_permalinks.rb} (84%)
 create mode 100644 app/models/communication/website/configs/development_config.rb
 rename app/models/communication/website/configs/{base_url.rb => production_config.rb} (84%)
 delete mode 100644 app/views/admin/communication/websites/configs/base_urls/static.html.erb
 create mode 100644 app/views/admin/communication/websites/configs/default_languages/static.html.erb
 rename app/views/admin/communication/websites/configs/{permalinks => default_permalinks}/static.html.erb (100%)
 create mode 100644 app/views/admin/communication/websites/configs/development_config/static.html.erb
 create mode 100644 app/views/admin/communication/websites/configs/production_config/static.html.erb

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 72f5ddb8b..30298135c 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -73,7 +73,7 @@ class Communication::Website < ApplicationRecord
   end
 
   def git_dependencies(website)
-    dependencies = [self, config_permalinks, config_base_url] + menus
+    dependencies = [self, config_default_languages, config_default_permalinks, config_development_config, config_production_config] + menus
     dependencies += pages + pages.map(&:active_storage_blobs).flatten
     dependencies += posts + posts.map(&:active_storage_blobs).flatten
     dependencies += people_with_facets + people.map(&:active_storage_blobs).flatten
diff --git a/app/models/communication/website/configs/default_languages.rb b/app/models/communication/website/configs/default_languages.rb
new file mode 100644
index 000000000..56f6c9865
--- /dev/null
+++ b/app/models/communication/website/configs/default_languages.rb
@@ -0,0 +1,46 @@
+# == Schema Information
+#
+# Table name: communication_websites
+#
+#  id               :uuid             not null, primary key
+#  about_type       :string           indexed => [about_id]
+#  access_token     :string
+#  git_branch       :string
+#  git_endpoint     :string
+#  git_provider     :integer          default("github")
+#  in_production    :boolean          default(FALSE)
+#  name             :string
+#  plausible_url    :string
+#  repository       :string
+#  style            :text
+#  style_updated_at :date
+#  url              :string
+#  created_at       :datetime         not null
+#  updated_at       :datetime         not null
+#  about_id         :uuid             indexed => [about_type]
+#  university_id    :uuid             not null, indexed
+#
+# Indexes
+#
+#  index_communication_websites_on_about          (about_type,about_id)
+#  index_communication_websites_on_university_id  (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_bb6a496c08  (university_id => universities.id)
+#
+class Communication::Website::Configs::DefaultLanguages < Communication::Website
+
+  def self.polymorphic_name
+    'Communication::Website::Configs::DefaultLanguages'
+  end
+
+  def git_path(website)
+    "config/_default/languages.yaml"
+  end
+
+  def template_static
+    "admin/communication/websites/configs/default_languages/static"
+  end
+
+end
diff --git a/app/models/communication/website/configs/permalinks.rb b/app/models/communication/website/configs/default_permalinks.rb
similarity index 84%
rename from app/models/communication/website/configs/permalinks.rb
rename to app/models/communication/website/configs/default_permalinks.rb
index cf836fd47..b028dd2b6 100644
--- a/app/models/communication/website/configs/permalinks.rb
+++ b/app/models/communication/website/configs/default_permalinks.rb
@@ -29,10 +29,10 @@
 #
 #  fk_rails_bb6a496c08  (university_id => universities.id)
 #
-class Communication::Website::Configs::Permalinks < Communication::Website
+class Communication::Website::Configs::DefaultPermalinks < Communication::Website
 
   def self.polymorphic_name
-    'Communication::Website::Configs::Permalinks'
+    'Communication::Website::Configs::DefaultPermalinks'
   end
 
   def git_path(website)
@@ -40,7 +40,7 @@ class Communication::Website::Configs::Permalinks < Communication::Website
   end
 
   def template_static
-    "admin/communication/websites/configs/permalinks/static"
+    "admin/communication/websites/configs/default_permalinks/static"
   end
 
 end
diff --git a/app/models/communication/website/configs/development_config.rb b/app/models/communication/website/configs/development_config.rb
new file mode 100644
index 000000000..5d5d9a8cb
--- /dev/null
+++ b/app/models/communication/website/configs/development_config.rb
@@ -0,0 +1,46 @@
+# == Schema Information
+#
+# Table name: communication_websites
+#
+#  id               :uuid             not null, primary key
+#  about_type       :string           indexed => [about_id]
+#  access_token     :string
+#  git_branch       :string
+#  git_endpoint     :string
+#  git_provider     :integer          default("github")
+#  in_production    :boolean          default(FALSE)
+#  name             :string
+#  plausible_url    :string
+#  repository       :string
+#  style            :text
+#  style_updated_at :date
+#  url              :string
+#  created_at       :datetime         not null
+#  updated_at       :datetime         not null
+#  about_id         :uuid             indexed => [about_type]
+#  university_id    :uuid             not null, indexed
+#
+# Indexes
+#
+#  index_communication_websites_on_about          (about_type,about_id)
+#  index_communication_websites_on_university_id  (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_bb6a496c08  (university_id => universities.id)
+#
+class Communication::Website::Configs::DevelopmentConfig < Communication::Website
+
+  def self.polymorphic_name
+    'Communication::Website::Configs::DevelopmentConfig'
+  end
+
+  def git_path(website)
+    "config/development/config.yaml"
+  end
+
+  def template_static
+    "admin/communication/websites/configs/development_config/static"
+  end
+
+end
diff --git a/app/models/communication/website/configs/base_url.rb b/app/models/communication/website/configs/production_config.rb
similarity index 84%
rename from app/models/communication/website/configs/base_url.rb
rename to app/models/communication/website/configs/production_config.rb
index 080e03ddd..b5aff1621 100644
--- a/app/models/communication/website/configs/base_url.rb
+++ b/app/models/communication/website/configs/production_config.rb
@@ -29,10 +29,10 @@
 #
 #  fk_rails_bb6a496c08  (university_id => universities.id)
 #
-class Communication::Website::Configs::BaseUrl < Communication::Website
+class Communication::Website::Configs::ProductionConfig < Communication::Website
 
   def self.polymorphic_name
-    'Communication::Website::Configs::BaseUrl'
+    'Communication::Website::Configs::ProductionConfig'
   end
 
   def git_path(website)
@@ -40,7 +40,7 @@ class Communication::Website::Configs::BaseUrl < Communication::Website
   end
 
   def template_static
-    "admin/communication/websites/configs/base_urls/static"
+    "admin/communication/websites/configs/production_config/static"
   end
 
 end
diff --git a/app/models/communication/website/menu.rb b/app/models/communication/website/menu.rb
index 9a792698b..6317937ec 100644
--- a/app/models/communication/website/menu.rb
+++ b/app/models/communication/website/menu.rb
@@ -39,7 +39,10 @@ class Communication::Website::Menu < ApplicationRecord
   end
 
   def git_path(website)
-    "data/menus/#{identifier}.yml"
+    # TODO I18n : Right now, we use the language of the website. Add a language to the menu (and future translations)
+    path = "data/menus/"
+    path += "#{website.languages.first.iso_code}/" if website.languages.any?
+    "#{path}#{identifier}.yml"
   end
 
   def template_static
diff --git a/app/models/communication/website/page/with_kind.rb b/app/models/communication/website/page/with_kind.rb
index a504be139..b3724e964 100644
--- a/app/models/communication/website/page/with_kind.rb
+++ b/app/models/communication/website/page/with_kind.rb
@@ -51,7 +51,7 @@ module Communication::Website::Page::WithKind
     end
 
     def special_page_git_dependencies(website)
-      dependencies = [website.config_permalinks]
+      dependencies = [website.config_default_permalinks]
       case kind
       when "communication_posts"
         dependencies += [
diff --git a/app/models/communication/website/with_configs.rb b/app/models/communication/website/with_configs.rb
index c08987ad4..34cb40689 100644
--- a/app/models/communication/website/with_configs.rb
+++ b/app/models/communication/website/with_configs.rb
@@ -3,12 +3,20 @@ module Communication::Website::WithConfigs
 
   included do
 
-    def config_permalinks
-      @config_permalinks ||= Communication::Website::Configs::Permalinks.find(id)
+    def config_default_languages
+      @config_default_languages ||= Communication::Website::Configs::DefaultLanguages.find(id)
     end
 
-    def config_base_url
-      @config_base_url ||= Communication::Website::Configs::BaseUrl.find(id)
+    def config_default_permalinks
+      @config_default_permalinks ||= Communication::Website::Configs::DefaultPermalinks.find(id)
+    end
+
+    def config_development_config
+      @config_development_config ||= Communication::Website::Configs::DevelopmentConfig.find(id)
+    end
+
+    def config_production_config
+      @config_production_config ||= Communication::Website::Configs::ProductionConfig.find(id)
     end
 
   end
diff --git a/app/views/admin/communication/websites/configs/base_urls/static.html.erb b/app/views/admin/communication/websites/configs/base_urls/static.html.erb
deleted file mode 100644
index 47d8819e1..000000000
--- a/app/views/admin/communication/websites/configs/base_urls/static.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-baseURL: <%= @website.url %>
diff --git a/app/views/admin/communication/websites/configs/default_languages/static.html.erb b/app/views/admin/communication/websites/configs/default_languages/static.html.erb
new file mode 100644
index 000000000..761b5f61a
--- /dev/null
+++ b/app/views/admin/communication/websites/configs/default_languages/static.html.erb
@@ -0,0 +1,7 @@
+<% @website.languages.each do |language| %>
+<%= language.iso_code %>:
+  title: <%= @website %><%# TODO I18n: Traduire le nom du site %>
+  contentDir: content/<%= language.iso_code %>
+  languageCode: <%= language.iso_code %>
+  languageName: <%= language.name %>
+<% end %>
\ No newline at end of file
diff --git a/app/views/admin/communication/websites/configs/permalinks/static.html.erb b/app/views/admin/communication/websites/configs/default_permalinks/static.html.erb
similarity index 100%
rename from app/views/admin/communication/websites/configs/permalinks/static.html.erb
rename to app/views/admin/communication/websites/configs/default_permalinks/static.html.erb
diff --git a/app/views/admin/communication/websites/configs/development_config/static.html.erb b/app/views/admin/communication/websites/configs/development_config/static.html.erb
new file mode 100644
index 000000000..a7b621aa3
--- /dev/null
+++ b/app/views/admin/communication/websites/configs/development_config/static.html.erb
@@ -0,0 +1,4 @@
+## LANGUAGE
+<%# TODO I18n: Add "Default language" in Communication::Website %>
+defaultContentLanguage: <%= @website.languages.first.iso_code %>
+defaultContentLanguageInSubdir: <%= @website.languages.many? %>
\ No newline at end of file
diff --git a/app/views/admin/communication/websites/configs/production_config/static.html.erb b/app/views/admin/communication/websites/configs/production_config/static.html.erb
new file mode 100644
index 000000000..249cc5b70
--- /dev/null
+++ b/app/views/admin/communication/websites/configs/production_config/static.html.erb
@@ -0,0 +1,6 @@
+baseURL: <%= @website.url %>
+
+## LANGUAGE
+<%# TODO I18n: Add "Default language" in Communication::Website %>
+defaultContentLanguage: <%= @website.languages.first.iso_code %>
+defaultContentLanguageInSubdir: <%= @website.languages.many? %>
\ No newline at end of file
-- 
GitLab