Skip to content

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

FEATURE: Also localize banners (#32908)

Natalie Tayb485e04e4f5e
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 66979849bd8..a2119c430c0 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -1384,10 +1384,15 @@ class Topic < ActiveRecord::Base
Jobs.cancel_scheduled_job(:remove_banner, topic_id: self.id)
end
- def banner
+ def banner(guardian = nil)
post = self.ordered_posts.first
- { html: post.cooked, key: self.id, url: self.url }
+ html = post.cooked
+ if (guardian && ContentLocalization.show_translated_post?(post, guardian))
+ html = post.get_localization&.cooked.presence || html
+ end
+
+ { html:, key: self.id, url: self.url }
end
cattr_accessor :slug_computed_callbacks
diff --git a/lib/application_layout_preloader.rb b/lib/application_layout_preloader.rb
index a4801274413..f9a2a9dab52 100644
--- a/lib/application_layout_preloader.rb
+++ b/lib/application_layout_preloader.rb
@@ -41,7 +41,7 @@ class ApplicationLayoutPreloader
.banner_json_cache
.defer_get_set("json") do
topic = Topic.where(archetype: Archetype.banner).first
- banner = topic.present? ? topic.banner : {}
+ banner = topic.present? ? topic.banner(@guardian) : {}
MultiJson.dump(banner)
end
end
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 1749e8cdb7f..ec8c2da1a4e 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -1,7 +1,7 @@
# encoding: utf-8
# frozen_string_literal: true
-RSpec.describe Topic do
+describe Topic do
let(:now) { Time.zone.local(2013, 11, 20, 8, 0) }
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:user1) { Fabricate(:user, refresh_auto_groups: true) }
@@ -1645,12 +1645,10 @@ RSpec.describe Topic do
end
end
- describe "banner" do
+ describe "banners" do
fab!(:topic)
fab!(:user) { topic.user }
- let(:banner) { { html: "<p>BANNER</p>", url: topic.url, key: topic.id } }
-
- before { topic.stubs(:banner).returns(banner) }
+ fab!(:first_post) { Fabricate(:post, topic: topic, user: topic.user, cooked: "<p>BANNER</p>") }
describe "make_banner!" do
it "changes the topic archetype to 'banner'" do
@@ -1709,6 +1707,22 @@ RSpec.describe Topic do
end
end
end
+
+ describe "#banner" do
+ it "returns the banner hash" do
+ expect(topic.banner).to include(html: "<p>BANNER</p>", key: topic.id, url: topic.url)
+ end
+
+ it "returns a localized banner" do
+ SiteSetting.experimental_content_localization = true
+
+ first_post.update!(locale: "en")
+ I18n.locale = :ja
+ Fabricate(:post_localization, post: first_post, locale: :ja, cooked: "<p>バナー</p>")
+
+ expect(topic.banner(Guardian.new(user))).to include(html: "<p>バナー</p>")
+ end
+ end
end
context "with last_poster info" do
@@ -2604,7 +2618,7 @@ RSpec.describe Topic do
end
end
- describe "#secure_category?" do
+ describe "#read_restricted_category?" do
let(:category) { Category.new }
it "is true if the category is secure" do