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
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.rbnew file mode 100644index 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+enddiff --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.rbnew file mode 100644index 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+enddiff --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.rbnew file mode 100644index 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+enddiff --git a/api/app/serializers/spree/api/v2/platform/hero_image_serializer.rb b/api/app/serializers/spree/api/v2/platform/hero_image_serializer.rbnew file mode 100644index 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+enddiff --git a/api/app/serializers/spree/api/v2/platform/image_gallery_serializer.rb b/api/app/serializers/spree/api/v2/platform/image_gallery_serializer.rbnew file mode 100644index 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+enddiff --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.rbnew file mode 100644index 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+enddiff --git a/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb b/api/app/serializers/spree/v2/storefront/cms_section_serializer.rbindex 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 Spreeattributes :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_helpersurl_helpers.rails_representation_path(section.send("img_#{count}_#{size}"), only_path: true)enddiff --git a/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rbindex 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 dolet!(:homepage) { create(:cms_homepage, store: store) }let!(:cms_section) dosection = 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')sectionendlet(:file) { File.open(file_fixture('icon_256x256.jpg')) }@@ -72,7 +73,8 @@ describe Spree::V2::Storefront::CmsSectionSerializer dolet!(:homepage) { create(:cms_homepage, store: store) }let!(:cms_section) dosection = 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')sectionendlet(:file) { File.open(file_fixture('icon_256x256.jpg')) }@@ -85,7 +87,8 @@ describe Spree::V2::Storefront::CmsSectionSerializer dolet!(:homepage) { create(:cms_homepage, store: store) }let!(:cms_section) dosection = 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')sectionendlet(: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.rbindex 553eda44e6..ed359d7087 100644--- a/core/app/models/spree/cms_section.rb+++ b/core/app/models/spree/cms_section.rb@@ -7,22 +7,22 @@ module Spreevalidate :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))endendend@@ -31,8 +31,6 @@ module Spreevalidates :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.rbnew file mode 100644index 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+enddiff --git a/core/app/models/spree/cms_section_image_one.rb b/core/app/models/spree/cms_section_image_one.rbnew file mode 100644index 0000000000..1c6250257d--- /dev/null+++ b/core/app/models/spree/cms_section_image_one.rb@@ -0,0 +1,4 @@+module Spree+ class CmsSectionImageOne < CmsSectionImage+ end+enddiff --git a/core/app/models/spree/cms_section_image_three.rb b/core/app/models/spree/cms_section_image_three.rbnew file mode 100644index 0000000000..e2f68a1e20--- /dev/null+++ b/core/app/models/spree/cms_section_image_three.rb@@ -0,0 +1,4 @@+module Spree+ class CmsSectionImageThree < CmsSectionImage+ end+enddiff --git a/core/app/models/spree/cms_section_image_two.rb b/core/app/models/spree/cms_section_image_two.rbnew file mode 100644index 0000000000..ae2bf16f82--- /dev/null+++ b/core/app/models/spree/cms_section_image_two.rb@@ -0,0 +1,4 @@+module Spree+ class CmsSectionImageTwo < CmsSectionImage+ end+enddiff --git a/core/spec/models/spree/cms_section_spec.rb b/core/spec/models/spree/cms_section_spec.rbindex 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 dolet(:section) dosection = 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')sectionendlet(: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_validendendend