Skip to content

A platform for community discussion. Free, open, simple.

UX: fix category badges on 404 page and oneboxes (#34071)

Krised420f93911b
diff --git a/lib/category_badge.rb b/lib/category_badge.rb
index 05ac27a358e..a824f88f242 100644
--- a/lib/category_badge.rb
+++ b/lib/category_badge.rb
@@ -41,7 +41,16 @@ module CategoryBadge
def self.style_for_browser(category, opts)
data = shared_data(category, opts)
- class_names = "badge-category #{data[:parent_category] ? "--has-parent" : ""}"
+ style_class =
+ if category.respond_to?(:style_type) && category.style_type.present?
+ case category.style_type
+ when "icon"
+ "--style-icon"
+ when "emoji"
+ "--style-emoji"
+ end
+ end || "--style-square"
+ class_names = "badge-category #{style_class} #{data[:parent_category] ? "--has-parent" : ""}"
description = category.description_text ? "title='#{category.description_text}'" : ""
badge_styles = {
@@ -57,6 +66,13 @@ module CategoryBadge
result << " style='#{map_styles_to_string(badge_styles)}'"
result << " data-parent-category-id='#{data[:parent_category].id}'" if data[:parent_category]
result << " data-drop-close='true' class='#{class_names}' #{description}>"
+
+ if category.style_type == "icon" && category.icon.present?
+ result << SvgSprite.raw_svg(category.icon)
+ elsif category.style_type == "emoji" && category.emoji.present?
+ result << Emoji.codes_to_img(":#{category.emoji}:")
+ end
+
result << "<span class='badge-category__name'>"
result << ERB::Util.html_escape(category.name)
result << "</span></span>"
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index ef8bb9e1696..f27e8942a59 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -455,6 +455,28 @@ RSpec.describe ApplicationController do
expect(fake_logger.errors.length).to eq(0)
expect(fake_logger.warnings.length).to eq(0)
end
+
+ it "should render category badges with correct style classes on 404 page" do
+ Discourse.cache.delete("page_not_found_topics:#{I18n.locale}")
+
+ square_cat = Fabricate(:category, style_type: :square)
+ icon_cat = Fabricate(:category, style_type: :icon, icon: "user")
+ emoji_cat = Fabricate(:category, style_type: :emoji, emoji: "smile")
+
+ Fabricate(:topic, title: "Square Category Topic", category: square_cat)
+ Fabricate(:topic, title: "Icon Category Topic", category: icon_cat)
+ Fabricate(:topic, title: "Emoji Category Topic", category: emoji_cat)
+
+ get "/t/nope-nope/99999999"
+ expect(response.status).to eq(404)
+
+ expect(response.body).to include("badge-category --style-square")
+ expect(response.body).to include("badge-category --style-icon")
+ expect(response.body).to include("badge-category --style-emoji")
+
+ expect(response.body).to include('<svg id="user"')
+ expect(response.body).to include('class="emoji"')
+ end
end
it "should cache results" do