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.
This commit is contained in:
Prikesh Savla
2025-12-08 09:08:35 +05:30
parent dbd0100044
commit f4d59305d7
12 changed files with 36 additions and 43 deletions

View File

@@ -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)

View File

@@ -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),

View File

@@ -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) }

View File

@@ -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)

View File

@@ -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|

View File

@@ -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

BIN
spec/fixtures/files/logo.bmp vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -2,7 +2,7 @@
http_interactions:
- request:
method: get
uri: https://s3.amazonaws.com/ofn_production/eofop2en1y6tu9fr1x9b0wzwgs5r
uri: https://s3.amazonaws.com/ofn_production/eofop2en1y6tu9fr1x9b0wzwgs5r.png
body:
encoding: US-ASCII
string: ''

View File

@@ -391,24 +391,8 @@ RSpec.describe Enterprise do
let(:content_type) { 'image/png' }
before do
blob = instance_double(
"ActiveStorage::Blob",
filename: ActiveStorage::Filename.new('white-label-logo.png'),
content_type:,
byte_size: 1024
)
# InstanceDouble is not working for attachment case as the blob method is not yet defined
# on instantiation.
attachment = double(
"ActiveStorage::Attached::One",
blank?: false,
attached?: true,
blob:
)
allow(enterprise)
.to receive(:white_label_logo).and_return(attachment)
blob = Rack::Test::UploadedFile.new('spec/fixtures/files/logo.png', content_type)
enterprise.white_label_logo.attach(blob)
end
context 'when the file attached is a PNG image' do
@@ -419,6 +403,12 @@ RSpec.describe Enterprise do
context 'when the file attached is a BMP image' do
let(:content_type) { 'image/bmp' }
before do
blob = Rack::Test::UploadedFile.new('spec/fixtures/files/logo.bmp', content_type)
enterprise.white_label_logo.attach(blob)
end
it 'is not valid' do
expect(enterprise).not_to be_valid
end

View File

@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe ImageImporter do
let(:ofn_url) { "https://s3.amazonaws.com/ofn_production/eofop2en1y6tu9fr1x9b0wzwgs5r" }
let(:ofn_url) { "https://s3.amazonaws.com/ofn_production/eofop2en1y6tu9fr1x9b0wzwgs5r.png" }
let(:product) { create(:product) }
describe "#import" do

View File

@@ -604,7 +604,7 @@ RSpec.describe '
click_button "Create"
expect(page).to have_text "Attachment has an invalid content type"
expect(page).to have_text "Attachment is not a valid image"
expect(page).to have_text "Attachment is not identified as a valid media file"
end
it "deleting product images" do

View File

@@ -770,12 +770,12 @@ RSpec.describe 'As an enterprise user, I can update my products' do
end
end
it 'shows a modal telling not a valid image when uploading a non valid image file' do
it 'shows a modal telling not a valid image when uploading an invalid image file' do
within ".reveal-modal" do
attach_file 'image[attachment]',
Rails.public_path.join('invalid_image.jpg'),
visible: false
expect(page).to have_content /Attachment is not a valid image/
expect(page).to have_content /Attachment is not identified as a valid media file/
end
end
end