From f4d59305d71d288332e40e31c32553656cd368c6 Mon Sep 17 00:00:00 2001 From: Prikesh Savla Date: Mon, 8 Dec 2025 09:08:35 +0530 Subject: [PATCH] Upgraded gem active_storage_validations from 1.1.2 to 3.0.2 and fixed any upgrade related issues Changed all references of processable_image to processable_file which was a breaking change from v1 to v2 https://github.com/igorkasyanchuk/active_storage_validations/tree/3.0.2?tab=readme-ov-file#upgrading-from-1x-to-2x Also it upgraded the way of validating files from just the file name and content type, so tests also needed to change for file upload checks Refactored all the similar file image validator content type in Spree::Image::ACCEPTED_CONTENT_TYPES and Updated ImageBuilder.import method to use the url.path when getting filename. --- Gemfile.lock | 11 ++++---- app/models/enterprise.rb | 12 ++++---- app/models/enterprise_group.rb | 8 +++--- app/models/spree/image.rb | 6 ++-- .../app/services/image_builder.rb | 2 +- .../supplied_product_importer_spec.rb | 4 +-- spec/fixtures/files/logo.bmp | Bin 0 -> 1818 bytes .../_import/downloads_from_the_Internet.yml | 2 +- spec/models/enterprise_spec.rb | 26 ++++++------------ spec/services/image_importer_spec.rb | 2 +- spec/system/admin/products_spec.rb | 2 +- spec/system/admin/products_v3/update_spec.rb | 4 +-- 12 files changed, 36 insertions(+), 43 deletions(-) create mode 100644 spec/fixtures/files/logo.bmp diff --git a/Gemfile.lock b/Gemfile.lock index 1f02aa3ad8..1dfd9a5d42 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -104,11 +104,12 @@ GEM rails-html-sanitizer (~> 1.6) active_model_serializers (0.8.4) activemodel (>= 3.0) - active_storage_validations (1.1.4) - activejob (>= 5.2.0) - activemodel (>= 5.2.0) - activestorage (>= 5.2.0) - activesupport (>= 5.2.0) + active_storage_validations (3.0.2) + activejob (>= 6.1.4) + activemodel (>= 6.1.4) + activestorage (>= 6.1.4) + activesupport (>= 6.1.4) + marcel (>= 1.0.3) activejob (7.1.6) activesupport (= 7.1.6) globalid (>= 0.3.6) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index b36c3cb33e..c126b28984 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -110,14 +110,14 @@ class Enterprise < ApplicationRecord end validates :logo, - processable_image: true, - content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + processable_file: true, + content_type: ::Spree::Image::ACCEPTED_CONTENT_TYPES validates :promo_image, - processable_image: true, - content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + processable_file: true, + content_type: ::Spree::Image::ACCEPTED_CONTENT_TYPES validates :white_label_logo, - processable_image: true, - content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + processable_file: true, + content_type: ::Spree::Image::ACCEPTED_CONTENT_TYPES validates :terms_and_conditions, content_type: { in: "application/pdf", message: I18n.t(:enterprise_terms_and_conditions_type_error), diff --git a/app/models/enterprise_group.rb b/app/models/enterprise_group.rb index e6208e3000..cb2252b2d4 100644 --- a/app/models/enterprise_group.rb +++ b/app/models/enterprise_group.rb @@ -29,11 +29,11 @@ class EnterpriseGroup < ApplicationRecord has_one_attached :promo_image, service: image_service validates :logo, - processable_image: true, - content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + processable_file: true, + content_type: ::Spree::Image::ACCEPTED_CONTENT_TYPES validates :promo_image, - processable_image: true, - content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + processable_file: true, + content_type: ::Spree::Image::ACCEPTED_CONTENT_TYPES scope :by_position, -> { order('position ASC') } scope :on_front_page, -> { where(on_front_page: true) } diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 350b1c5b4c..431b1679b0 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -2,6 +2,8 @@ module Spree class Image < Asset + ACCEPTED_CONTENT_TYPES = %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + has_one_attached :attachment, service: image_service do |attachment| attachment.variant :mini, resize_to_fill: [48, 48] attachment.variant :small, resize_to_fill: [227, 227] @@ -11,8 +13,8 @@ module Spree validates :attachment, attached: true, - processable_image: true, - content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + processable_file: true, + content_type: ACCEPTED_CONTENT_TYPES validate :no_attachment_errors def self.default_image_url(size) diff --git a/engines/dfc_provider/app/services/image_builder.rb b/engines/dfc_provider/app/services/image_builder.rb index 06ab056e0c..798a5d0f38 100644 --- a/engines/dfc_provider/app/services/image_builder.rb +++ b/engines/dfc_provider/app/services/image_builder.rb @@ -19,7 +19,7 @@ class ImageBuilder < DfcBuilder def self.import(image_link) url = URI.parse(image_link) - filename = File.basename(image_link) + filename = File.basename(url.path) metadata = { custom: { origin: image_link } } Spree::Image.new.tap do |image| diff --git a/engines/dfc_provider/spec/services/supplied_product_importer_spec.rb b/engines/dfc_provider/spec/services/supplied_product_importer_spec.rb index 815a1c6897..07a8d1bbda 100644 --- a/engines/dfc_provider/spec/services/supplied_product_importer_spec.rb +++ b/engines/dfc_provider/spec/services/supplied_product_importer_spec.rb @@ -251,7 +251,7 @@ RSpec.describe SuppliedProductImporter do supplied_product.isVariantOf << tomatoes imported_product = importer.import_variant(supplied_product, supplier).product - expect(imported_product.image.attachment.filename).to eq "tomato.png?v=1" + expect(imported_product.image.attachment.filename).to eq "tomato.png" expect { importer.import_variant(supplied_product, supplier).product @@ -266,7 +266,7 @@ RSpec.describe SuppliedProductImporter do } .to change { imported_product.image } - expect(imported_product.image.attachment.filename).to eq "tomato.png?v=2" + expect(imported_product.image.attachment.filename).to eq "tomato.png" end context "when spree_product_uri doesn't match the server host" do diff --git a/spec/fixtures/files/logo.bmp b/spec/fixtures/files/logo.bmp new file mode 100644 index 0000000000000000000000000000000000000000..f03e68b138349937618b46d6561098e48e82f842 GIT binary patch literal 1818 zcmbu9NoZ6-5Qg7mm`pUHLOdm|5pwb>2oeUzU=R}!MNt$*R75l$Od!D}3Q=58)Qj=p zMT6po8^nW}1o!2jiAxj(#|0JikSOj5&+mUz72ZKl7V}O0UEN*XUH#t6)|RG&$aeC- z0Y<>bFbosH*dU4CTNnf-#d&C&g+Y-2sU2pkXVrVp|9UMAtV-g%UXE{v2k_RAR!n^f z8(Zkw_nm(KAG##_7yo$@j0DNqH2EtWzTnJ!0bZl&5X zbT>4ZRxYpQd~^|>ugYHFpyaURi`rHDoH4KVDYwv(9AX8oRP51yyZQttGS~V6Q zhu7Wcz0=(u1>KAGru)=8Q2{xdKMJiix4{vRRaxH^w4Q&}@F#RwyczuqH2x9DbXL_! z_6GKW>L0xyHZMb4wi%YeId~7U^)^P&%_-Ae=uXq+=p1CM?eG0MVoxll+Vp|Wq8k4O z*>>0+sW3c_*B!|kJr_Cw1=em+r&*cBI%r>nEcAo+y%L^+%)cY5ef*p1IctgE1?7=x xo}Mw)QqREwXonl%XB^0-J;>