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
Use Active Storage for Store favicon and (#12306)
Damian Legawiecc979ff846655
diff --git a/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb b/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rbindex 9e8340acfa..3c36c3f346 100644--- a/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb+++ b/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb@@ -6,8 +6,8 @@ module Spreeincluded dodef self.store_image_url_for(store, attribute_name)- attachment = store.send(attribute_name)&.attachment- return unless attachment&.attached?+ attachment = store.send(attribute_name)+ return unless attachment.attached?url_helpers = Rails.application.routes.url_helpersif Spree.public_storage_service_namediff --git a/api/spec/requests/spree/api/v2/storefront/stores_spec.rb b/api/spec/requests/spree/api/v2/storefront/stores_spec.rbindex bc6653cad7..bbdc162afa 100644--- a/api/spec/requests/spree/api/v2/storefront/stores_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/stores_spec.rb@@ -34,13 +34,9 @@ describe 'Storefront API v2 Stores spec', type: :request dolet!(:store) { create(:store, :with_favicon) }it 'returns store favicon path' do- expect(json_response.dig(:data, :attributes, :favicon_path)).to end_with('favicon.ico')+ expect(json_response.dig(:data, :attributes, :favicon_path)).to end_with('thinking-cat.jpg')endend-- context 'with no favicon attached' do- it { expect(json_response['data']).to have_attribute(:favicon_path).with_value(nil) }- 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 62491f144e..794a8ff56c 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@@ -6,8 +6,7 @@ describe Spree::Api::V2::Platform::StoreSerializer dolet!(:store) { Spree::Store.default }let!(:menus) { [create(:menu, store: store), create(:menu, location: 'Footer', store: store)] }let!(:logo) do- store.build_logo- store.logo.attachment.attach(io: File.new(Spree::Core::Engine.root + 'spec/fixtures' + 'thinking-cat.jpg'), filename: 'thinking-cat.jpg')+ store.logo.attach(io: File.new(Spree::Core::Engine.root + 'spec/fixtures' + 'thinking-cat.jpg'), filename: 'thinking-cat.jpg')store.savestore.logoenddiff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rbindex 279dd5c3bd..439b249d50 100644--- a/core/app/helpers/spree/base_helper.rb+++ b/core/app/helpers/spree/base_helper.rb@@ -54,27 +54,6 @@ module Spreeendend- def logo(image_path = nil, options = {})- Spree::Deprecation.warn(<<-DEPRECATION, caller)- `BaseHelper#logo` is deprecated and will be removed in Spree 5.0.- Please use `FrontendHelper#logo` instead- DEPRECATION-- image_path ||= if current_store.logo.attached? && current_store.logo.variable?- main_app.cdn_image_url(current_store.logo.variant(resize_to_limit: [244, 104]))- elsif current_store.logo.attached? && current_store.logo.image?- main_app.cdn_image_url(current_store.logo)- else- 'logo/spree_50.png'- end-- path = spree.respond_to?(:root_path) ? spree.root_path : main_app.root_path-- link_to path, 'aria-label': current_store.name, method: options[:method] do- image_tag image_path, alt: current_store.name, title: current_store.name- end- end-def spree_favicon_pathif current_store.favicon.present?main_app.cdn_image_url(current_store.favicon)diff --git a/core/app/helpers/spree/mail_helper.rb b/core/app/helpers/spree/mail_helper.rbindex 119c7caf8a..76dc294ed9 100644--- a/core/app/helpers/spree/mail_helper.rb+++ b/core/app/helpers/spree/mail_helper.rb@@ -12,7 +12,7 @@ module Spreeenddef store_logo- @store_logo ||= current_store&.mailer_logo&.attachment || current_store&.logo&.attachment+ @store_logo ||= current_store&.mailer_logo || current_store&.logoenddef logo_pathdiff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex ff28c92ab1..3c40718445 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -52,9 +52,9 @@ module Spreehas_many :store_payment_methods, class_name: 'Spree::StorePaymentMethod'has_many :payment_methods, through: :store_payment_methods, class_name: 'Spree::PaymentMethod'+ # FIXME: remove old CMS codehas_many :cms_pages, class_name: 'Spree::CmsPage'has_many :cms_sections, through: :cms_pages, class_name: 'Spree::CmsSection'-has_many :menus, class_name: 'Spree::Menu'has_many :menu_items, through: :menus, class_name: 'Spree::MenuItem'@@ -109,7 +109,6 @@ module Spreevalidates :preferred_digital_asset_authorized_days, numericality: { only_integer: true, greater_than: 0 }validates :code, uniqueness: { case_sensitive: false, conditions: -> { with_deleted } }validates :mail_from_address, email: { allow_blank: false }-# FIXME: we should remove this condition in v5if !ENV['SPREE_DISABLE_DB_CONNECTION'] &&connected? &&@@ -117,17 +116,15 @@ module Spreeconnection.column_exists?(:spree_stores, :new_order_notifications_email)validates :new_order_notifications_email, email: { allow_blank: true }end+ validates :favicon_image, :social_image, :mailer_logo, content_type: Rails.application.config.active_storage.web_image_content_types- default_scope { order(created_at: :asc) }-- has_one :logo, class_name: 'Spree::StoreLogo', dependent: :destroy, as: :viewable- accepts_nested_attributes_for :logo, reject_if: :all_blank-- has_one :mailer_logo, class_name: 'Spree::StoreMailerLogo', dependent: :destroy, as: :viewable- accepts_nested_attributes_for :mailer_logo, reject_if: :all_blank-- has_one :favicon_image, class_name: 'Spree::StoreFaviconImage', dependent: :destroy, as: :viewable- accepts_nested_attributes_for :favicon_image, reject_if: :all_blank+ #+ # Attachments+ #+ has_one_attached :logo, service: Spree.public_storage_service_name+ has_one_attached :favicon_image, service: Spree.public_storage_service_name+ has_one_attached :social_image, service: Spree.public_storage_service_name+ has_one_attached :mailer_logo, service: Spree.public_storage_service_name## Callbacks@@ -258,9 +255,9 @@ module Spreeenddef favicon- return unless favicon_image&.attachment&.attached?+ return unless favicon_image.attached? && favicon_image.variable?- favicon_image.attachment.variant(resize_to_limit: [32, 32])+ favicon_image.variant(resize_to_limit: [32, 32])enddef can_be_deleted?diff --git a/core/config/initializers/active_storage.rb b/core/config/initializers/active_storage.rbindex 892e53f8aa..a66165c0ba 100644--- a/core/config/initializers/active_storage.rb+++ b/core/config/initializers/active_storage.rb@@ -1 +1,4 @@Rails.application.config.active_storage.content_types_to_serve_as_binary.delete('image/svg+xml')++# https://guides.rubyonrails.org/configuring.html#config-active-storage-web-image-content-types+Rails.application.config.active_storage.web_image_content_types << 'image/webp'diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rbindex 51f8a75a46..40d81896c0 100644--- a/core/lib/spree/permitted_attributes.rb+++ b/core/lib/spree/permitted_attributes.rb@@ -140,7 +140,9 @@ module Spree:preferred_digital_asset_authorized_clicks, :preferred_digital_asset_authorized_days,:preferred_limit_digital_download_count, :preferred_limit_digital_download_days,:preferred_digital_asset_link_expire_time,- { mailer_logo_attributes: {}, favicon_image_attributes: {}, logo_attributes: {} }]+ :mailer_logo, :import_products_from_store_id, :import_payment_methods_from_store_id,+ :checkout_message, :customer_terms_of_service, :customer_privacy_policy,+ :customer_returns_policy, :customer_shipping_policy]@@store_credit_attributes = %i[amount currency category_id memo]diff --git a/core/lib/spree/testing_support/factories/store_factory.rb b/core/lib/spree/testing_support/factories/store_factory.rbindex e19dfb097e..cac1bfb3a2 100644--- a/core/lib/spree/testing_support/factories/store_factory.rb+++ b/core/lib/spree/testing_support/factories/store_factory.rb@@ -15,13 +15,12 @@ FactoryBot.define dometa_description { 'Sample store description.' }trait :with_favicon do- transient do- filepath { Spree::Core::Engine.root.join('spec', 'fixtures', 'favicon.ico') }- image_type { 'image/x-icon' }- end-- favicon_image do- create(:favicon_image, attachment: Rack::Test::UploadedFile.new(filepath, image_type))+ after(:build) do |store|+ store.favicon_image.attach(+ io: File.open(Spree::Core::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')),+ filename: 'thinking-cat.jpg',+ content_type: 'image/jpeg'+ )endendenddiff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rbindex c43680ae90..e43fabcdb4 100644--- a/core/spec/helpers/base_helper_spec.rb+++ b/core/spec/helpers/base_helper_spec.rb@@ -397,7 +397,7 @@ describe Spree::BaseHelper, type: :helper dolet(:current_store) { create(:store, :with_favicon) }it do- expect(spree_favicon_path).to end_with('favicon.ico')+ expect(spree_favicon_path).to end_with('thinking-cat.jpg')expect(URI.parse(spree_favicon_path).host).to be_presentendenddiff --git a/emails/spec/helpers/spree/mail_helper_spec.rb b/emails/spec/helpers/spree/mail_helper_spec.rbindex 59dfa47424..4bcc455b78 100644--- a/emails/spec/helpers/spree/mail_helper_spec.rb+++ b/emails/spec/helpers/spree/mail_helper_spec.rb@@ -62,14 +62,13 @@ module Spreelet(:logo_image) { File.open(File.expand_path('../../../app/assets/images/logo/spree_50.png', __dir__)) }before do- store.build_mailer_logo- store.mailer_logo.attachment.attach(io: logo_image, filename: 'spree_50.png', content_type: 'image/png')- store.mailer_logo.attachment.save!+ store.mailer_logo.attach(io: logo_image, filename: 'spree_50.png', content_type: 'image/png')+ store.save!@order = create(:order, store: store)endit 'shows logo attached to orders store' do- expect(subject).to include(store.mailer_logo.attachment.filename.to_s)+ expect(subject).to include(store.mailer_logo.filename.to_s)endend@@ -77,13 +76,12 @@ module Spreelet(:logo_image) { File.open(File.expand_path('../../../app/assets/images/noimage/mini.png', __dir__)) }before do- store.build_mailer_logo- store.mailer_logo.attachment.attach(io: logo_image, filename: 'mini.png', content_type: 'image/png')- store.mailer_logo.attachment.save!+ store.mailer_logo.attach(io: logo_image, filename: 'mini.png', content_type: 'image/png')+ store.save!endit 'shows logo attached to current store' do- expect(subject).to include(store.mailer_logo.attachment.filename.to_s)+ expect(subject).to include(store.mailer_logo.filename.to_s)endendend