1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add support for eager loading all rich text associations at once (#39397)

* Add `with_all_rich_text` method to eager load all RichText models at once

* Update actiontext/test/test_helper.rb

Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>

* Update actiontext/lib/action_text/attribute.rb

Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>

Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>
This commit is contained in:
matt swanson 2020-11-24 12:19:46 -05:00 committed by GitHub
parent ad99c0839e
commit 9b6459aab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 0 deletions

View file

@ -1,3 +1,7 @@
* Add `with_all_rich_text` method to eager load all rich text associations on a model at once
*Matt Swanson*, *DHH*
## Rails 6.1.0.rc1 (November 02, 2020) ##
* Declare `ActionText::FixtureSet.attachment` to generate an

View file

@ -45,6 +45,16 @@ module ActionText
scope :"with_rich_text_#{name}", -> { includes("rich_text_#{name}") }
scope :"with_rich_text_#{name}_and_embeds", -> { includes("rich_text_#{name}": { embeds_attachments: :blob }) }
end
# Eager load all dependent RichText models in bulk.
def with_all_rich_text
eager_load(rich_text_association_names)
end
private
def rich_text_association_names
reflect_on_all_associations(:has_one).collect(&:name).select { |n| n.start_with?("rich_text_") }
end
end
end
end

View file

@ -22,6 +22,23 @@ if ActiveSupport::TestCase.respond_to?(:fixture_path=)
end
class ActiveSupport::TestCase
def assert_queries(expected_count)
ActiveRecord::Base.connection.materialize_transactions
queries = []
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, sql:, name:, **|
queries << sql unless %w[ SCHEMA TRANSACTION ].include?(name)
end
yield.tap do
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
end
end
def assert_no_queries(&block)
assert_queries(0, &block)
end
private
def create_file_blob(filename:, content_type:, metadata: nil)
ActiveStorage::Blob.create_and_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata

View file

@ -86,4 +86,23 @@ class ActionText::ModelTest < ActiveSupport::TestCase
assert_kind_of ActionText::RichText, message.content
end
end
test "eager loading" do
Message.create!(subject: "Subject", content: "<h1>Content</h1>")
message = assert_queries(2) { Message.with_rich_text_content.last }
assert_no_queries do
assert_equal "Content", message.content.to_plain_text
end
end
test "eager loading all rich text" do
Message.create!(subject: "Subject", content: "<h1>Content</h1>", body: "<h2>Body</h2>")
message = assert_queries(1) { Message.with_all_rich_text.last }
assert_no_queries do
assert_equal "Content", message.content.to_plain_text
assert_equal "Body", message.body.to_plain_text
end
end
end