From 7f348eb9d48150c2ccec6177e08d9cc1926346a4 Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Mon, 14 Jun 2021 11:03:48 -0500 Subject: [PATCH] Document Active Storage & Action Text eager loading Docs for https://github.com/rails/rails/pull/40842 + https://github.com/rails/rails/pull/39397 [ci skip] --- actiontext/lib/action_text/attribute.rb | 1 + activestorage/app/models/active_storage/attachment.rb | 8 ++++++++ .../app/models/active_storage/blob/representable.rb | 4 ++-- .../app/models/active_storage/variant_with_record.rb | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/actiontext/lib/action_text/attribute.rb b/actiontext/lib/action_text/attribute.rb index c9d9c1d4ee..c7d81b95fe 100644 --- a/actiontext/lib/action_text/attribute.rb +++ b/actiontext/lib/action_text/attribute.rb @@ -24,6 +24,7 @@ module Attribute # # Message.all.with_rich_text_content # Avoids N+1 queries when you just want the body, not the attachments. # Message.all.with_rich_text_content_and_embeds # Avoids N+1 queries when you just want the body and attachments. + # Message.all.with_all_rich_text # Loads all rich text associations. # # === Options # diff --git a/activestorage/app/models/active_storage/attachment.rb b/activestorage/app/models/active_storage/attachment.rb index 8035082911..6e8dcac906 100644 --- a/activestorage/app/models/active_storage/attachment.rb +++ b/activestorage/app/models/active_storage/attachment.rb @@ -7,6 +7,14 @@ # on the attachments table prevents blobs from being purged if they’re still attached to any records. # # Attachments also have access to all methods from {ActiveStorage::Blob}[rdoc-ref:ActiveStorage::Blob]. +# +# If you wish to preload attachments or blobs, you can use these scopes: +# +# # preloads attachments, their corresponding blobs, and variant records (if using `ActiveStorage.track_variants`) +# User.all.with_attached_avatars +# +# # preloads blobs and variant records (if using `ActiveStorage.track_variants`) +# User.first.avatars.with_all_variant_records class ActiveStorage::Attachment < ActiveStorage::Record self.table_name = "active_storage_attachments" diff --git a/activestorage/app/models/active_storage/blob/representable.rb b/activestorage/app/models/active_storage/blob/representable.rb index 65d7d64b9e..4029614f91 100644 --- a/activestorage/app/models/active_storage/blob/representable.rb +++ b/activestorage/app/models/active_storage/blob/representable.rb @@ -12,8 +12,8 @@ module ActiveStorage::Blob::Representable has_one_attached :preview_image end - # Returns an ActiveStorage::Variant instance with the set of +transformations+ provided. This is only relevant for image - # files, and it allows any image to be transformed for size, colors, and the like. Example: + # Returns an ActiveStorage::Variant or ActiveStorage::VariantWithRecord instance with the set of +transformations+ provided. + # This is only relevant for image files, and it allows any image to be transformed for size, colors, and the like. Example: # # avatar.variant(resize_to_limit: [100, 100]).processed.url # diff --git a/activestorage/app/models/active_storage/variant_with_record.rb b/activestorage/app/models/active_storage/variant_with_record.rb index 89d17ce41d..d91b42f460 100644 --- a/activestorage/app/models/active_storage/variant_with_record.rb +++ b/activestorage/app/models/active_storage/variant_with_record.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +# Like a ActiveStorage::Variant, but keeps detail about the variant in the database as a +# ActiveStorage::VariantRecord. This is only used if `ActiveStorage.track_variants` is enabled. class ActiveStorage::VariantWithRecord attr_reader :blob, :variation