Make sure variant is created on the same service

This commit is contained in:
Peter Zhu 2019-12-06 20:28:08 -05:00 committed by George Claghorn
parent 352445b2b6
commit d0dd199206
3 changed files with 11 additions and 4 deletions

View File

@ -38,11 +38,11 @@ class ActiveStorage::VariantWithRecord
blob.open do |input|
if blob.content_type.in?(WEB_IMAGE_CONTENT_TYPES)
variation.transform(input) do |output|
yield io: output, filename: blob.filename, content_type: blob.content_type
yield io: output, filename: blob.filename, content_type: blob.content_type, service_name: blob.service.name
end
else
variation.transform(input, format: "png") do |output|
yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png"
yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png", service_name: blob.service.name
end
end
end

View File

@ -49,4 +49,11 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
assert_equal 100, image.width
assert_equal 67, image.height
end
test "variant of a blob is on the same service" do
blob = create_file_blob(filename: "racecar.jpg", service_name: "local_public")
variant = blob.variant(resize: "100x100").process
assert_equal "local_public", variant.image.blob.service_name
end
end

View File

@ -67,8 +67,8 @@ class ActiveSupport::TestCase
ActiveStorage::Blob.create_and_upload! key: key, io: StringIO.new(data), filename: filename, content_type: content_type, identify: identify, service_name: service_name, record: record
end
def create_file_blob(key: nil, filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil, record: nil)
ActiveStorage::Blob.create_and_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata, record: record
def create_file_blob(key: nil, filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil, service_name: nil, record: nil)
ActiveStorage::Blob.create_and_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata, service_name: service_name, record: record
end
def create_blob_before_direct_upload(key: nil, filename: "hello.txt", byte_size:, checksum:, content_type: "text/plain", record: nil)