aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2017-11-13 16:36:39 -0500
committerGeorge Claghorn <george@basecamp.com>2017-11-13 16:36:39 -0500
commit704a7e425ca99af1b778c764a86e5388647631dd (patch)
tree115382292c62a22a4ae2220b20f5dd94eba8ff70 /activestorage
parent233d6a2b563e34156c5fc2317c19dc1823447c18 (diff)
downloadrails-704a7e425ca99af1b778c764a86e5388647631dd.tar.gz
rails-704a7e425ca99af1b778c764a86e5388647631dd.tar.bz2
rails-704a7e425ca99af1b778c764a86e5388647631dd.zip
Preserve existing metadata when analyzing a blob
Closes #31138.
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/app/models/active_storage/blob.rb2
-rw-r--r--activestorage/test/models/attachments_test.rb25
-rw-r--r--activestorage/test/test_helper.rb4
3 files changed, 28 insertions, 3 deletions
diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb
index 99823e14c6..2aa05d665e 100644
--- a/activestorage/app/models/active_storage/blob.rb
+++ b/activestorage/app/models/active_storage/blob.rb
@@ -249,7 +249,7 @@ class ActiveStorage::Blob < ActiveRecord::Base
# You won't ordinarily need to call this method from a Rails application. New blobs are automatically and asynchronously
# analyzed via #analyze_later when they're attached for the first time.
def analyze
- update! metadata: extract_metadata_via_analyzer
+ update! metadata: metadata.merge(extract_metadata_via_analyzer)
end
# Enqueues an ActiveStorage::AnalyzeJob which calls #analyze.
diff --git a/activestorage/test/models/attachments_test.rb b/activestorage/test/models/attachments_test.rb
index 47f2bd7911..96bc963cff 100644
--- a/activestorage/test/models/attachments_test.rb
+++ b/activestorage/test/models/attachments_test.rb
@@ -88,6 +88,16 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
end
+ test "preserve existing metadata when analyzing a newly-attached blob" do
+ blob = create_file_blob(metadata: { foo: "bar" })
+
+ perform_enqueued_jobs do
+ @user.avatar.attach blob
+ end
+
+ assert_equal "bar", blob.reload.metadata[:foo]
+ end
+
test "purge attached blob" do
@user.avatar.attach create_blob(filename: "funky.jpg")
avatar_key = @user.avatar.key
@@ -193,6 +203,21 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
end
+ test "preserve existing metadata when analyzing newly-attached blobs" do
+ blobs = [
+ create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", metadata: { foo: "bar" }),
+ create_file_blob(filename: "video.mp4", content_type: "video/mp4", metadata: { foo: "bar" })
+ ]
+
+ perform_enqueued_jobs do
+ @user.highlights.attach(blobs)
+ end
+
+ blobs.each do |blob|
+ assert_equal "bar", blob.reload.metadata[:foo]
+ end
+ end
+
test "purge attached blobs" do
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
highlight_keys = @user.highlights.collect(&:key)
diff --git a/activestorage/test/test_helper.rb b/activestorage/test/test_helper.rb
index 38408cdad3..55da781f2a 100644
--- a/activestorage/test/test_helper.rb
+++ b/activestorage/test/test_helper.rb
@@ -45,8 +45,8 @@ class ActiveSupport::TestCase
ActiveStorage::Blob.create_after_upload! io: StringIO.new(data), filename: filename, content_type: content_type
end
- def create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")
- ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type
+ def create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil)
+ ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata
end
def create_blob_before_direct_upload(filename: "hello.txt", byte_size:, checksum:, content_type: "text/plain")