From 3741f1cfc97e0671fe22d33fdbf280a8ef0539fd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 19 Dec 2022 18:55:36 +0100
Subject: [PATCH] not null default language + filter js

---
 .../javascripts/admin/communication/init.js   |  1 +
 .../admin/communication/websites.js           | 56 +++++++++++++++++++
 app/models/communication/website.rb           |  4 +-
 .../website/configs/default_languages.rb      |  2 +-
 .../website/configs/default_permalinks.rb     |  2 +-
 .../website/configs/development_config.rb     |  2 +-
 .../website/configs/production_config.rb      |  2 +-
 .../communication/websites/_form.html.erb     |  4 +-
 config/locales/communication/en.yml           |  5 +-
 config/locales/communication/fr.yml           |  5 +-
 ...ault_language_to_communication_websites.rb |  5 ++
 db/schema.rb                                  |  4 +-
 test/fixtures/communication/websites.yml      |  2 +-
 13 files changed, 75 insertions(+), 19 deletions(-)
 create mode 100644 app/assets/javascripts/admin/communication/websites.js
 create mode 100644 db/migrate/20221219174414_set_required_default_language_to_communication_websites.rb

diff --git a/app/assets/javascripts/admin/communication/init.js b/app/assets/javascripts/admin/communication/init.js
index ab94a3b30..7d114c0b6 100644
--- a/app/assets/javascripts/admin/communication/init.js
+++ b/app/assets/javascripts/admin/communication/init.js
@@ -1,5 +1,6 @@
 //= require_self
 //= require ./menu_items
 //= require ./preview
+//= require ./websites
 
 window.osuny.communication = {};
diff --git a/app/assets/javascripts/admin/communication/websites.js b/app/assets/javascripts/admin/communication/websites.js
new file mode 100644
index 000000000..d43c0eabe
--- /dev/null
+++ b/app/assets/javascripts/admin/communication/websites.js
@@ -0,0 +1,56 @@
+/* global $ */
+window.osuny.communication.websites = {
+    init: function () {
+        'use strict';
+        this.languagesCheckboxes = document.querySelectorAll('.js-languages input[type="checkbox"]');
+        this.defaultLanguageSelect = document.querySelector('.js-default-language');
+        this.defaultLanguageOptions = this.defaultLanguageSelect.querySelectorAll('option');
+        this.initEvents();
+        this.onChangeCheckbox();
+    },
+
+    initEvents: function () {
+        'use strict';
+        var i;
+        for (i = 0 ; i < this.languagesCheckboxes.length ; i += 1) {
+            this.languagesCheckboxes[i].addEventListener('change', this.onChangeCheckbox.bind(this));
+        }
+    },
+
+    onChangeCheckbox: function () {
+        'use strict';
+        var languageCheckbox,
+            languageOption,
+            i;
+
+        // Clean options
+        this.defaultLanguageSelect.innerHTML = "";
+
+        // Re-hydrate options
+        for (i = 0; i < this.defaultLanguageOptions.length; i += 1) {
+            languageOption = this.defaultLanguageOptions[i];
+            languageCheckbox = document.querySelector('.js-languages input[type="checkbox"][value="' + languageOption.value + '"]')
+            if (languageOption.value === "" || languageCheckbox.checked) {
+                console.log(languageOption);
+                this.defaultLanguageSelect.appendChild(languageOption);
+            }
+        }
+    },
+
+    invoke: function () {
+        'use strict';
+        return {
+            init: this.init.bind(this)
+        };
+    }
+}.invoke();
+
+window.addEventListener('DOMContentLoaded', function () {
+    'use strict';
+    if (document.body.classList.contains('websites-new') ||
+            document.body.classList.contains('websites-edit') ||
+            document.body.classList.contains('websites-create') ||
+            document.body.classList.contains('websites-update')) {
+        window.osuny.communication.websites.init();
+    }
+});
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 6139d6a52..7fcb7aaa7 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -18,7 +18,7 @@
 #  created_at          :datetime         not null
 #  updated_at          :datetime         not null
 #  about_id            :uuid             indexed => [about_type]
-#  default_language_id :uuid             indexed
+#  default_language_id :uuid             not null, indexed
 #  university_id       :uuid             not null, indexed
 #
 # Indexes
@@ -52,7 +52,7 @@ class Communication::Website < ApplicationRecord
     gitlab: 1
   }
 
-  belongs_to :default_language, class_name: "Language", optional: true
+  belongs_to :default_language, class_name: "Language"
   has_and_belongs_to_many :languages,
                           class_name: 'Language',
                           join_table: 'communication_websites_languages',
diff --git a/app/models/communication/website/configs/default_languages.rb b/app/models/communication/website/configs/default_languages.rb
index d3d276779..dfaf62762 100644
--- a/app/models/communication/website/configs/default_languages.rb
+++ b/app/models/communication/website/configs/default_languages.rb
@@ -18,7 +18,7 @@
 #  created_at          :datetime         not null
 #  updated_at          :datetime         not null
 #  about_id            :uuid             indexed => [about_type]
-#  default_language_id :uuid             indexed
+#  default_language_id :uuid             not null, indexed
 #  university_id       :uuid             not null, indexed
 #
 # Indexes
diff --git a/app/models/communication/website/configs/default_permalinks.rb b/app/models/communication/website/configs/default_permalinks.rb
index 842de1d49..cb70b44bd 100644
--- a/app/models/communication/website/configs/default_permalinks.rb
+++ b/app/models/communication/website/configs/default_permalinks.rb
@@ -18,7 +18,7 @@
 #  created_at          :datetime         not null
 #  updated_at          :datetime         not null
 #  about_id            :uuid             indexed => [about_type]
