From 88ebeaac7fcf140cbab489e841bf9c197f84c4ea Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 14 Jul 2026 02:17:04 +0200 Subject: Make revisions locale aware --- app/controllers/page_translations_controller.rb | 5 ----- app/controllers/revisions_controller.rb | 16 +++++++++++++--- app/views/page_translations/edit.html.erb | 2 ++ app/views/page_translations/index.html.erb | 19 ------------------- app/views/page_translations/show.html.erb | 2 ++ app/views/revisions/index.html.erb | 19 +++++++++++++++++-- app/views/revisions/show.html.erb | 11 ++++++----- config/routes.rb | 2 +- test/controllers/page_translations_controller_test.rb | 12 ------------ 9 files changed, 41 insertions(+), 47 deletions(-) delete mode 100644 app/views/page_translations/index.html.erb diff --git a/app/controllers/page_translations_controller.rb b/app/controllers/page_translations_controller.rb index 9446cd2..38a7c4f 100644 --- a/app/controllers/page_translations_controller.rb +++ b/app/controllers/page_translations_controller.rb @@ -5,11 +5,6 @@ class PageTranslationsController < ApplicationController before_action :find_node before_action :find_locale, :only => [:show, :edit, :update, :autosave, :destroy] - def index - page = @node.draft || @node.head - @translations = page ? page.translation_summary : [] - end - def show @page = @node.draft || @node.head @translation = @page.translations.find_by(:locale => @locale) diff --git a/app/controllers/revisions_controller.rb b/app/controllers/revisions_controller.rb index 0aed243..c5932a4 100644 --- a/app/controllers/revisions_controller.rb +++ b/app/controllers/revisions_controller.rb @@ -5,10 +5,11 @@ class RevisionsController < ApplicationController before_action :login_required layout 'admin' - + def index - @node = Node.find(params[:node_id]) - @pages = @node.pages.all + @node = Node.find(params[:node_id]) + @pages = @node.pages.all + @locale = resolve_locale(params[:locale]) end def diff @@ -43,6 +44,7 @@ class RevisionsController < ApplicationController def show @node = Node.find(params[:node_id]) @page = @node.pages.find(params[:id]) + @locale = resolve_locale(params[:locale]) end def restore @@ -51,4 +53,12 @@ class RevisionsController < ApplicationController flash[:notice] = "Revision #{page.revision} restored" redirect_to node_path(page.node) end + + private + + def resolve_locale(requested) + candidate = requested.presence&.to_sym + allowed = [I18n.default_locale] + Page.non_default_locales + allowed.include?(candidate) ? candidate : I18n.default_locale + end end diff --git a/app/views/page_translations/edit.html.erb b/app/views/page_translations/edit.html.erb index 89b594e..981fb74 100644 --- a/app/views/page_translations/edit.html.erb +++ b/app/views/page_translations/edit.html.erb @@ -2,6 +2,8 @@ Editing the <%= @locale.to_s.upcase %> translation of <%= title_for_node(@node) %>. +

<%= link_to 'View revision history', node_revisions_path(@node, :locale => @locale) %>

+

Metadata such as images, tags, template, and author belong to the page as a whole, not to a single translation — change those from the diff --git a/app/views/page_translations/index.html.erb b/app/views/page_translations/index.html.erb deleted file mode 100644 index a8c4f5d..0000000 --- a/app/views/page_translations/index.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -

Translations — <%= title_for_node(@node) %>

- -
-
- <% @translations.each do |t| %> -
- <%= t[:locale].to_s.upcase %> - <% if t[:exists] %> - <%= t[:title] %> — updated <%= t[:updated_at] %> - <% if t[:outdated] %>may be outdated<% end %> - <%= link_to 'Edit', edit_node_translation_path(@node, t[:locale]) %> - <% else %> - Not yet translated. - <%= link_to 'Add', edit_node_translation_path(@node, t[:locale]) %> - <% end %> -
- <% end %> -
-
diff --git a/app/views/page_translations/show.html.erb b/app/views/page_translations/show.html.erb index c5bd151..60eb7cf 100644 --- a/app/views/page_translations/show.html.erb +++ b/app/views/page_translations/show.html.erb @@ -1,5 +1,7 @@

