From 41460804bf9575059c98bc17ba1a407b26b3493e Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Sun, 19 May 2024 21:38:30 +0200 Subject: [PATCH] move icon helpers to InstanceHelper --- app/helpers/application_helper.rb | 12 --------- app/helpers/instance_helper.rb | 12 +++++++++ app/serializers/manifest_serializer.rb | 2 +- app/serializers/rest/instance_serializer.rb | 1 - spec/helpers/application_helper_spec.rb | 29 --------------------- spec/helpers/instance_helper_spec.rb | 29 +++++++++++++++++++++ 6 files changed, 42 insertions(+), 43 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7563ae6105f89..0aa27f3bd4a12 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -244,18 +244,6 @@ def mascot_url full_asset_url(instance_presenter.mascot&.file&.url || frontend_asset_path('images/elephant_ui_plane.svg')) end - def instance_presenter - @instance_presenter ||= InstancePresenter.new - end - - 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 - private def storage_host_var diff --git a/app/helpers/instance_helper.rb b/app/helpers/instance_helper.rb index 893afdd51ff10..63f351caeb665 100644 --- a/app/helpers/instance_helper.rb +++ b/app/helpers/instance_helper.rb @@ -13,6 +13,18 @@ def description_for_sign_up(invite = nil) safe_join([description_prefix(invite), I18n.t('auth.description.suffix')], ' ') end + def instance_presenter + @instance_presenter ||= InstancePresenter.new + end + + 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 + private def description_prefix(invite) diff --git a/app/serializers/manifest_serializer.rb b/app/serializers/manifest_serializer.rb index a39fb5ef54076..cf0164c24a34b 100644 --- a/app/serializers/manifest_serializer.rb +++ b/app/serializers/manifest_serializer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ManifestSerializer < ActiveModel::Serializer - include ApplicationHelper + include InstanceHelper include RoutingHelper include ActionView::Helpers::TextHelper diff --git a/app/serializers/rest/instance_serializer.rb b/app/serializers/rest/instance_serializer.rb index 31d53035f567b..2c53e104a534f 100644 --- a/app/serializers/rest/instance_serializer.rb +++ b/app/serializers/rest/instance_serializer.rb @@ -8,7 +8,6 @@ class ContactSerializer < ActiveModel::Serializer end include InstanceHelper - include ApplicationHelper include RoutingHelper attributes :domain, :title, :version, :source_url, :description, diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 56974513bef16..9330eb0dae110 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -285,33 +285,4 @@ def current_theme = 'default' end end end - - describe 'favicon' do - context 'when an icon exists' do - let!(:favicon) { Fabricate(:site_upload, var: 'favicon') } - let!(:app_icon) { Fabricate(:site_upload, var: 'app_icon') } - - it 'returns the URL of the icon' do - expect(helper.favicon_path).to eq(favicon.file.url('48')) - expect(helper.app_icon_path).to eq(app_icon.file.url('48')) - end - - it 'returns the URL of the icon with size parameter' do - 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 - - context 'when an icon does not exist' do - it 'returns nil' do - expect(helper.favicon_path).to be_nil - expect(helper.app_icon_path).to be_nil - end - - it 'returns nil with size parameter' do - expect(helper.favicon_path(16)).to be_nil - expect(helper.app_icon_path(16)).to be_nil - end - end - end end diff --git a/spec/helpers/instance_helper_spec.rb b/spec/helpers/instance_helper_spec.rb index 7e39b7cb3d1ea..9a2d8841586e3 100644 --- a/spec/helpers/instance_helper_spec.rb +++ b/spec/helpers/instance_helper_spec.rb @@ -24,4 +24,33 @@ expect(helper.site_hostname).to eq 'example.com' end end + + describe 'favicon' do + context 'when an icon exists' do + let!(:favicon) { Fabricate(:site_upload, var: 'favicon') } + let!(:app_icon) { Fabricate(:site_upload, var: 'app_icon') } + + it 'returns the URL of the icon' do + expect(helper.favicon_path).to eq(favicon.file.url('48')) + expect(helper.app_icon_path).to eq(app_icon.file.url('48')) + end + + it 'returns the URL of the icon with size parameter' do + 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 + + context 'when an icon does not exist' do + it 'returns nil' do + expect(helper.favicon_path).to be_nil + expect(helper.app_icon_path).to be_nil + end + + it 'returns nil with size parameter' do + expect(helper.favicon_path(16)).to be_nil + expect(helper.app_icon_path(16)).to be_nil + end + end + end end