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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix displaying and removing store images (#12327)
Łukasz Adamczyk0c71e73d58a9
diff --git a/admin/app/controllers/spree/admin/base_controller.rb b/admin/app/controllers/spree/admin/base_controller.rbindex 08dc0149aa..0c6e0f712a 100644--- a/admin/app/controllers/spree/admin/base_controller.rb+++ b/admin/app/controllers/spree/admin/base_controller.rb@@ -115,6 +115,20 @@ module Spreesession.delete(k)endend++ def remove_assets(attachment_types, object: nil)+ attachment_types.each do |attachment_type|+ remove_param = "remove_#{attachment_type}"+ if params[remove_param] == '1'+ object ||= attachment_type == 'asset' ? @page_section : @object+ attachment = object.public_send(attachment_type)+ if attachment.attached?+ attachment.detach+ attachment.purge_later+ end+ end+ end+ endendendenddiff --git a/admin/app/controllers/spree/admin/resource_controller.rb b/admin/app/controllers/spree/admin/resource_controller.rbindex 0d7fa70f1e..44b8c760f0 100644--- a/admin/app/controllers/spree/admin/resource_controller.rb+++ b/admin/app/controllers/spree/admin/resource_controller.rb@@ -21,17 +21,7 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseControllerinvoke_callbacks(:update, :before)if @object.update(permitted_resource_params)set_current_store- %w[asset image square_image].each do |attachment_type|- remove_param = "remove_#{attachment_type}"- if params[remove_param] == '1'- object = attachment_type == 'asset' ? @page_section : @object- attachment = object.public_send(attachment_type)- if attachment.attached?- attachment.detach- attachment.purge_later- end- end- end+ remove_assets(%w[asset image square_image])invoke_callbacks(:update, :after)respond_with(@object) do |format|diff --git a/admin/app/controllers/spree/admin/storefront_controller.rb b/admin/app/controllers/spree/admin/storefront_controller.rbindex 72e9752d53..d729159143 100644--- a/admin/app/controllers/spree/admin/storefront_controller.rb+++ b/admin/app/controllers/spree/admin/storefront_controller.rb@@ -8,6 +8,7 @@ module Spreedef update@store = current_storeif @store.update(store_params)+ remove_assets(%w[favicon_image social_image], object: @store)flash[:success] = flash_message_for(@store, :successfully_updated)elseflash[:error] = @store.errors.full_messages.to_sentencediff --git a/admin/app/controllers/spree/admin/stores_controller.rb b/admin/app/controllers/spree/admin/stores_controller.rbindex be09c495e6..d5c13ea91c 100644--- a/admin/app/controllers/spree/admin/stores_controller.rb+++ b/admin/app/controllers/spree/admin/stores_controller.rb@@ -34,6 +34,7 @@ module Spree@store.assign_attributes(permitted_store_params)if @store.save+ remove_assets(%w[logo mailer_logo], object: @store)flash[:success] = flash_message_for(@store, :successfully_updated)elseflash[:error] = "#{Spree.t('store_errors.unable_to_update')}: #{@store.errors.full_messages.join(', ')}"diff --git a/admin/app/helpers/spree/admin/stores_helper.rb b/admin/app/helpers/spree/admin/stores_helper.rbindex e7d0f00f67..f813ed5260 100644--- a/admin/app/helpers/spree/admin/stores_helper.rb+++ b/admin/app/helpers/spree/admin/stores_helper.rb@@ -17,10 +17,10 @@ module Spreereturn unless storeRails.cache.fetch(["#{store.cache_key_with_version}/admin_icon", opts.to_param]) do- if store.favicon_image&.attached? && store.favicon_image&.variable?+ if store.logo&.attached? && store.logo&.variable?image_tag(main_app.cdn_image_url(- store.favicon_image.variant(+ store.logo.variant(spree_image_variant_options(resize_to_fill: [opts[:width] * 2, opts[:height] * 2])@@ -30,10 +30,10 @@ module Spreewidth: opts[:width],height: opts[:height])- elsif store.logo&.attached? && store.logo&.variable?+ elsif store.favicon_image&.attached? && store.favicon_image&.variable?image_tag(main_app.cdn_image_url(- store.logo.variant(+ store.favicon_image.variant(spree_image_variant_options(resize_to_fill: [opts[:width] * 2, opts[:height] * 2])diff --git a/admin/spec/controllers/spree/admin/storefront_controller_spec.rb b/admin/spec/controllers/spree/admin/storefront_controller_spec.rbnew file mode 100644index 0000000000..7c3c7810a0--- /dev/null+++ b/admin/spec/controllers/spree/admin/storefront_controller_spec.rb@@ -0,0 +1,63 @@+require 'spec_helper'++describe Spree::Admin::StorefrontController do+ stub_authorization!+ render_views++ let(:store) { Spree::Store.default }+ let!(:uk_country) { create(:country, iso: 'GB', iso3: 'GBR', name: 'United Kingdom') }++ describe 'GET #edit' do+ subject { get :edit, params: { id: store.id } }++ it 'renders the edit template' do+ subject+ expect(response).to render_template(:edit)+ end+ end++ describe 'PUT #update' do+ subject { patch :update, params: { id: store.id, store: store_params, **other_params } }++ let(:store_params) do+ {+ name: 'New Store Name',+ meta_description: 'This is a cool store',+ }+ end++ let(:other_params) { {} }++ it 'updates the store data' do+ subject++ expect(store.reload.name).to eq('New Store Name')+ expect(store.meta_description).to eq('This is a cool store')+ end++ context 'when removing assets' do+ let(:image_path) { Spree::Core::Engine.root.join('spec/fixtures/files/icon_256x256.png') }+ let(:image_file) { Rack::Test::UploadedFile.new(image_path, 'image/png') }++ context 'when removing the favicon' do+ let(:other_params) { { remove_favicon_image: '1' } }++ before { store.favicon_image.attach(image_file) }++ it 'removes the favicon' do+ expect { subject }.to change { store.reload.favicon_image.attached? }.from(true).to(false)+ end+ end++ context 'when removing the social image' do+ let(:other_params) { { remove_social_image: '1' } }++ before { store.social_image.attach(image_file) }++ it 'removes the social image' do+ expect { subject }.to change { store.reload.social_image.attached? }.from(true).to(false)+ end+ end+ end+ end+enddiff --git a/admin/spec/controllers/spree/admin/stores_controller_spec.rb b/admin/spec/controllers/spree/admin/stores_controller_spec.rbindex 331e382953..d07cc2bcc2 100644--- a/admin/spec/controllers/spree/admin/stores_controller_spec.rb+++ b/admin/spec/controllers/spree/admin/stores_controller_spec.rb@@ -7,9 +7,6 @@ describe Spree::Admin::StoresController dolet(:store) { Spree::Store.default }let!(:uk_country) { create(:country, iso: 'GB', iso3: 'GBR', name: 'United Kingdom') }- let(:image_file) { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'fixtures', 'thinking-cat.jpg'), 'image/jpeg') }- let(:store_with_logo) { create(:store, logo: image_file) }-describe 'POST #create' dosubject { post :create, params: { store: store_params }, format: :turbo_stream }@@ -34,7 +31,7 @@ describe Spree::Admin::StoresController doenddescribe 'PUT #update' do- subject { patch :update, params: { id: store.id, store: store_params } }+ subject { patch :update, params: { id: store.id, store: store_params, **other_params } }let(:store_params) do{@@ -44,6 +41,8 @@ describe Spree::Admin::StoresController do}end+ let(:other_params) { {} }+before dostore.update!(checkout_zone: nil)end@@ -55,5 +54,30 @@ describe Spree::Admin::StoresController doexpect(store.default_currency).to eq('GBR')expect(store.default_country).to eq(uk_country)end++ context 'when removing assets' do+ let(:image_path) { Spree::Core::Engine.root.join('spec/fixtures/files/icon_256x256.png') }+ let(:image_file) { Rack::Test::UploadedFile.new(image_path, 'image/png') }++ context 'when removing the logo' do+ let(:other_params) { { remove_logo: '1' } }++ before { store.logo.attach(image_file) }++ it 'removes the logo' do+ expect { subject }.to change { store.reload.logo.attached? }.from(true).to(false)+ end+ end++ context 'when removing the mailer logo' do+ let(:other_params) { { remove_mailer_logo: '1' } }++ before { store.mailer_logo.attach(image_file) }++ it 'removes the mailer logo' do+ expect { subject }.to change { store.reload.mailer_logo.attached? }.from(true).to(false)+ end+ end+ endendenddiff --git a/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rbindex c715e5efea..272da0d2bd 100644--- a/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb@@ -47,7 +47,10 @@ describe Spree::Api::V2::Platform::StoreSerializer dosettings: store.settings,logo: url_helpers.rails_blob_path(logo.attachment),mailer_logo: nil,- favicon_path: nil+ favicon_path: nil,+ storefront_custom_code_body_end: nil,+ storefront_custom_code_body_start: nil,+ storefront_custom_code_head: nil},relationships: {default_country: {diff --git a/core/db/migrate/20250123135358_add_custom_code_fields_to_spree_store.rb b/core/db/migrate/20250123135358_add_custom_code_fields_to_spree_store.rbnew file mode 100644index 0000000000..b56e07539d--- /dev/null+++ b/core/db/migrate/20250123135358_add_custom_code_fields_to_spree_store.rb@@ -0,0 +1,7 @@+class AddCustomCodeFieldsToSpreeStore < ActiveRecord::Migration[6.1]+ def change+ add_column :spree_stores, :storefront_custom_code_head, :text+ add_column :spree_stores, :storefront_custom_code_body_start, :text+ add_column :spree_stores, :storefront_custom_code_body_end, :text+ end+end