Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

add favicon_path attribute to V2::Storefront::StoreSerializer

Łukasz Adamczyk78170d8664b9
diff --git a/api/app/serializers/spree/v2/storefront/store_serializer.rb b/api/app/serializers/spree/v2/storefront/store_serializer.rb
index 8bd8683a89..468da22ba2 100644
--- a/api/app/serializers/spree/v2/storefront/store_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/store_serializer.rb
@@ -10,6 +10,13 @@ module Spree
has_many :menus
has_one :default_country, serializer: :country, record_type: :country, id_method_name: :default_country_id
+
+ attribute :favicon_path do |store|
+ if store.favicon_image.attached?
+ url_helpers = Rails.application.routes.url_helpers
+ url_helpers.rails_representation_path(store.favicon, only_path: true)
+ end
+ end
end
end
end
diff --git a/api/spec/requests/spree/api/v2/storefront/stores_spec.rb b/api/spec/requests/spree/api/v2/storefront/stores_spec.rb
index 8c49988d00..d84b77f61f 100644
--- a/api/spec/requests/spree/api/v2/storefront/stores_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/stores_spec.rb
@@ -28,6 +28,20 @@ describe 'Storefront API v2 Stores spec', type: :request do
expect(json_response['data']).to have_relationship(:default_country)
expect(json_response['data']).to have_relationship(:default_country).with_data('id' => store.default_country_id.to_s, 'type' => 'country')
end
+
+ describe 'favicon_path attribute' do
+ context 'with favicon attached' do
+ let!(:store) { create(:store, :with_favicon) }
+
+ it 'returns store favicon path' do
+ expect(json_response.dig(:data, :attributes, :favicon_path)).to end_with('favicon.ico')
+ end
+ end
+
+ context 'with no favicon attached' do
+ it { expect(json_response['data']).to have_attribute(:favicon_path).with_value(nil) }
+ end
+ end
end
context 'with invalid code param' do
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index 5a478cb62d..5152bfce06 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -60,7 +60,7 @@ module Spree
end
end
- def favicon_path
+ def spree_favicon_path
main_app.url_for(current_store.favicon || 'favicon.ico')
end
diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb
index f2ed2915bc..b6c02d2e48 100644
--- a/core/spec/helpers/base_helper_spec.rb
+++ b/core/spec/helpers/base_helper_spec.rb
@@ -327,19 +327,19 @@ describe Spree::BaseHelper, type: :helper do
end
end
- describe '#favicon_path' do
+ describe '#spree_favicon_path' do
context 'when a store has its own favicon' do
let(:current_store) { create(:store, :with_favicon) }
it do
- expect(favicon_path).to end_with('favicon.ico')
- expect(URI.parse(favicon_path).host).to be_present
+ expect(spree_favicon_path).to end_with('favicon.ico')
+ expect(URI.parse(spree_favicon_path).host).to be_present
end
end
context 'when a store has no favicon' do
it do
- expect(favicon_path).to eq('favicon.ico')
+ expect(spree_favicon_path).to eq('favicon.ico')
end
end
end
diff --git a/frontend/app/views/spree/shared/_head.html.erb b/frontend/app/views/spree/shared/_head.html.erb
index b08fa67d5b..5fe3a62629 100644
--- a/frontend/app/views/spree/shared/_head.html.erb
+++ b/frontend/app/views/spree/shared/_head.html.erb
@@ -4,7 +4,7 @@
<%== meta_data_tags %>
<%== meta_robots %>
<%= canonical_tag(current_store.url) %>
-<%= favicon_link_tag(favicon_path) %>
+<%= favicon_link_tag(spree_favicon_path) %>
<%= stylesheet_link_tag 'spree/frontend/all', media: 'screen', 'data-turbolinks-track': 'reload' %>
<%= csrf_meta_tags %>
<%= render 'spree/shared/paths' %>