Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strict loading for Active Storage previews #50043

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions activestorage/CHANGELOG.md
@@ -1,3 +1,9 @@
* Prevent `ActiveRecord::StrictLoadingViolationError` when strict loading is
enabled and the variant of an Active Storage preview has already been
processed (for example, by calling `ActiveStorage::Preview#url`).

*Jonathan Hefner*

* Fix `preprocessed: true` option for named variants of previewable files.

*Nico Wenterodt*
Expand Down
5 changes: 4 additions & 1 deletion activestorage/app/models/active_storage/blob.rb
Expand Up @@ -134,7 +134,10 @@ def signed_id_verifier # :nodoc:

def scope_for_strict_loading # :nodoc:
if strict_loading_by_default? && ActiveStorage.track_variants
includes(variant_records: { image_attachment: :blob }, preview_image_attachment: :blob)
includes(
variant_records: { image_attachment: :blob },
preview_image_attachment: { blob: { variant_records: { image_attachment: :blob } } }
)
else
all
end
Expand Down
Expand Up @@ -110,7 +110,7 @@ class ActiveStorage::Representations::RedirectControllerWithPreviewsTest < Actio
class ActiveStorage::Representations::RedirectControllerWithPreviewsWithStrictLoadingTest < ActionDispatch::IntegrationTest
setup do
@blob = create_file_blob filename: "report.pdf", content_type: "application/pdf"
@blob.preview(resize_to_limit: [100, 100]).processed
@blob.preview(resize_to_limit: [100, 100]).processed.send(:variant).processed
end

test "showing existing preview record inline" do
Expand Down
21 changes: 3 additions & 18 deletions activestorage/test/models/blob_test.rb
Expand Up @@ -328,28 +328,13 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
end

test "scope_for_strict_loading adds includes only when track_variants and strict_loading_by_default" do
assert_empty(
ActiveStorage::Blob.scope_for_strict_loading.includes_values,
"Expected ActiveStorage::Blob.scope_for_strict_loading have no includes"
)
assert_empty ActiveStorage::Blob.scope_for_strict_loading.includes_values

with_strict_loading_by_default do
includes_values = ActiveStorage::Blob.scope_for_strict_loading.includes_values

assert(
includes_values.any? { |values| values[:variant_records] == { image_attachment: :blob } },
"Expected ActiveStorage::Blob.scope_for_strict_loading to have variant_records included"
)
assert(
includes_values.any? { |values| values[:preview_image_attachment] == :blob },
"Expected ActiveStorage::Blob.scope_for_strict_loading to have preview_image_attachment included"
)
assert_not_empty ActiveStorage::Blob.scope_for_strict_loading.includes_values

without_variant_tracking do
assert_empty(
ActiveStorage::Blob.scope_for_strict_loading.includes_values,
"Expected ActiveStorage::Blob.scope_for_strict_loading have no includes"
)
assert_empty ActiveStorage::Blob.scope_for_strict_loading.includes_values
end
end
end
Expand Down