From 2649a4fc256eef00a37880d7e50ad9ea655b3bd4 Mon Sep 17 00:00:00 2001 From: Pia B Date: Fri, 19 Jun 2026 16:19:02 +0200 Subject: [PATCH] refactor translation chaining to be more generic --- app/helpers/admin/action_logs_helper.rb | 21 ++++++++++++++++--- .../admin/action_logs/_action_log.html.haml | 16 +++++--------- config/locales/en-GB.yml | 8 +------ config/locales/en.yml | 7 +++++++ spec/helpers/admin/action_logs_helper_spec.rb | 18 +++++++++++----- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/app/helpers/admin/action_logs_helper.rb b/app/helpers/admin/action_logs_helper.rb index 54b88b2c4db..9199e6669e4 100644 --- a/app/helpers/admin/action_logs_helper.rb +++ b/app/helpers/admin/action_logs_helper.rb @@ -42,11 +42,26 @@ module Admin::ActionLogsHelper end end - def translation_key(log, key) + def chain_multiple_translations(action_log) + translations = +'' + case action_log.target_type + when 'Tag' + %i(usable trendable listable).each do |key| + fetch_key = permutation_of_key(action_log, key) + next if fetch_key.nil? + + translations.concat(" #{t("admin.trends.tags.#{fetch_key}")};") + end + else + return + end + translations + end + + def permutation_of_key(log, key) return if log.public_send(key).nil? - status = log.public_send(key) ? key : :"not_#{key}" - "#{t("admin.trends.tags.#{status}")};" + log.public_send(key) ? key : :"not_#{key}" end def sorted_action_log_types diff --git a/app/views/admin/action_logs/_action_log.html.haml b/app/views/admin/action_logs/_action_log.html.haml index 7c91288a708..2032a7ace31 100644 --- a/app/views/admin/action_logs/_action_log.html.haml +++ b/app/views/admin/action_logs/_action_log.html.haml @@ -4,17 +4,11 @@ = image_tag action_log.account.avatar.url(:original), alt: '', width: 40, height: 40, class: 'avatar' .log-entry__content .log-entry__title - - if action_log.target_type == 'Tag' - = t "admin.action_logs.actions.#{action_log.action}_#{action_log.target_type.underscore}_html", - name: content_tag(:span, action_log.account.username, class: 'username'), - target: content_tag(:span, log_target(action_log), class: 'target'), - usable: translation_key(action_log, :usable), - listable: translation_key(action_log, :listable), - trendable: translation_key(action_log, :trendable) + = t("admin.action_logs.actions.#{action_log.action}_#{action_log.target_type.underscore}_html", + name: content_tag(:span, action_log.account.username, class: 'username'), + target: content_tag(:span, log_target(action_log), class: 'target')) + - if action_log.recorded_changes.present? + = t(chain_multiple_translations(action_log)) - - else - = t "admin.action_logs.actions.#{action_log.action}_#{action_log.target_type.underscore}_html", - name: content_tag(:span, action_log.account.username, class: 'username'), - target: content_tag(:span, log_target(action_log), class: 'target') .log-entry__timestamp %time.formatted{ datetime: action_log.created_at.iso8601 } diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index b4e7947171d..4287504b547 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -289,11 +289,7 @@ en-GB: enable_relay_html: "%{name} enabled the relay %{target}" enable_sign_in_token_auth_user_html: "%{name} enabled email token authentication for %{target}" enable_user_html: "%{name} enabled login for user %{target}" - listable_tag_html: can be suggested memorialize_account_html: "%{name} turned %{target}'s account into a memoriam page" - not_listable_tag_html: won't be suggested - not_trendable_tag_html: won't appear under trends - not_usable_tag_html: cannot be used promote_user_html: "%{name} promoted user %{target}" publish_terms_of_service_html: "%{name} published updates to the terms of service" reject_appeal_html: "%{name} rejected moderation decision appeal from %{target}" @@ -306,7 +302,6 @@ en-GB: sensitive_account_html: "%{name} marked %{target}'s media as sensitive" silence_account_html: "%{name} limited %{target}'s account" suspend_account_html: "%{name} suspended %{target}'s account" - trendable_tag_html: can appear under trends unassigned_report_html: "%{name} unassigned report %{target}" unblock_email_account_html: "%{name} unblocked %{target}'s email address" unsensitive_account_html: "%{name} unmarked %{target}'s media as sensitive" @@ -319,10 +314,9 @@ en-GB: update_ip_block_html: "%{name} changed rule for IP %{target}" update_report_html: "%{name} updated report %{target}" update_status_html: "%{name} updated post by %{target}" - update_tag_html: "%{name} changed %{target} settings to: %{usable} %{trendable} %{listable}" + update_tag_html: "%{name} changed %{target} settings to: " update_user_role_html: "%{name} changed %{target} role" update_username_block_html: "%{name} updated rule for usernames containing %{target}" - usable_tag_html: can be used; deleted_account: deleted account empty: No logs found. filter_by_action: Filter by action diff --git a/config/locales/en.yml b/config/locales/en.yml index 39ca34dd682..869075d334e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -289,7 +289,11 @@ en: enable_relay_html: "%{name} enabled the relay %{target}" enable_sign_in_token_auth_user_html: "%{name} enabled email token authentication for %{target}" enable_user_html: "%{name} enabled login for user %{target}" + listable_tag_html: can be suggested memorialize_account_html: "%{name} turned %{target}'s account into a memoriam page" + not_listable_tag_html: won't be suggested + not_trendable_tag_html: won't appear under trends + not_usable_tag_html: cannot be used promote_user_html: "%{name} promoted user %{target}" publish_terms_of_service_html: "%{name} published updates to the terms of service" reject_appeal_html: "%{name} rejected moderation decision appeal from %{target}" @@ -302,6 +306,7 @@ en: sensitive_account_html: "%{name} marked %{target}'s media as sensitive" silence_account_html: "%{name} limited %{target}'s account" suspend_account_html: "%{name} suspended %{target}'s account" + trendable_tag_html: can appear under trends unassigned_report_html: "%{name} unassigned report %{target}" unblock_email_account_html: "%{name} unblocked %{target}'s email address" unsensitive_account_html: "%{name} unmarked %{target}'s media as sensitive" @@ -314,8 +319,10 @@ en: update_ip_block_html: "%{name} changed rule for IP %{target}" update_report_html: "%{name} updated report %{target}" update_status_html: "%{name} updated post by %{target}" + update_tag_html: "%{name} changed %{target} settings to: " update_user_role_html: "%{name} changed %{target} role" update_username_block_html: "%{name} updated rule for usernames containing %{target}" + usable_tag_html: can be used; deleted_account: deleted account empty: No logs found. filter_by_action: Filter by action diff --git a/spec/helpers/admin/action_logs_helper_spec.rb b/spec/helpers/admin/action_logs_helper_spec.rb index 1eaecf45303..cab108fc426 100644 --- a/spec/helpers/admin/action_logs_helper_spec.rb +++ b/spec/helpers/admin/action_logs_helper_spec.rb @@ -5,15 +5,23 @@ require 'rails_helper' RSpec.describe Admin::ActionLogsHelper do before { sign_in Fabricate(:admin_user) } - describe '#translation_key' do + describe 'with Tags' do let(:tag) { Fabricate(:tag, name: '#supertag') } let(:account) { Fabricate(:account) } let!(:log) { Fabricate(:action_log, target: tag, account: account, usable: false, listable: true) } - it 'returns translation keys for all different states' do - expect(helper.translation_key(log, :usable)).to eq("#{t('admin.trends.tags.not_usable')};") - expect(helper.translation_key(log, :trendable)).to be_nil - expect(helper.translation_key(log, :listable)).to eq("#{t('admin.trends.tags.listable')};") + describe '#permutation_of_key' do + it 'returns different permutations for all different states' do + expect(helper.permutation_of_key(log, :usable)).to eq(:not_usable) + expect(helper.permutation_of_key(log, :trendable)).to be_nil + expect(helper.permutation_of_key(log, :listable)).to eq(:listable) + end + end + + describe '#chain_multiple_translations' do + it 'returns translation keys for all different states' do + expect(helper.chain_multiple_translations(log)).to eq(' Cannot be used; Can be suggested;') + end end end end