refactor translation chaining to be more generic
Some checks failed
Chromatic / Check for relevant changes (push) Has been cancelled
Chromatic / Run Chromatic (push) Has been cancelled

This commit is contained in:
Pia B
2026-06-19 16:19:02 +02:00
parent afa894d79a
commit 2649a4fc25
5 changed files with 44 additions and 26 deletions

View File

@@ -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

View File

@@ -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 }

View File

@@ -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

View File

@@ -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

View File

@@ -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