FIX: Use generic, static names for badges

This is necessary, because seeding badges with a translated name will result in duplicate badges whenever the default locale changes. The static name solves this.
Moreover, it's not necessary to set the description during seeding. The serializer will localize the names and descriptions at runtime.

The DB migration tries to rename the existing badges by looking them up by their translated time.
This commit is contained in:
Gerhard Schlager 2022-11-22 00:24:16 +01:00 committed by Gerhard Schlager
parent 747fb0c334
commit d6c8089ca3
3 changed files with 78 additions and 10 deletions

View File

@ -26,10 +26,10 @@ en:
others: "No accepted solutions." others: "No accepted solutions."
badges: badges:
helpdesk: solved_1:
name: "Helpdesk" name: "Helpdesk"
description: "First Accepted Answer on a Topic" description: "First Accepted Answer on a Topic"
tech_support: solved_2:
name: "Tech Support" name: "Tech Support"
description: "10 Accepted answers" description: "10 Accepted answers"

View File

@ -2,7 +2,7 @@
return unless badge_grouping = BadgeGrouping.find_by(name: "Community") return unless badge_grouping = BadgeGrouping.find_by(name: "Community")
helpdesk_query = <<-SQL first_solution_query = <<-SQL
SELECT post_id, user_id, created_at AS granted_at SELECT post_id, user_id, created_at AS granted_at
FROM ( FROM (
SELECT p.id AS post_id, p.user_id, pcf.created_at, SELECT p.id AS post_id, p.user_id, pcf.created_at,
@ -18,12 +18,11 @@ helpdesk_query = <<-SQL
SQL SQL
Badge.seed(:name) do |badge| Badge.seed(:name) do |badge|
badge.name = I18n.t("badges.helpdesk.name") badge.name = "Solved 1"
badge.icon = "check-square" badge.icon = "check-square"
badge.badge_type_id = 3 badge.badge_type_id = 3
badge.badge_grouping = badge_grouping badge.badge_grouping = badge_grouping
badge.description = I18n.t("badges.helpdesk.description") badge.query = first_solution_query
badge.query = helpdesk_query
badge.listable = true badge.listable = true
badge.target_posts = true badge.target_posts = true
badge.enabled = false badge.enabled = false
@ -33,7 +32,7 @@ Badge.seed(:name) do |badge|
badge.system = true badge.system = true
end end
tech_support_query = <<-SQL solved_10_query = <<-SQL
SELECT p.user_id, MAX(pcf.created_at) AS granted_at SELECT p.user_id, MAX(pcf.created_at) AS granted_at
FROM post_custom_fields pcf FROM post_custom_fields pcf
JOIN badge_posts p ON pcf.post_id = p.id JOIN badge_posts p ON pcf.post_id = p.id
@ -46,12 +45,11 @@ tech_support_query = <<-SQL
SQL SQL
Badge.seed(:name) do |badge| Badge.seed(:name) do |badge|
badge.name = I18n.t("badges.tech_support.name") badge.name = "Solved 2"
badge.icon = "check-square" badge.icon = "check-square"
badge.badge_type_id = 2 badge.badge_type_id = 2
badge.badge_grouping = badge_grouping badge.badge_grouping = badge_grouping
badge.description = I18n.t("badges.tech_support.description") badge.query = solved_10_query
badge.query = tech_support_query
badge.listable = true badge.listable = true
badge.allow_title = true badge.allow_title = true
badge.target_posts = false badge.target_posts = false

View File

@ -0,0 +1,70 @@
# frozen_string_literal: true
class RenameBadges < ActiveRecord::Migration[6.1]
HELPDESK_TRANSLATIONS = {
"ar" => "مكتب المساعدة",
"el" => "Γραφείο βοήθειας",
"es" => "Servicio de ayuda",
"fi" => "Neuvonta",
"fr" => "Service d'assistance",
"he" => "דלפק עזרה",
"hu" => "Ügyfélszolgálat",
"ja" => "ヘルプデスク",
"ko" => "안내 데스크",
"pl_PL" => "Dział pomocy technicznej",
"ro" => "Ajutor",
"ru" => "Служба поддержки",
"sl" => "Služba za pomoč",
"sv" => "Kundtjänst",
"tr_TR" => "Yardım masası",
"zh_CN" => "帮助台",
"zh_TW" => "服務台"
}
TECH_SUPPORT_TRANSLATIONS = {
"ar" => "الدعم الفني",
"de" => "Technischer Support",
"el" => "Τεχνική υποστήριξη",
"es" => "Asistencia técnica",
"fi" => "Tukipalvelu",
"fr" => "Assistance technique",
"he" => "תמיכה טכנית",
"hu" => "Műszaki támogatás",
"id" => "Dukungan Teknis",
"it" => "Supporto Tecnico",
"ja" => "技術サポート",
"ko" => "기술 지원",
"nl" => "Technische ondersteuning",
"pl_PL" => "Wsparcie techniczne",
"pt_BR" => "Suporte Técnico",
"ro" => "Asistenţă tehnică",
"ru" => "Техническая поддержка",
"sl" => "Tehnična podpora",
"sv" => "Teknisk support",
"tr_TR" => "Teknik Destek",
"zh_CN" => "技术支持",
"zh_TW" => "技術支援"
}
def up
default_locale = DB.query_single("SELECT value FROM site_settings WHERE name = 'default_locale'").first || "en"
sql = <<~SQL
UPDATE badges
SET name = :new_name,
description = NULL,
long_description = NULL
WHERE name = :old_name
SQL
badge_name = HELPDESK_TRANSLATIONS.fetch(default_locale, "Helpdesk")
DB.exec(sql, old_name: badge_name, new_name: "Solved 1")
badge_name = TECH_SUPPORT_TRANSLATIONS.fetch(default_locale, "Tech Support")
DB.exec(sql, old_name: badge_name, new_name: "Solved 2")
end
def down
raise ActiveRecord::IrreversibleMigration
end
end