Fix repetitive database queries from #30040 (#30259)

pull/2710/head^2
Jason Punyon 2024-05-15 05:38:16 -04:00 committed by GitHub
parent 6beead3867
commit 85c625d319
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 13 deletions

View File

@ -240,11 +240,16 @@ module ApplicationHelper
EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s
end end
def site_icon_path(type, size = '48') def instance_presenter
icon = SiteUpload.find_by(var: type) @instance_presenter ||= InstancePresenter.new
return nil unless icon end
icon.file.url(size) def favicon_path(size = '48')
instance_presenter.favicon&.file&.url(size)
end
def app_icon_path(size = '48')
instance_presenter.app_icon&.file&.url(size)
end end
private private

View File

@ -81,4 +81,16 @@ class InstancePresenter < ActiveModelSerializers::Model
def mascot def mascot
@mascot ||= Rails.cache.fetch('site_uploads/mascot') { SiteUpload.find_by(var: 'mascot') } @mascot ||= Rails.cache.fetch('site_uploads/mascot') { SiteUpload.find_by(var: 'mascot') }
end end
def favicon
return @favicon if defined?(@favicon)
@favicon ||= Rails.cache.fetch('site_uploads/favicon') { SiteUpload.find_by(var: 'favicon') }
end
def app_icon
return @app_icon if defined?(@app_icon)
@app_icon ||= Rails.cache.fetch('site_uploads/app_icon') { SiteUpload.find_by(var: 'app_icon') }
end
end end

View File

@ -27,7 +27,7 @@ class ManifestSerializer < ActiveModel::Serializer
def icons def icons
SiteUpload::ANDROID_ICON_SIZES.map do |size| SiteUpload::ANDROID_ICON_SIZES.map do |size|
src = site_icon_path('app_icon', size.to_i) src = app_icon_path(size.to_i)
src = URI.join(root_url, src).to_s if src.present? src = URI.join(root_url, src).to_s if src.present?
{ {

View File

@ -11,13 +11,13 @@
- if storage_host? - if storage_host?
%link{ rel: 'dns-prefetch', href: storage_host }/ %link{ rel: 'dns-prefetch', href: storage_host }/
%link{ rel: 'icon', href: site_icon_path('favicon', 'ico') || '/favicon.ico', type: 'image/x-icon' }/ %link{ rel: 'icon', href: favicon_path('ico') || '/favicon.ico', type: 'image/x-icon' }/
- SiteUpload::FAVICON_SIZES.each do |size| - SiteUpload::FAVICON_SIZES.each do |size|
%link{ rel: 'icon', sizes: "#{size}x#{size}", href: site_icon_path('favicon', size.to_i) || frontend_asset_path("icons/favicon-#{size}x#{size}.png"), type: 'image/png' }/ %link{ rel: 'icon', sizes: "#{size}x#{size}", href: favicon_path(size.to_i) || frontend_asset_path("icons/favicon-#{size}x#{size}.png"), type: 'image/png' }/
- SiteUpload::APPLE_ICON_SIZES.each do |size| - SiteUpload::APPLE_ICON_SIZES.each do |size|
%link{ rel: 'apple-touch-icon', sizes: "#{size}x#{size}", href: site_icon_path('app_icon', size.to_i) || frontend_asset_path("icons/apple-touch-icon-#{size}x#{size}.png") }/ %link{ rel: 'apple-touch-icon', sizes: "#{size}x#{size}", href: app_icon_path(size.to_i) || frontend_asset_path("icons/apple-touch-icon-#{size}x#{size}.png") }/
%link{ rel: 'mask-icon', href: frontend_asset_path('images/logo-symbol-icon.svg'), color: '#6364FF' }/ %link{ rel: 'mask-icon', href: frontend_asset_path('images/logo-symbol-icon.svg'), color: '#6364FF' }/
%link{ rel: 'manifest', href: manifest_path(format: :json) }/ %link{ rel: 'manifest', href: manifest_path(format: :json) }/

View File

@ -286,26 +286,31 @@ describe ApplicationHelper do
end end
end end
describe '#site_icon_path' do describe 'favicon' do
context 'when an icon exists' do context 'when an icon exists' do
let!(:favicon) { Fabricate(:site_upload, var: 'favicon') } let!(:favicon) { Fabricate(:site_upload, var: 'favicon') }
let!(:app_icon) { Fabricate(:site_upload, var: 'app_icon') }
it 'returns the URL of the icon' do it 'returns the URL of the icon' do
expect(helper.site_icon_path('favicon')).to eq(favicon.file.url('48')) expect(helper.favicon_path).to eq(favicon.file.url('48'))
expect(helper.app_icon_path).to eq(app_icon.file.url('48'))
end end
it 'returns the URL of the icon with size parameter' do it 'returns the URL of the icon with size parameter' do
expect(helper.site_icon_path('favicon', 16)).to eq(favicon.file.url('16')) expect(helper.favicon_path(16)).to eq(favicon.file.url('16'))
expect(helper.app_icon_path(16)).to eq(app_icon.file.url('16'))
end end
end end
context 'when an icon does not exist' do context 'when an icon does not exist' do
it 'returns nil' do it 'returns nil' do
expect(helper.site_icon_path('favicon')).to be_nil expect(helper.favicon_path).to be_nil
expect(helper.app_icon_path).to be_nil
end end
it 'returns nil with size parameter' do it 'returns nil with size parameter' do
expect(helper.site_icon_path('favicon', 16)).to be_nil expect(helper.favicon_path(16)).to be_nil
expect(helper.app_icon_path(16)).to be_nil
end end
end end
end end