diff --git a/Gemfile.lock b/Gemfile.lock index 63ce22f80665c1d805ac01b43a6f4e4f06ed641d..945f4dcf00ec6b00b07fdc50ceaf89010334b44b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -103,16 +103,16 @@ GEM autoprefixer-rails (10.4.13.0) execjs (~> 2) aws-eventstream (1.2.0) - aws-partitions (1.785.0) - aws-sdk-core (3.177.0) + aws-partitions (1.786.0) + aws-sdk-core (3.178.0) aws-eventstream (~> 1, >= 1.0.2) aws-partitions (~> 1, >= 1.651.0) aws-sigv4 (~> 1.5) jmespath (~> 1, >= 1.6.1) - aws-sdk-kms (1.70.0) + aws-sdk-kms (1.71.0) aws-sdk-core (~> 3, >= 3.177.0) aws-sigv4 (~> 1.1) - aws-sdk-s3 (1.128.0) + aws-sdk-s3 (1.130.0) aws-sdk-core (~> 3, >= 3.177.0) aws-sdk-kms (~> 1) aws-sigv4 (~> 1.6) @@ -393,7 +393,7 @@ GEM requests (~> 1.0.2) pg (1.5.3) popper_js (2.11.7) - public_suffix (5.0.1) + public_suffix (5.0.3) puma (6.3.0) nio4r (~> 2.0) racc (1.7.1) diff --git a/app/controllers/admin/communication/websites/menus/items_controller.rb b/app/controllers/admin/communication/websites/menus/items_controller.rb index d110e26bb7c47569258c2f039b45579bd721cf6a..dd37cd429182e34ba06a54cf0473ea10cf06584f 100644 --- a/app/controllers/admin/communication/websites/menus/items_controller.rb +++ b/app/controllers/admin/communication/websites/menus/items_controller.rb @@ -50,6 +50,7 @@ class Admin::Communication::Websites::Menus::ItemsController < Admin::Communicat end def create + @menu.stop_automatism! @item.menu = @menu @item.website = @website if @item.save @@ -62,6 +63,7 @@ class Admin::Communication::Websites::Menus::ItemsController < Admin::Communicat end def update + @menu.stop_automatism! if @item.update(item_params) redirect_to redirect_path(@item), notice: t('admin.successfully_updated_html', model: @item.to_s) diff --git a/app/controllers/admin/communication/websites/pages_controller.rb b/app/controllers/admin/communication/websites/pages_controller.rb index cad0b279aa86865e4a37242adf4c8a504d736b7a..c99e344ddcb8420035192c9ac9ff8193b2eea971 100644 --- a/app/controllers/admin/communication/websites/pages_controller.rb +++ b/app/controllers/admin/communication/websites/pages_controller.rb @@ -24,6 +24,7 @@ class Admin::Communication::Websites::PagesController < Admin::Communication::We end old_parent_page.sync_with_git parent_page.sync_with_git + @website.generate_automatic_menus(parent_page.language) end def children diff --git a/app/models/communication/website/menu.rb b/app/models/communication/website/menu.rb index 8f4435ce04d301f6338c2c10256be89ad6a4363f..fc424526fd36643738d39bae1f531e202a11f635 100644 --- a/app/models/communication/website/menu.rb +++ b/app/models/communication/website/menu.rb @@ -3,6 +3,7 @@ # Table name: communication_website_menus # # id :uuid not null, primary key +# automatic :boolean default(TRUE) # identifier :string # title :string # created_at :datetime not null @@ -29,6 +30,7 @@ class Communication::Website::Menu < ApplicationRecord include AsDirectObject include Sanitizable + include WithAutomatism include WithTranslations include WithUniversity diff --git a/app/models/communication/website/menu/with_automatism.rb b/app/models/communication/website/menu/with_automatism.rb new file mode 100644 index 0000000000000000000000000000000000000000..6ac0fb714111ad57fe5b71a671e53c19e1a9e628 --- /dev/null +++ b/app/models/communication/website/menu/with_automatism.rb @@ -0,0 +1,61 @@ +module Communication::Website::Menu::WithAutomatism + extend ActiveSupport::Concern + + included do + scope :automatic, -> { where(automatic: true) } + end + + def generate_automatically + begin + pause_git_sync + clear_items + create_items + ensure + unpause_git_sync + end + end + + def stop_automatism! + update_column :automatic, false + end + + protected + + def pause_git_sync + Communication::Website::Menu.skip_callback :save, :after, :connect_dependencies + Communication::Website::Menu.skip_callback :touch, :after, :connect_dependencies + end + + def clear_items + # We don't use the destroy method to prevent items' callbacks + items.delete_all + end + + def create_items + home = website.pages.where(language_id: language_id).root.first + create_items_for_children_of(home) if home + end + + def create_items_for_children_of(page, parent_item = nil) + page.children.ordered.each do |child_page| + next if child_page.default_menu_identifier != identifier + create_item_for(child_page, parent_item) + end + end + + def create_item_for(page, parent_item = nil) + item = items.create kind: :page, + about: page, + title: page.title, + position: page.position, + website: website, + university: university, + parent: parent_item + create_items_for_children_of(page, item) + end + + def unpause_git_sync + Communication::Website::Menu.set_callback :save, :after, :connect_dependencies + Communication::Website::Menu.set_callback :touch, :after, :connect_dependencies + end +end \ No newline at end of file diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index dc4c2a23056f40047d55c26713ed1ab9cdd61e23..d61dff08e4dacf04feaae9b215c021bfd2bdc7f0 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -48,6 +48,7 @@ class Communication::Website::Page < ApplicationRecord include AsDirectObject include Sanitizable include WithAccessibility + include WithAutomaticMenus include WithBlobs include WithBlocks include WithDuplication diff --git a/app/models/communication/website/page/accessibility.rb b/app/models/communication/website/page/accessibility.rb index c18349324c1ad87cec6e451ea17e0d3647d3e57c..29864c889b9bdbea32f691634e49bef85cf8db5a 100644 --- a/app/models/communication/website/page/accessibility.rb +++ b/app/models/communication/website/page/accessibility.rb @@ -51,6 +51,10 @@ class Communication::Website::Page::Accessibility < Communication::Website::Page false end + def default_menu_identifier + 'legal' + end + def generate_from_template generate_declaration generate_results diff --git a/app/models/communication/website/page/legal_term.rb b/app/models/communication/website/page/legal_term.rb index c5c5fe46a97168e3e964f56e56a8b99ea0c9a2c0..0ba9097a2e0cfce86f2bb03349e59eeb705204e9 100644 --- a/app/models/communication/website/page/legal_term.rb +++ b/app/models/communication/website/page/legal_term.rb @@ -50,5 +50,9 @@ class Communication::Website::Page::LegalTerm < Communication::Website::Page def is_listed_among_children? false end + + def default_menu_identifier + 'legal' + end end diff --git a/app/models/communication/website/page/privacy_policy.rb b/app/models/communication/website/page/privacy_policy.rb index 8ab378817f65934bb453ca2769b300e18ef05d61..b1b588b0cfa213e2c4381983d850d579608c6b76 100644 --- a/app/models/communication/website/page/privacy_policy.rb +++ b/app/models/communication/website/page/privacy_policy.rb @@ -50,5 +50,9 @@ class Communication::Website::Page::PrivacyPolicy < Communication::Website::Page def is_listed_among_children? false end + + def default_menu_identifier + 'legal' + end end diff --git a/app/models/communication/website/page/sitemap.rb b/app/models/communication/website/page/sitemap.rb index 70b355d3d68b290af0312f9d0760b7bd295be64a..6880c938b5313c648467e6aeff1072639f757133 100644 --- a/app/models/communication/website/page/sitemap.rb +++ b/app/models/communication/website/page/sitemap.rb @@ -67,4 +67,8 @@ class Communication::Website::Page::Sitemap < Communication::Website::Page 'sitemap' end + def default_menu_identifier + 'legal' + end + end diff --git a/app/models/communication/website/page/with_automatic_menus.rb b/app/models/communication/website/page/with_automatic_menus.rb new file mode 100644 index 0000000000000000000000000000000000000000..74f8a32fd96d0395817e451ebae548d345f58891 --- /dev/null +++ b/app/models/communication/website/page/with_automatic_menus.rb @@ -0,0 +1,13 @@ +module Communication::Website::Page::WithAutomaticMenus + extend ActiveSupport::Concern + + included do + after_save :generate_automatic_menus + end + + protected + + def generate_automatic_menus + website.generate_automatic_menus(language) + end +end \ No newline at end of file diff --git a/app/models/communication/website/page/with_type.rb b/app/models/communication/website/page/with_type.rb index dd1333e11bd8de027a238c2237c6ba93708f984b..d264af369064759f049f3e1015e4e7562f564416 100644 --- a/app/models/communication/website/page/with_type.rb +++ b/app/models/communication/website/page/with_type.rb @@ -86,6 +86,10 @@ module Communication::Website::Page::WithType nil end + def default_menu_identifier + 'primary' + end + def generate_from_template end diff --git a/app/models/communication/website/with_menus.rb b/app/models/communication/website/with_menus.rb index 771c77f0d8b465496fa7c5150a0567d83c70e4a5..b32d411cbae5acd2f8e58bdb37a41b8f48d24cee 100644 --- a/app/models/communication/website/with_menus.rb +++ b/app/models/communication/website/with_menus.rb @@ -64,9 +64,13 @@ module Communication::Website::WithMenus def initialize_menus find_or_create_menu 'primary' find_or_create_menu 'social' - find_or_create_menu('legal') do |menu| - # Only executed after menu creation - fill_legal_menu(menu) + find_or_create_menu 'legal' + generate_automatic_menus(default_language) + end + + def generate_automatic_menus(language) + menus.automatic.for_language(language).find_each do |menu| + menu.generate_automatically end end @@ -76,24 +80,6 @@ module Communication::Website::WithMenus menu = menus.where(identifier: identifier, university: university, language: default_language).first_or_initialize do |menu| menu.title = I18n.t("communication.website.menus.default_title.#{identifier}") end - unless menu.persisted? - menu.save - yield(menu) if block_given? - end - menu - end - - def fill_legal_menu(menu) - [ - Communication::Website::Page::LegalTerm, - Communication::Website::Page::PrivacyPolicy, - Communication::Website::Page::Accessibility, - Communication::Website::Page::Sitemap - ].each do |page_class| - page = special_page(page_class, language: menu.language) - menu.items.where(kind: 'page', about: page, university: university, website: self).first_or_create do |item| - item.title = page.title - end - end + menu.save unless menu.persisted? end end diff --git a/app/views/admin/communication/websites/menus/_list.html.erb b/app/views/admin/communication/websites/menus/_list.html.erb index 1e162483b622a0f77858a3b6f99ed7dba2f335a8..f372f34de3d0649e17004bb6169e11527f87b0ab 100644 --- a/app/views/admin/communication/websites/menus/_list.html.erb +++ b/app/views/admin/communication/websites/menus/_list.html.erb @@ -5,6 +5,7 @@ <th><%= Communication::Website::Menu.human_attribute_name('title') %></th> <th><%= Communication::Website::Menu.human_attribute_name('items') %></th> <th></th> + <th></th> </tr> </thead> <tbody> @@ -12,6 +13,13 @@ <tr> <td><%= link_to menu, admin_communication_website_menu_path(website_id: menu.website.id, id: menu.id) %></td> <td><%= menu.items.count %></td> + <td> + <% if menu.automatic %> + <span class="badge bg-success"> + <%= Communication::Website::Menu.human_attribute_name('automatic') %> + </span> + <% end %> + </td> <td> <div class="btn-group" role="group"> <%= link_to t('edit'), diff --git a/app/views/admin/communication/websites/menus/show.html.erb b/app/views/admin/communication/websites/menus/show.html.erb index 249e1ec2a0b7aa7e7e330db6a294f4028f32be3c..6ee945b33e16d5c7ff8f612774ce8488a897e3ea 100644 --- a/app/views/admin/communication/websites/menus/show.html.erb +++ b/app/views/admin/communication/websites/menus/show.html.erb @@ -7,7 +7,15 @@ new_admin_communication_website_menu_item_path(website_id: @website, menu_id: @menu.id), class: button_classes if can?(:create, Communication::Website::Menu::Item) %> <%= osuny_panel Communication::Website::Menu::Item.model_name.human(count: 2), action: action do %> - <%= render 'admin/communication/websites/menus/items/list', items: @root_items if @items.any? %> + <% if @menu.automatic %> + <div class="p-3 mb-2 border border-success"> + <span class="badge bg-success"> + <%= Communication::Website::Menu.human_attribute_name('automatic') %> + </span> + <p class="mt-2 mb-0"><%= sanitize t('communication.website.menus.automatic') %></p> + </div> + <% end %> + <%= render 'admin/communication/websites/menus/items/list', items: @root_items if @items.any? %> <% end %> <% end %> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index ac788040f39a6f32fc96ca6894924f171cf870ec..53d7b5157d2a2120082a6d5a2df5f31cafca3a94 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -164,6 +164,7 @@ en: communication/website/imported/medium: filename: Filename communication/website/menu: + automatic: Automatic identifier: Identifier items: Items title: Title @@ -709,6 +710,7 @@ en: last_pages: Last pages last_posts: Last posts menus: + automatic: This menu is created automatically. <br>This means that it reflects the site's tree structure. <br>As soon as you want to modify it, by creating an item, the automatism will stop. default_title: legal: Legal primary: Main menu diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index 9f52cbd6066f6e617aa1a534727d06d82667ea32..a38a79a3dc2a5fa1751949fa57a2f0c893f3c0e5 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -164,6 +164,7 @@ fr: communication/website/imported/medium: filename: Nom du fichier communication/website/menu: + automatic: Automatique identifier: Identifiant items: Éléments title: Titre @@ -706,6 +707,7 @@ fr: last_pages: Dernières pages last_posts: Dernières actualités menus: + automatic: Ce menu se constitue automatiquement. <br>Cela signifie qu'il reflète l'arborescence du site. <br>Dès que vous souhaiterez le modifier, en créant un élément, l'automatisme s'arrêtera. default_title: legal: Informations légales primary: Menu principal diff --git a/db/migrate/20230716050520_add_automatic_to_website_menus.rb b/db/migrate/20230716050520_add_automatic_to_website_menus.rb new file mode 100644 index 0000000000000000000000000000000000000000..e02d5fd7f8a46034a20ef7553d3e2eb3f1801f59 --- /dev/null +++ b/db/migrate/20230716050520_add_automatic_to_website_menus.rb @@ -0,0 +1,9 @@ +class AddAutomaticToWebsiteMenus < ActiveRecord::Migration[7.0] + def change + add_column :communication_website_menus, :automatic, :boolean, default: true + + Communication::Website::Menu.find_each do |menu| + menu.update_column :automatic, false if menu.items.any? + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 112100075b532564a850f119196fe83a9dcb85e7..7ab94a7582f9ac7507748f6eb2d1b485c074ec49 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: 2023_07_11_073707) do +ActiveRecord::Schema[7.0].define(version: 2023_07_16_050520) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -105,8 +105,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_11_073707) do t.datetime "updated_at", null: false t.string "title" t.boolean "published", default: true - t.uuid "communication_website_id" t.uuid "heading_id" + t.uuid "communication_website_id" t.index ["about_type", "about_id"], name: "index_communication_website_blocks_on_about" t.index ["communication_website_id"], name: "index_communication_blocks_on_communication_website_id" t.index ["heading_id"], name: "index_communication_blocks_on_heading_id" @@ -223,7 +223,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_11_073707) do t.text "home_sentence" t.text "sass" t.text "css" - t.boolean "allow_experiences_modification", default: true t.index ["about_type", "about_id"], name: "index_communication_extranets_on_about" t.index ["university_id"], name: "index_communication_extranets_on_university_id" end @@ -425,6 +424,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_11_073707) do t.datetime "updated_at", null: false t.uuid "original_id" t.uuid "language_id", null: false + t.boolean "automatic", default: true t.index ["communication_website_id"], name: "idx_comm_website_menus_on_communication_website_id" t.index ["language_id"], name: "index_communication_website_menus_on_language_id" t.index ["original_id"], name: "index_communication_website_menus_on_original_id" @@ -462,7 +462,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_11_073707) do t.index ["university_id"], name: "index_communication_website_pages_on_university_id" end - create_table "communication_website_permalinks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + create_table "communication_website_permalinks", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t| t.uuid "university_id", null: false t.uuid "website_id", null: false t.string "about_type", null: false diff --git a/test/fixtures/communication/website/menus.yml b/test/fixtures/communication/website/menus.yml index 5ede2b15cbd4a1b3734191cb65fe4b41d17c226f..a0f4022412a332eebdcb61f36c936f1db20e564a 100644 --- a/test/fixtures/communication/website/menus.yml +++ b/test/fixtures/communication/website/menus.yml @@ -3,6 +3,7 @@ # Table name: communication_website_menus # # id :uuid not null, primary key +# automatic :boolean default(TRUE) # identifier :string # title :string # created_at :datetime not null