From eca95f90de3de7c6507fc529e00e1e36f136a234 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 28 Jul 2026 00:02:55 +0200 Subject: Let editors manage menu titles in both locales --- app/controllers/menu_items_controller.rb | 6 ++-- app/models/menu_item.rb | 6 ++++ app/views/menu_items/edit.html.erb | 3 +- app/views/menu_items/new.html.erb | 3 +- config/locales/de.yml | 2 ++ config/locales/en.yml | 2 ++ test/controllers/menu_items_controller_test.rb | 42 ++++++++++++++++++++-- .../page_translations_controller_test.rb | 2 +- 8 files changed, 56 insertions(+), 10 deletions(-) diff --git a/app/controllers/menu_items_controller.rb b/app/controllers/menu_items_controller.rb index b095c8df..8b4e636e 100644 --- a/app/controllers/menu_items_controller.rb +++ b/app/controllers/menu_items_controller.rb @@ -19,7 +19,9 @@ class MenuItemsController < ApplicationController end def create - if MenuItem.create( menu_item_params ) + @menu_item = MenuItem.new( menu_item_params ) + + if @menu_item.save redirect_to menu_items_path else render :new @@ -58,6 +60,6 @@ class MenuItemsController < ApplicationController private def menu_item_params - params.require(:menu_item).permit(:node_id, :path, :position, :type, :title, :type_id) + params.require(:menu_item).permit(:node_id, :path, :position, :titles => {}) end end diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index 7769b7fa..b8df9ddb 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -3,11 +3,17 @@ class MenuItem < ApplicationRecord default_scope -> { where(:type => "MenuItem") } translates :title + validates :title, presence: true acts_as_list :scope => :type before_save :determine_type_id + def titles=(values) + values.each do |locale, value| + Globalize.with_locale(locale) { self.title = value.to_s.strip.presence } + end + end private diff --git a/app/views/menu_items/edit.html.erb b/app/views/menu_items/edit.html.erb index 0652dc78..2bd65575 100644 --- a/app/views/menu_items/edit.html.erb +++ b/app/views/menu_items/edit.html.erb @@ -15,8 +15,7 @@
<%= MenuItem.human_attribute_name(:path) %>
<%= f.text_field :path %>
-
<%= MenuItem.human_attribute_name(:title) %>
-
<%= f.text_field :title %>
+ <%= render "title_fields", :menu_item => @menu_item %>
<%= f.submit t("admin.common.update") %>
diff --git a/app/views/menu_items/new.html.erb b/app/views/menu_items/new.html.erb index 713ac967..08872ec0 100644 --- a/app/views/menu_items/new.html.erb +++ b/app/views/menu_items/new.html.erb @@ -13,8 +13,7 @@
<%= MenuItem.human_attribute_name(:path) %>
<%= f.text_field :path %>
-
<%= MenuItem.human_attribute_name(:title) %>
-
<%= f.text_field :title %>
+ <%= render "title_fields", :menu_item => @menu_item %>
<%= f.submit t("admin.common.create") %>
<% end %> diff --git a/config/locales/de.yml b/config/locales/de.yml index 30e2f1b5..7253d1e4 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -638,6 +638,8 @@ de: title: "Menüeintrag hinzufügen" edit: title: "Menüeintrag bearbeiten" + title_fields: + falls_back: "Menu-Title leer lassen, fall-back nach deutsch" layouts: application: diff --git a/config/locales/en.yml b/config/locales/en.yml index f76dfecb..b21008f7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -455,6 +455,8 @@ en: title: "Add Menu Item" edit: title: "Edit Menu Item" + title_fields: + falls_back: "Menu item title falls back to German if empty" flash: common: diff --git a/test/controllers/menu_items_controller_test.rb b/test/controllers/menu_items_controller_test.rb index c47467a3..15a7b30b 100644 --- a/test/controllers/menu_items_controller_test.rb +++ b/test/controllers/menu_items_controller_test.rb @@ -1,8 +1,44 @@ require 'test_helper' class MenuItemsControllerTest < ActionController::TestCase - # Replace this with your real tests. - test "the truth" do - assert true + def create_menu_item(title = "Ausgangstitel") + item = MenuItem.new(:path => "/menu_title_test") + item.titles = { I18n.default_locale.to_s => title } + item.save! + item + end + + test "updating stores a title per locale" do + login_as :quentin + item = create_menu_item + + patch :update, params: { :id => item.id, + :menu_item => { :titles => { "de" => "Transparenz", "en" => "Transparency" } } } + + assert_equal "Transparenz", item.reload.translations.find_by(:locale => "de").title + assert_equal "Transparency", item.translations.find_by(:locale => "en").title + end + + test "blanking a non-default title falls back to the default locale" do + login_as :quentin + item = create_menu_item + patch :update, params: { :id => item.id, + :menu_item => { :titles => { "de" => "Transparenz", "en" => "Transparency" } } } + + patch :update, params: { :id => item.id, + :menu_item => { :titles => { "de" => "Transparenz", "en" => "" } } } + + item.reload + assert_equal "Transparenz", Globalize.with_locale(:en) { item.title } + end + + test "a blank default title is rejected" do + login_as :quentin + item = create_menu_item + patch :update, params: { :id => item.id, + :menu_item => { :titles => { "de" => "" } } } + + assert_response :success # re-rendered :edit, not a redirect + assert_not_equal "", item.reload.translations.find_by(:locale => "de").title end end diff --git a/test/controllers/page_translations_controller_test.rb b/test/controllers/page_translations_controller_test.rb index 7484a203..31eefe84 100644 --- a/test/controllers/page_translations_controller_test.rb +++ b/test/controllers/page_translations_controller_test.rb @@ -30,7 +30,7 @@ class PageTranslationsControllerTest < ActionController::TestCase node = Node.root.children.create!(:slug => "translations_exit_test") node.lock_for_editing!(users(:quentin)) - patch :update, params: { :node_id => node.id, :translation_locale => "en", :page => { :title => "x" }, :commit => "Save + Unlock + Exit" } + patch :update, params: { :node_id => node.id, :translation_locale => "en", :page => { :title => "x" }, :unlock_exit => "1" } assert_nil node.reload.lock_owner assert_redirected_to node_path(node) -- cgit v1.3