-#  default_language_id :uuid             indexed
+#  default_language_id :uuid             not null, indexed
 #  university_id       :uuid             not null, indexed
 #
 # Indexes
diff --git a/app/models/communication/website/configs/development_config.rb b/app/models/communication/website/configs/development_config.rb
index 3d9d48815..f4f672d45 100644
--- a/app/models/communication/website/configs/development_config.rb
+++ b/app/models/communication/website/configs/development_config.rb
@@ -18,7 +18,7 @@
 #  created_at          :datetime         not null
 #  updated_at          :datetime         not null
 #  about_id            :uuid             indexed => [about_type]
-#  default_language_id :uuid             indexed
+#  default_language_id :uuid             not null, indexed
 #  university_id       :uuid             not null, indexed
 #
 # Indexes
diff --git a/app/models/communication/website/configs/production_config.rb b/app/models/communication/website/configs/production_config.rb
index f16360bd6..e40eb7abd 100644
--- a/app/models/communication/website/configs/production_config.rb
+++ b/app/models/communication/website/configs/production_config.rb
@@ -18,7 +18,7 @@
 #  created_at          :datetime         not null
 #  updated_at          :datetime         not null
 #  about_id            :uuid             indexed => [about_type]
-#  default_language_id :uuid             indexed
+#  default_language_id :uuid             not null, indexed
 #  university_id       :uuid             not null, indexed
 #
 # Indexes
diff --git a/app/views/admin/communication/websites/_form.html.erb b/app/views/admin/communication/websites/_form.html.erb
index 30a3dcc40..9530351b1 100644
--- a/app/views/admin/communication/websites/_form.html.erb
+++ b/app/views/admin/communication/websites/_form.html.erb
@@ -12,8 +12,8 @@
           <%= f.input :name %>
           <%= f.input :url %>
           <%= render 'admin/communication/abouts', f: f, i18n_key: 'activerecord.attributes.communication/website.about_' %>
-          <%= f.association :languages, as: :check_boxes %>
-          <%= f.association :default_language %>
+          <%= f.association :languages, as: :check_boxes, required: true, wrapper_html: { class: "js-languages" } %>
+          <%= f.association :default_language, include_blank: t('simple_form.include_blanks.defaults.language'), input_html: { class: "js-default-language" } %>
           <%= f.input :in_production %>
         </div>
       </div>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 8c279ae64..09ad7c1a4 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -697,7 +697,4 @@ en:
         description: If this field is empty, the "lead text" field will be used. If also emty the main text's begining will be used.
         full_width: On large screens, a full width page uses all available space for the content. This is good for landing pages, or to make them spectacular. If the page is not full width, the content column will be smaller to make reading easier. The unused space might be used for a table of contents.
       communication_website_post:
-        description: If this field is empty, the "lead text" field will be used. If also emty the main text's begining will be used.
-  include_blanks:
-    communication_website:
-      default_language: "Select a language"
\ No newline at end of file
+        description: If this field is empty, the "lead text" field will be used. If also emty the main text's begining will be used.
\ No newline at end of file
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 2e98a1652..ce630f8f5 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -697,7 +697,4 @@ fr:
         description: Si ce champ est vide le "chapô" sera utilisé. Si il est également vide le début du texte principal sera utilisé.
         full_width: Sur de grands écrans, la page en pleine largeur utilisera tout l'espace disponible, ce qui est pertinent pour événementialiser une page. Si la page n'est pas en pleine largeur, l'espace dédié au contenu sera réduit pour faciliter la lecture, et l'espace libre pourra être utilisé pour une table des matières facilitant la navigation.
       communication_website_post:
-        description: Si ce champ est vide le "chapô" sera utilisé. Si il est également vide le début du texte principal sera utilisé.
-  include_blanks:
-    communication_website:
-      default_language: "Sélectionnez une langue"
\ No newline at end of file
+        description: Si ce champ est vide le "chapô" sera utilisé. Si il est également vide le début du texte principal sera utilisé.
\ No newline at end of file
diff --git a/db/migrate/20221219174414_set_required_default_language_to_communication_websites.rb b/db/migrate/20221219174414_set_required_default_language_to_communication_websites.rb
new file mode 100644
index 000000000..16679cb3c
--- /dev/null
+++ b/db/migrate/20221219174414_set_required_default_language_to_communication_websites.rb
@@ -0,0 +1,5 @@
+class SetRequiredDefaultLanguageToCommunicationWebsites < ActiveRecord::Migration[7.0]
+  def change
+    change_column_null :communication_websites, :default_language_id, false
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index dea20520f..8822ce92e 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[7.0].define(version: 2022_12_19_170849) do
+ActiveRecord::Schema[7.0].define(version: 2022_12_19_174414) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -382,7 +382,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_12_19_170849) do
     t.string "plausible_url"
     t.string "git_branch"
     t.boolean "in_production", default: false
-    t.uuid "default_language_id"
+    t.uuid "default_language_id", null: false
     t.index ["about_type", "about_id"], name: "index_communication_websites_on_about"
     t.index ["default_language_id"], name: "index_communication_websites_on_default_language_id"
     t.index ["university_id"], name: "index_communication_websites_on_university_id"
diff --git a/test/fixtures/communication/websites.yml b/test/fixtures/communication/websites.yml
index 4b9fff274..d8b3f38cc 100644
--- a/test/fixtures/communication/websites.yml
+++ b/test/fixtures/communication/websites.yml
@@ -18,7 +18,7 @@
 #  created_at          :datetime         not null
 #  updated_at          :datetime         not null
 #  about_id            :uuid             indexed => [about_type]
-#  default_language_id :uuid             indexed
+#  default_language_id :uuid             not null, indexed
 #  university_id       :uuid             not null, indexed
 #
 # Indexes
-- 
GitLab