Revert "Immediate avatar and embed variants"

This commit is contained in:
Jorge Manrubia
2025-12-07 12:06:03 +01:00
committed by GitHub
parent 7fe22b5767
commit c8c91259c7
9 changed files with 17 additions and 59 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ GIT
GIT
remote: https://github.com/rails/rails.git
revision: 3d105fc346fbb3121bbcf6340f2b19104bf326f0
revision: 3c3f6c8a253c8a0f346695374f927c9ab32fbefb
branch: main
specs:
actioncable (8.2.0.alpha)
+3 -3
View File
@@ -75,7 +75,7 @@ GIT
GIT
remote: https://github.com/rails/rails.git
revision: 3d105fc346fbb3121bbcf6340f2b19104bf326f0
revision: 690ec8898318b8f50714e86676353ebe1551261e
branch: main
specs:
actioncable (8.2.0.alpha)
@@ -292,7 +292,7 @@ GEM
actionview (>= 7.0.0)
activesupport (>= 7.0.0)
jmespath (1.6.2)
json (2.17.1)
json (2.16.0)
jwt (3.1.2)
base64
kamal (2.9.0)
@@ -552,7 +552,7 @@ GEM
ostruct
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.9)
stringio (3.1.8)
thor (1.4.0)
thruster (0.1.16)
thruster (0.1.16-aarch64-linux)
+6 -3
View File
@@ -1,10 +1,13 @@
module Attachments
extend ActiveSupport::Concern
# Variants used by ActionText embeds. Processed immediately on attachment to avoid
# read replica issues (lazy variants would attempt writes on read replicas).
# The variants we use must all declared here so that we can preprocess them.
#
# Patched into ActionText::RichText in config/initializers/action_text.rb
# If they are not preprocessed, then Rails will attempt to transform the image on-the-fly when
# they are first viewed, which may be on the read replica where writing to the database is not
# allowed. Chaos will ensue if that happens.
#
# These variants are patched into ActionText::RichText in config/initializers/action_text.rb
VARIANTS = {
# vipsthumbnail used to create sized image variants has a intent setting to manage colors during
# resize. By setting an invalid intent value the gif-incompatible intent filtering is skipped and
+1 -1
View File
@@ -5,7 +5,7 @@ module User::Avatar
included do
has_one_attached :avatar do |attachable|
attachable.variant :thumb, resize_to_fill: [ 256, 256 ], process: :immediately
attachable.variant :thumb, resize_to_fill: [ 256, 256 ]
end
validate :avatar_content_type_allowed
+1 -1
View File
@@ -7,7 +7,7 @@ module ActionText
# This overrides the default :embeds association!
has_many_attached :embeds do |attachable|
::Attachments::VARIANTS.each do |variant_name, variant_options|
attachable.variant variant_name, **variant_options, process: :immediately
attachable.variant variant_name, variant_options.merge(preprocessed: true)
end
end
end
@@ -27,9 +27,7 @@ class Users::AvatarsControllerTest < ActionDispatch::IntegrationTest
end
test "show own image redirects to the blob url" do
# Create blob separately to ensure file is uploaded before variant processing
blob = ActiveStorage::Blob.create_and_upload!(io: File.open(file_fixture("moon.jpg")), filename: "moon.jpg", content_type: "image/jpeg")
users(:david).avatar.attach(blob)
users(:david).avatar.attach(io: File.open(file_fixture("moon.jpg")), filename: "moon.jpg", content_type: "image/jpeg")
assert users(:david).avatar.attached?
get user_avatar_path(users(:david))
@@ -38,9 +36,7 @@ class Users::AvatarsControllerTest < ActionDispatch::IntegrationTest
end
test "show other image redirects to the blob url" do
# Create blob separately to ensure file is uploaded before variant processing
blob = ActiveStorage::Blob.create_and_upload!(io: File.open(file_fixture("moon.jpg")), filename: "moon.jpg", content_type: "image/jpeg")
users(:kevin).avatar.attach(blob)
users(:kevin).avatar.attach(io: File.open(file_fixture("moon.jpg")), filename: "moon.jpg", content_type: "image/jpeg")
assert users(:kevin).avatar.attached?
get user_avatar_path(users(:kevin))
@@ -22,12 +22,11 @@ class Notification::BundleMailerTest < ActionMailer::TestCase
end
test "renders avatar with external image URL when avatar is attached" do
blob = ActiveStorage::Blob.create_and_upload!(
@user.avatar.attach(
io: File.open(Rails.root.join("test", "fixtures", "files", "avatar.png")),
filename: "avatar.png",
content_type: "image/png"
)
@user.avatar.attach(blob)
create_notification(@user)
-20
View File
@@ -4,24 +4,4 @@ class CommentTest < ActiveSupport::TestCase
setup do
Current.session = sessions(:david)
end
test "rich text embed variants are processed immediately on attachment" do
comment = cards(:logo).comments.create!(body: "Check this out")
comment.body.body.attachables # force load
blob = ActiveStorage::Blob.create_and_upload! \
io: File.open(file_fixture("moon.jpg")),
filename: "moon.jpg",
content_type: "image/jpeg"
comment.body.body = ActionText::Content.new(comment.body.body.to_html).append_attachables(blob)
comment.save!
embed = comment.body.embeds.sole
Attachments::VARIANTS.each_key do |variant_name|
variant = embed.variant(variant_name)
assert variant.processed?, "Expected #{variant_name} variant to be processed immediately"
end
end
end
+2 -22
View File
@@ -2,8 +2,7 @@ require "test_helper"
class User::AvatarTest < ActiveSupport::TestCase
test "avatar_thumbnail returns variant for variable images" do
blob = ActiveStorage::Blob.create_and_upload!(io: File.open(file_fixture("moon.jpg")), filename: "moon.jpg", content_type: "image/jpeg")
users(:david).avatar.attach(blob)
users(:david).avatar.attach(io: File.open(file_fixture("moon.jpg")), filename: "moon.jpg", content_type: "image/jpeg")
assert users(:david).avatar.variable?
assert_equal users(:david).avatar.variant(:thumb).blob, users(:david).avatar_thumbnail.blob
@@ -17,8 +16,7 @@ class User::AvatarTest < ActiveSupport::TestCase
end
test "allows valid image content types" do
blob = ActiveStorage::Blob.create_and_upload!(io: File.open(file_fixture("moon.jpg")), filename: "test.jpg", content_type: "image/jpeg")
users(:david).avatar.attach(blob)
users(:david).avatar.attach(io: File.open(file_fixture("moon.jpg")), filename: "test.jpg")
assert users(:david).valid?
end
@@ -29,22 +27,4 @@ class User::AvatarTest < ActiveSupport::TestCase
assert_not users(:david).valid?
assert_includes users(:david).errors[:avatar], "must be a JPEG, PNG, GIF, or WebP image"
end
test "thumb variant is processed immediately on attachment" do
# Create blob separately to ensure file is uploaded before variant processing.
#
# Root cause: When ActiveStorage::Record uses `connects_to` for read replica support
# (as in SAAS mode), it creates a separate connection pool from application models.
# Since after_commit callbacks are tracked per connection pool, the callback order
# between User's pool (upload) and Attachment's pool (create_variants) isn't guaranteed.
# In MySQL/SAAS mode, the Attachment callback fires before the file is uploaded.
blob = ActiveStorage::Blob.create_and_upload!(
io: File.open(file_fixture("avatar.png")),
filename: "avatar.png",
content_type: "image/png"
)
users(:david).avatar.attach(blob)
assert users(:david).avatar.variant(:thumb).processed?
end
end