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.

VEN-354 Define Store images in separate classes and return them in APIs (#11625)

Aleksandar Petrushev7b4a9d299acf
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.rb
new file mode 100644
index 0000000000..9e8340acfa
--- /dev/null
+++ b/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb
@@ -0,0 +1,35 @@
+module Spree
+ module Api
+ module V2
+ module StoreMediaSerializerImagesConcern
+ extend ActiveSupport::Concern
+
+ included do
+ def self.store_image_url_for(store, attribute_name)
+ attachment = store.send(attribute_name)&.attachment
+ return unless attachment&.attached?
+
+ url_helpers = Rails.application.routes.url_helpers
+ if Spree.public_storage_service_name
+ url_helpers.cdn_image_url(attachment)
+ else
+ url_helpers.rails_blob_path(attachment)
+ end
+ end
+
+ attribute :logo do |store|
+ store_image_url_for store, :logo
+ end
+
+ attribute :mailer_logo do |store|
+ store_image_url_for store, :mailer_logo
+ end
+
+ attribute :favicon_path do |store|
+ store_image_url_for store, :favicon_image
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/store_serializer.rb b/api/app/serializers/spree/api/v2/platform/store_serializer.rb
index bdf5e52a32..cc29ca5fbe 100644
--- a/api/app/serializers/spree/api/v2/platform/store_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/store_serializer.rb
@@ -4,6 +4,7 @@ module Spree
module Platform
class StoreSerializer < BaseSerializer
include ResourceSerializerConcern
+ include StoreMediaSerializerImagesConcern
has_many :menus
has_one :default_country, serializer: :country, record_type: :country, id_method_name: :default_country_id
diff --git a/api/app/serializers/spree/v2/storefront/store_serializer.rb b/api/app/serializers/spree/v2/storefront/store_serializer.rb
index 0b9028fb8e..ed5b16aca1 100644
--- a/api/app/serializers/spree/v2/storefront/store_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/store_serializer.rb
@@ -2,6 +2,8 @@ module Spree
module V2
module Storefront
class StoreSerializer < BaseSerializer
+ include Spree::Api::V2::StoreMediaSerializerImagesConcern
+
set_type :store
attributes :name, :url, :meta_description, :meta_keywords, :seo_title, :default_currency,
@@ -12,13 +14,6 @@ module Spree
has_many :cms_pages
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/serializers/spree/api/v2/platform/store_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
index 5f041117ee..93fcf2dbae 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
@@ -5,6 +5,13 @@ describe Spree::Api::V2::Platform::StoreSerializer do
let!(: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.save
+ store.logo
+ end
+ let(:url_helpers) { Rails.application.routes.url_helpers }
it { expect(subject).to be_kind_of(Hash) }
@@ -39,7 +46,10 @@ describe Spree::Api::V2::Platform::StoreSerializer do
seo_robots: store.seo_robots,
supported_locales: store.supported_locales,
deleted_at: store.deleted_at,
- settings: store.settings
+ settings: store.settings,
+ logo: url_helpers.rails_blob_path(logo.attachment),
+ mailer_logo: nil,
+ favicon_path: nil
},
relationships: {
default_country: {
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 0c0685af48..f925b9de1f 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -18,9 +18,6 @@ module Spree
attr_accessor :skip_validate_not_last
- MAILER_LOGO_CONTENT_TYPES = ['image/png', 'image/jpg', 'image/jpeg'].freeze
- FAVICON_CONTENT_TYPES = ['image/png', 'image/x-icon', 'image/vnd.microsoft.icon'].freeze
-
acts_as_paranoid
has_many :orders, class_name: 'Spree::Order'
@@ -80,28 +77,14 @@ module Spree
default_scope { order(created_at: :asc) }
- if Spree.public_storage_service_name
- has_one_attached :logo, service: Spree.public_storage_service_name
- else
- has_one_attached :logo
- end
- if Spree.public_storage_service_name
- has_one_attached :mailer_logo, service: Spree.public_storage_service_name
- else
- has_one_attached :mailer_logo
- end
- if Spree.public_storage_service_name
- has_one_attached :favicon_image, service: Spree.public_storage_service_name
- else
- has_one_attached :favicon_image
- end
+ 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
- validates :mailer_logo, content_type: MAILER_LOGO_CONTENT_TYPES
- validates :favicon_image, content_type: FAVICON_CONTENT_TYPES,
- dimension: { max: 256..256 },
- aspect_ratio: :square,
- size: { less_than_or_equal_to: 1.megabyte }
+ has_one :favicon_image, class_name: 'Spree::StoreFaviconImage', dependent: :destroy, as: :viewable
+ accepts_nested_attributes_for :favicon_image, reject_if: :all_blank
before_save :ensure_default_exists_and_is_unique
before_save :ensure_supported_currencies, :ensure_supported_locales, :ensure_default_country
@@ -198,9 +181,9 @@ module Spree
end
def favicon
- return unless favicon_image.attached?
+ return unless favicon_image&.attachment&.attached?
- favicon_image.variant(resize_to_limit: [32, 32])
+ favicon_image.attachment.variant(resize_to_limit: [32, 32])
end
def can_be_deleted?
diff --git a/core/app/models/spree/store_favicon_image.rb b/core/app/models/spree/store_favicon_image.rb
new file mode 100644
index 0000000000..f79605ebab
--- /dev/null
+++ b/core/app/models/spree/store_favicon_image.rb
@@ -0,0 +1,17 @@
+module Spree
+ class StoreFaviconImage < Asset
+ if Spree.public_storage_service_name
+ has_one_attached :attachment, service: Spree.public_storage_service_name
+ else
+ has_one_attached :attachment
+ end
+
+ VALID_CONTENT_TYPES = ['image/png', 'image/x-icon', 'image/vnd.microsoft.icon'].freeze
+
+ validates :attachment,
+ content_type: VALID_CONTENT_TYPES,
+ dimension: { max: 256..256 },
+ aspect_ratio: :square,
+ size: { less_than_or_equal_to: 1.megabyte }
+ end
+end
diff --git a/core/app/models/spree/store_logo.rb b/core/app/models/spree/store_logo.rb
new file mode 100644
index 0000000000..9e21104289
--- /dev/null
+++ b/core/app/models/spree/store_logo.rb
@@ -0,0 +1,9 @@
+module Spree
+ class StoreLogo < Asset
+ if Spree.public_storage_service_name
+ has_one_attached :attachment, service: Spree.public_storage_service_name
+ else
+ has_one_attached :attachment
+ end
+ end
+end
diff --git a/core/app/models/spree/store_mailer_logo.rb b/core/app/models/spree/store_mailer_logo.rb
new file mode 100644
index 0000000000..0645ebbdf1
--- /dev/null
+++ b/core/app/models/spree/store_mailer_logo.rb
@@ -0,0 +1,13 @@
+module Spree
+ class StoreMailerLogo < Asset
+ if Spree.public_storage_service_name
+ has_one_attached :attachment, service: Spree.public_storage_service_name
+ else
+ has_one_attached :attachment
+ end
+
+ VALID_CONTENT_TYPES = ['image/png', 'image/jpg', 'image/jpeg'].freeze
+
+ validates :attachment, content_type: VALID_CONTENT_TYPES
+ end
+end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 5c32ed2f35..bb66eaf2b2 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -126,14 +126,15 @@ module Spree
:quantity, :stock_item, :stock_item_id, :originator, :action
]
- @@store_attributes = [:name, :url, :seo_title, :code, :meta_keywords, :logo,
+ @@store_attributes = [:name, :url, :seo_title, :code, :meta_keywords,
:meta_description, :default_currency, :mail_from_address,
:customer_support_email, :facebook, :twitter, :instagram,
:description, :address, :contact_phone, :supported_locales,
:default_locale, :default_country_id, :supported_currencies,
- :new_order_notifications_email, :mailer_logo, :favicon_image,
- :checkout_zone_id, :seo_robots, :digital_asset_authorized_clicks,
- :digital_asset_authorized_days, :limit_digital_download_count, :limit_digital_download_days]
+ :new_order_notifications_email, :checkout_zone_id, :seo_robots,
+ :digital_asset_authorized_clicks, :digital_asset_authorized_days,
+ :limit_digital_download_count, :limit_digital_download_days, :digital_asset_link_expire_time,
+ { mailer_logo_attributes: {}, favicon_image_attributes: {}, logo_attributes: {} }]
@@store_credit_attributes = %i[amount currency category_id memo]
diff --git a/core/lib/spree/testing_support/factories/favicon_image_factory.rb b/core/lib/spree/testing_support/factories/favicon_image_factory.rb
new file mode 100644
index 0000000000..9ba38cba6e
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/favicon_image_factory.rb
@@ -0,0 +1,9 @@
+FactoryBot.define do
+ factory :favicon_image, class: Spree::StoreFaviconImage do
+ transient do
+ filepath { Spree::Core::Engine.root.join('spec', 'fixtures', 'favicon.ico') }
+ end
+
+ attachment { Rack::Test::UploadedFile.new(filepath) }
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/store_factory.rb b/core/lib/spree/testing_support/factories/store_factory.rb
index ce4aaaa66d..7358afe026 100644
--- a/core/lib/spree/testing_support/factories/store_factory.rb
+++ b/core/lib/spree/testing_support/factories/store_factory.rb
@@ -20,7 +20,7 @@ FactoryBot.define do
end
favicon_image do
- Rack::Test::UploadedFile.new(filepath, image_type)
+ create(:favicon_image, attachment: Rack::Test::UploadedFile.new(filepath, image_type))
end
end
end
diff --git a/core/spec/models/spree/store_favicon_image_spec.rb b/core/spec/models/spree/store_favicon_image_spec.rb
new file mode 100644
index 0000000000..a365dc07a2
--- /dev/null
+++ b/core/spec/models/spree/store_favicon_image_spec.rb
@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+describe Spree::StoreFaviconImage, type: :model do
+ describe 'validations' do
+ it 'validates image properties' do
+ expect(build(:favicon_image, filepath: file_fixture('icon_256x256.png'))).to be_valid
+ expect(build(:favicon_image, filepath: file_fixture('icon_512x512.png'))).not_to be_valid
+ expect(build(:favicon_image, filepath: file_fixture('icon_256x256.gif'))).not_to be_valid
+ expect(build(:favicon_image, filepath: file_fixture('img_256x128.png'))).not_to be_valid
+ end
+
+ context 'file size' do
+ let(:favicon_image) do
+ favicon_image = build(:favicon_image)
+ favicon_image.attachment.attach(io: file, filename: 'favicon.png')
+ favicon_image
+ end
+
+ let(:file) { File.open(file_fixture('icon_256x256.png')) }
+
+ before do
+ allow(file).to receive(:size).and_return(size)
+ end
+
+ context 'when size is 1 megabyte' do
+ let(:size) { 1.megabyte }
+
+ it 'is valid' do
+ expect(favicon_image).to be_valid
+ end
+ end
+
+ context 'when size is over 1 megabyte' do
+ let(:size) { 1.megabyte + 1 }
+
+ it 'is invalid' do
+ expect(favicon_image).not_to be_valid
+ end
+ end
+ end
+ end
+end
+
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 468bedea62..6b841ecdf8 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -142,46 +142,6 @@ describe Spree::Store, type: :model do
end
describe 'validations' do
- describe 'favicon image' do
- it 'validates image properties' do
- expect(build(:store, :with_favicon, filepath: file_fixture('icon_256x256.png'))).to be_valid
-
- expect(build(:store, :with_favicon, filepath: file_fixture('icon_512x512.png'))).not_to be_valid
- expect(build(:store, :with_favicon, filepath: file_fixture('icon_256x256.gif'))).not_to be_valid
- expect(build(:store, :with_favicon, filepath: file_fixture('img_256x128.png'))).not_to be_valid
- end
-
- context 'file size' do
- let(:store) do
- store = build(:store)
- store.favicon_image.attach(io: file, filename: 'favicon.png')
- store
- end
-
- let(:file) { File.open(file_fixture('icon_256x256.png')) }
-
- before do
- allow(file).to receive(:size).and_return(size)
- end
-
- context 'when size is 1 megabyte' do
- let(:size) { 1.megabyte }
-
- it 'is valid' do
- expect(store.valid?).to be(true)
- end
- end
-
- context 'when size is over 1 megabyte' do
- let(:size) { 1.megabyte + 1 }
-
- it 'is invalid' do
- expect(store.valid?).to be(false)
- end
- end
- end
- end
-
describe 'code uniqueness' do
context 'selected code was already used in a deleted store' do
let(:store_code) { 'store_code' }
diff --git a/emails/app/helpers/spree/mail_helper.rb b/emails/app/helpers/spree/mail_helper.rb
index d76f200c56..119c7caf8a 100644
--- a/emails/app/helpers/spree/mail_helper.rb
+++ b/emails/app/helpers/spree/mail_helper.rb
@@ -12,13 +12,13 @@ module Spree
end
def store_logo
- @store_logo ||= current_store&.mailer_logo || current_store&.logo
+ @store_logo ||= current_store&.mailer_logo&.attachment || current_store&.logo&.attachment
end
def logo_path
- return main_app.cdn_image_url(store_logo.variant(resize_to_limit: [244, 104])) if store_logo.variable?
+ return main_app.cdn_image_url(store_logo.variant(resize_to_limit: [244, 104])) if store_logo&.variable?
- return main_app.cdn_image_url(store_logo) if store_logo.image?
+ return main_app.cdn_image_url(store_logo) if store_logo&.image?
end
end
end
diff --git a/emails/spec/helpers/spree/mail_helper_spec.rb b/emails/spec/helpers/spree/mail_helper_spec.rb
index 82ff42eb65..a342c5fef2 100644
--- a/emails/spec/helpers/spree/mail_helper_spec.rb
+++ b/emails/spec/helpers/spree/mail_helper_spec.rb
@@ -62,7 +62,8 @@ module Spree
let(:logo_image) { File.open(File.expand_path('../../../app/assets/images/logo/spree_50.png', __dir__)) }
before do
- store.mailer_logo.attach(io: logo_image, filename: 'spree_50.png', content_type: 'image/png')
+ store.build_mailer_logo
+ store.mailer_logo.attachment.attach(io: logo_image, filename: 'spree_50.png', content_type: 'image/png')
@order = create(:order, store: store)
end
@@ -75,7 +76,8 @@ module Spree
let(:logo_image) { File.open(File.expand_path('../../../app/assets/images/noimage/mini.png', __dir__)) }
before do
- store.mailer_logo.attach(io: logo_image, filename: 'mini.png', content_type: 'image/png')
+ store.build_mailer_logo
+ store.mailer_logo.attachment.attach(io: logo_image, filename: 'mini.png', content_type: 'image/png')
end
it 'shows logo attached to current store' do