0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/app/services/sidebar_section_updater.rb
Natalie Tay 93e0ba7985
FIX: Allow manual Community sidebar links to be localized (#41898)
Follow up to https://github.com/discourse/discourse/pull/41795

This adds content-localization support for manually created links in the
built-in Community sidebar section.

Built-in Community links like Topics, My posts, Review, Admin, etc.
still use their existing frontend i18n labels and do not expose
localization controls. Only admin-created links in the Community section
can be localized (since only the admin can edit it).

The earlier PR had skipped adding these entirely for the Community
section
2026-07-22 15:57:56 +08:00

63 lines
1.8 KiB
Ruby
Vendored

# frozen_string_literal: true
class SidebarSectionUpdater
def self.update!(sidebar_section:, user:, section_params:, links_params:)
new(sidebar_section:, user:, section_params:, links_params:).update!
end
def initialize(sidebar_section:, user:, section_params:, links_params:)
@sidebar_section = sidebar_section
@user = user
@section_params = section_params
@links_params = (links_params || []).map { |link| link.to_h.with_indifferent_access }
end
def update!
ActiveRecord::Base.transaction do
@sidebar_section.update!(@section_params.merge(sidebar_urls_attributes: @links_params))
@sidebar_section.sidebar_section_links.update_all(user_id: @sidebar_section.user_id)
update_link_order
end
publish_public_update if @sidebar_section.public?
@sidebar_section
end
private
def update_link_order
order =
@sidebar_section
.sidebar_urls
.sort_by do |url|
@links_params.index { |link| link[:name] == url.name && link[:value] == url.value } || -1
end
.each_with_index
.map { |url, index| [url.id, index] }
.to_h
set_order(order)
end
def set_order(order)
position_generator =
(0..@sidebar_section.sidebar_section_links.count * 2).excluding(
@sidebar_section.sidebar_section_links.map(&:position),
).each
links =
@sidebar_section
.sidebar_section_links
.sort_by { |link| order[link.linkable_id] }
.map { |link| link.attributes.merge(position: position_generator.next) }
@sidebar_section.sidebar_section_links.upsert_all(links, update_only: [:position])
end
def publish_public_update
StaffActionLogger.new(@user).log_update_public_sidebar_section(@sidebar_section)
MessageBus.publish("/refresh-sidebar-sections", nil)
Site.clear_anon_cache!
end
end