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-355 Separate model for Cms Section images (#11627)

Aleksandar Petrushev0f7ea6811117
diff --git a/api/app/serializers/spree/api/v2/platform/cms_section_image_one_serializer.rb b/api/app/serializers/spree/api/v2/platform/cms_section_image_one_serializer.rb
new file mode 100644
index 0000000000..7e15fe97be
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/cms_section_image_one_serializer.rb
@@ -0,0 +1,9 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class CmsSectionImageOneSerializer < AssetSerializer; end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/cms_section_image_three_serializer.rb b/api/app/serializers/spree/api/v2/platform/cms_section_image_three_serializer.rb
new file mode 100644
index 0000000000..bf2d2bb904
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/cms_section_image_three_serializer.rb
@@ -0,0 +1,9 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class CmsSectionImageThreeSerializer < AssetSerializer; end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/cms_section_image_two_serializer.rb b/api/app/serializers/spree/api/v2/platform/cms_section_image_two_serializer.rb
new file mode 100644
index 0000000000..3fec349251
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/cms_section_image_two_serializer.rb
@@ -0,0 +1,9 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class CmsSectionImageTwoSerializer < AssetSerializer; end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/hero_image_serializer.rb b/api/app/serializers/spree/api/v2/platform/hero_image_serializer.rb
new file mode 100644
index 0000000000..fe876d0ccb
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/hero_image_serializer.rb
@@ -0,0 +1,10 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class HeroImageSerializer < CmsSectionSerializer
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/image_gallery_serializer.rb b/api/app/serializers/spree/api/v2/platform/image_gallery_serializer.rb
new file mode 100644
index 0000000000..7e78532d71
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/image_gallery_serializer.rb
@@ -0,0 +1,10 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class ImageGallerySerializer < CmsSectionSerializer
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/side_by_side_image_serializer.rb b/api/app/serializers/spree/api/v2/platform/side_by_side_image_serializer.rb
new file mode 100644
index 0000000000..fb9d194504
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/side_by_side_image_serializer.rb
@@ -0,0 +1,10 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class SideBySideImageSerializer < CmsSectionSerializer
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb b/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb
index c87a80788f..84a413d213 100644
--- a/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb
@@ -6,10 +6,10 @@ module Spree
attributes :name, :content, :settings, :link, :fit, :type, :position
- Spree::CmsSection::IMAGE_COUNT.each do |count|
- Spree::CmsSection::IMAGE_SIZE.each do |size|
+ Spree::CmsSectionImage::IMAGE_COUNT.each do |count|
+ Spree::CmsSectionImage::IMAGE_SIZE.each do |size|
attribute "img_#{count}_#{size}".to_sym do |section|
- if section.send("image_#{count}").attached? && section.send("img_#{count}_#{size}").present?
+ if section.send("image_#{count}")&.attachment&.attached? && section.send("img_#{count}_#{size}").present?
url_helpers = Rails.application.routes.url_helpers
url_helpers.rails_representation_path(section.send("img_#{count}_#{size}"), only_path: true)
end
diff --git a/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb
index 7e6d6aa491..768eb8a233 100644
--- a/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb
@@ -52,7 +52,8 @@ describe Spree::V2::Storefront::CmsSectionSerializer do
let!(:homepage) { create(:cms_homepage, store: store) }
let!(:cms_section) do
section = create(:cms_hero_image_section, cms_page: homepage)
- section.image_one.attach(io: file, filename: 't-shirt.png')
+ section.build_image_one
+ section.image_one.attachment.attach(io: file, filename: 't-shirt.png')
section
end
let(:file) { File.open(file_fixture('icon_256x256.jpg')) }
@@ -72,7 +73,8 @@ describe Spree::V2::Storefront::CmsSectionSerializer do
let!(:homepage) { create(:cms_homepage, store: store) }
let!(:cms_section) do
section = create(:cms_side_by_side_images_section, cms_page: homepage)
- section.image_one.attach(io: file, filename: 't-shirt.png')
+ section.build_image_one
+ section.image_one.attachment.attach(io: file, filename: 't-shirt.png')
section
end
let(:file) { File.open(file_fixture('icon_256x256.jpg')) }
@@ -85,7 +87,8 @@ describe Spree::V2::Storefront::CmsSectionSerializer do
let!(:homepage) { create(:cms_homepage, store: store) }
let!(:cms_section) do
section = create(:cms_image_gallery_section, cms_page: homepage)
- section.image_one.attach(io: file, filename: 't-shirt.png')
+ section.build_image_one
+ section.image_one.attachment.attach(io: file, filename: 't-shirt.png')
section
end
let(:file) { File.open(file_fixture('icon_256x256.jpg')) }
diff --git a/core/app/models/spree/cms_section.rb b/core/app/models/spree/cms_section.rb
index 553eda44e6..ed359d7087 100644
--- a/core/app/models/spree/cms_section.rb
+++ b/core/app/models/spree/cms_section.rb
@@ -7,22 +7,22 @@ module Spree
validate :reset_link_attributes
- IMAGE_COUNT = ['one', 'two', 'three']
- IMAGE_TYPES = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'].freeze
- IMAGE_SIZE = ['sm', 'md', 'lg', 'xl']
-
- IMAGE_COUNT.each do |count|
- if Spree.public_storage_service_name
- send(:has_one_attached, "image_#{count}".to_sym, service: Spree.public_storage_service_name)
- else
- send(:has_one_attached, "image_#{count}".to_sym)
- end
+ has_one :image_one, class_name: 'Spree::CmsSectionImageOne', dependent: :destroy, as: :viewable
+ accepts_nested_attributes_for :image_one, reject_if: :all_blank
+
+ has_one :image_two, class_name: 'Spree::CmsSectionImageTwo', dependent: :destroy, as: :viewable
+ accepts_nested_attributes_for :image_two, reject_if: :all_blank
- IMAGE_SIZE.each do |size|
+ has_one :image_three, class_name: 'Spree::CmsSectionImageThree', dependent: :destroy, as: :viewable
+ accepts_nested_attributes_for :image_three, reject_if: :all_blank
+
+ Spree::CmsSectionImage::IMAGE_COUNT.each do |count|
+ Spree::CmsSectionImage::IMAGE_SIZE.each do |size|
define_method("img_#{count}_#{size}") do |dimensions = nil|
- return if !send("image_#{count}").attached? || dimensions.nil?
+ image = send("image_#{count}")&.attachment
+ return if !image&.attached? || dimensions.nil?
- send("image_#{count}").variant(resize_to_limit: dimensions.split('x').map(&:to_i))
+ image.variant(resize_to_limit: dimensions.split('x').map(&:to_i))
end
end
end
@@ -31,8 +31,6 @@ module Spree
validates :name, :cms_page, :type, presence: true
- validates :image_one, :image_two, :image_three, content_type: IMAGE_TYPES
-
LINKED_RESOURCE_TYPE = []
TYPES = ['Spree::Cms::Sections::HeroImage',
diff --git a/core/app/models/spree/cms_section_image.rb b/core/app/models/spree/cms_section_image.rb
new file mode 100644
index 0000000000..9668b10160
--- /dev/null
+++ b/core/app/models/spree/cms_section_image.rb
@@ -0,0 +1,15 @@
+module Spree
+ class CmsSectionImage < Asset
+ if Spree.public_storage_service_name
+ has_one_attached :attachment, service: Spree.public_storage_service_name
+ else
+ has_one_attached :attachment
+ end
+
+ IMAGE_COUNT = ['one', 'two', 'three']
+ IMAGE_TYPES = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'].freeze
+ IMAGE_SIZE = ['sm', 'md', 'lg', 'xl']
+
+ validates :attachment, attached: true, content_type: IMAGE_TYPES
+ end
+end
diff --git a/core/app/models/spree/cms_section_image_one.rb b/core/app/models/spree/cms_section_image_one.rb
new file mode 100644
index 0000000000..1c6250257d
--- /dev/null
+++ b/core/app/models/spree/cms_section_image_one.rb
@@ -0,0 +1,4 @@
+module Spree
+ class CmsSectionImageOne < CmsSectionImage
+ end
+end
diff --git a/core/app/models/spree/cms_section_image_three.rb b/core/app/models/spree/cms_section_image_three.rb
new file mode 100644
index 0000000000..e2f68a1e20
--- /dev/null
+++ b/core/app/models/spree/cms_section_image_three.rb
@@ -0,0 +1,4 @@
+module Spree
+ class CmsSectionImageThree < CmsSectionImage
+ end
+end
diff --git a/core/app/models/spree/cms_section_image_two.rb b/core/app/models/spree/cms_section_image_two.rb
new file mode 100644
index 0000000000..ae2bf16f82
--- /dev/null
+++ b/core/app/models/spree/cms_section_image_two.rb
@@ -0,0 +1,4 @@
+module Spree
+ class CmsSectionImageTwo < CmsSectionImage
+ end
+end
diff --git a/core/spec/models/spree/cms_section_spec.rb b/core/spec/models/spree/cms_section_spec.rb
index bbcbcf9d0f..f574c1282f 100644
--- a/core/spec/models/spree/cms_section_spec.rb
+++ b/core/spec/models/spree/cms_section_spec.rb
@@ -18,14 +18,19 @@ describe Spree::CmsSection, type: :model do
let(:section) do
section = build(:cms_hero_image_section, cms_page: homepage)
- section.image_one.attach(io: file, filename: 't-shirt.png')
+ section.build_image_one
+ section.image_one.attachment.attach(io: file, filename: 't-shirt.png')
section
end
let(:file) { File.open(file_fixture('icon_256x256.png')) }
it 'is valid' do
- expect(section.valid?).to be(true)
+ expect(section).to be_valid
+ end
+
+ it 'image is valid' do
+ expect(section.image_one).to be_valid
end
end
end