discourse/app/serializers/admin_plugin_serializer.rb
Martin Brennan 52a50f1028
PERF: Admin plugin preload settings routes (#31215)
Followup 503f9b6f02ac5c4918d41611848c886b8755e5a0

This previous commit introduced an autogenerated
settings route for every plugin with more than one
setting defined. Plugins with only one setting
only have enabled_site_settings defined, which are
handled using the toggle in the admin plugin list,
so we don't need a dedicated setting page for them.

However in production this introduced a performance
issue, since we were looking through SiteSetting.all_settings
for every plugin, which could be quite slow in some
cases especially on our hosting.

Instead, we already have all the plugin settings cached
inside `SiteSetting.plugins`. We can instead use this to
count how many settings the plugin has, then if there is > 1
for a plugin we use the settings route. This is a much faster lookup
than
searching through SiteSetting.all_settings.
2025-02-07 11:23:43 +10:00

117 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class AdminPluginSerializer < ApplicationSerializer
attributes :id,
:name,
:about,
:version,
:url,
:admin_route,
:enabled,
:enabled_setting,
:has_settings,
:has_only_enabled_setting,
:humanized_name,
:is_official,
:is_discourse_owned,
:label,
:commit_hash,
:commit_url,
:meta_url,
:authors
def id
object.directory_name
end
def name
object.metadata.name
end
def humanized_name
object.humanized_name
end
def about
object.metadata.about
end
def version
object.metadata.version
end
def url
object.metadata.url
end
def authors
object.metadata.authors
end
def enabled
object.enabled?
end
def include_enabled_setting?
enabled_setting.present?
end
def enabled_setting
object.enabled_site_setting
end
def plugin_settings
object.plugin_settings
end
def has_settings
object.any_settings?
end
def has_only_enabled_setting
object.has_only_enabled_setting?
end
def include_url?
url.present?
end
def admin_route
object.full_admin_route
end
def include_admin_route?
admin_route.present?
end
def is_official
Plugin::Metadata::OFFICIAL_PLUGINS.include?(object.name)
end
def include_label?
is_discourse_owned
end
def label
return if !is_discourse_owned
object.metadata.label
end
def is_discourse_owned
object.discourse_owned?
end
def commit_hash
object.commit_hash
end
def commit_url
object.commit_url
end
def meta_url
return if object.metadata.meta_topic_id.blank?
"https://meta.discourse.org/t/#{object.metadata.meta_topic_id}"
end
end