Compare — <%= title_for_node(@node) %>

+

<%= link_to 'View revision history', node_revisions_path(@node, :locale => @locale) %>

+

<%= @locale.to_s.upcase %>

diff --git a/app/views/revisions/index.html.erb b/app/views/revisions/index.html.erb index 4680ae2..7ea74a6 100644 --- a/app/views/revisions/index.html.erb +++ b/app/views/revisions/index.html.erb @@ -4,6 +4,20 @@ <%= link_to 'Back to node', node_path(@node) %>

+<% if Page.non_default_locales.any? %> +

+ Locale: + <% ([I18n.default_locale] + Page.non_default_locales).each_with_index do |locale, i| %> + <%= " · ".html_safe if i > 0 %> + <% if locale == @locale %> + <%= locale.to_s.upcase %> + <% else %> + <%= link_to locale.to_s.upcase, node_revisions_path(@node, :locale => locale) %> + <% end %> + <% end %> +

+<% end %> + @@ -20,6 +34,7 @@ - + - +
<%= button_to 'Diff revisions', diff_node_revisions_path(@node), method: :post, + params: { locale: @locale }, form: { id: 'diff_form', class: 'button_to computation' }, disabled: true %> @@ -35,10 +50,10 @@ <%= radio_button_tag :start_revision, page.revision, index == 1 %> <%= radio_button_tag :end_revision, page.revision, index == 0 %> <%= page.revision %><%= page.title %><%= page.translations.find_by(:locale => @locale)&.title || "—" %> <%= page.editor.try(:login) %> <%= page.updated_at %><%= link_to 'show', node_revision_path(@node, page) %><%= link_to 'show', node_revision_path(@node, page, :locale => @locale) %> <%= button_to 'restore', restore_node_revision_path(@node, page), method: :put, diff --git a/app/views/revisions/show.html.erb b/app/views/revisions/show.html.erb index 92d959b..74fb51f 100644 --- a/app/views/revisions/show.html.erb +++ b/app/views/revisions/show.html.erb @@ -1,12 +1,13 @@ +<% translation = @page.translations.find_by(:locale => @locale) %>
-

Revision <%= @page.revision %>: <%= @page.title %>

+

Revision <%= @page.revision %>: <%= translation&.title %> (<%= @locale.to_s.upcase %>)

Actions
- <%= link_to 'Show all revisions', node_revisions_path(@node) %> + <%= link_to 'Show all revisions', node_revisions_path(@node, :locale => @locale) %>
@@ -27,12 +28,12 @@
<%= @page.published_at %>
Title
-
<%= @page.title %>
+
<%= translation&.title %>
Abstract
-
<%= @page.abstract %>
+
<%= translation&.abstract %>
Body
-
<%= raw @page.body %>
+
<%= raw translation&.body %>
diff --git a/config/routes.rb b/config/routes.rb index 826bb18..8e5ff23 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,7 +58,7 @@ Cccms::Application.routes.draw do resources :translations, controller: 'page_translations', param: :translation_locale, constraints: { translation_locale: /en/ }, - only: [:index, :show, :edit, :update, :destroy] do + only: [:show, :edit, :update, :destroy] do member do put :autosave end diff --git a/test/controllers/page_translations_controller_test.rb b/test/controllers/page_translations_controller_test.rb index 8c899fd..8fa732f 100644 --- a/test/controllers/page_translations_controller_test.rb +++ b/test/controllers/page_translations_controller_test.rb @@ -1,18 +1,6 @@ require 'test_helper' class PageTranslationsControllerTest < ActionController::TestCase - test "index lists the default locale's existing translation and flags a missing one" do - login_as :quentin - node = Node.root.children.create!(:slug => "translations_index_test") - node.publish_draft! - - get :index, params: { :node_id => node.id } - - assert_response :success - assert_equal [:en], assigns(:translations).map { |t| t[:locale] } - assert_equal false, assigns(:translations).first[:exists] - end - test "update creates a first-time translation on a fresh draft, leaving head untouched" do login_as :quentin node = Node.root.children.create!(:slug => "translations_create_test") -- cgit v1.3