Skip to content

FEATURE: Also localize banners #32908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1384,10 +1384,15 @@ def remove_banner!(user)
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
Comment on lines +1390 to +1393
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
html = post.cooked
if (guardian && ContentLocalization.show_translated_post?(post, guardian))
html = post.get_localization&.cooked.presence || html
end
html =
if (guardian && ContentLocalization.show_translated_post?(post, guardian))
post.get_localization&.cooked.presence
end
html ||= post.cooked

Little bit of code golf.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I actually fiddled with this section quite a bit, but I found that the original one I wrote is more readable (html is post.cooked by default).

I will leave it as-is unless you think there is some perf issue.


{ html:, key: self.id, url: self.url }
end

cattr_accessor :slug_computed_callbacks
Expand Down
2 changes: 1 addition & 1 deletion lib/application_layout_preloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def banner_json
.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
Expand Down
26 changes: 20 additions & 6 deletions spec/models/topic_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down Expand Up @@ -1645,12 +1645,10 @@ def set_state!(group, user, state)
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
Expand Down Expand Up @@ -1709,6 +1707,22 @@ def set_state!(group, user, state)
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
Expand Down Expand Up @@ -2604,7 +2618,7 @@ def set_state!(group, user, state)
end
end

describe "#secure_category?" do
describe "#read_restricted_category?" do
let(:category) { Category.new }

it "is true if the category is secure" do
Expand Down
Loading