Skip to content

Commit

Permalink
Remove deprecated private API in ActiveRecord::Associations::Preloader
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Nov 17, 2021
1 parent 0792661 commit e3b9779
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
38 changes: 14 additions & 24 deletions activerecord/lib/active_record/associations/preloader.rb
Expand Up @@ -93,25 +93,21 @@ class Preloader # :nodoc:
# associations before querying the database. This can save database
# queries by reusing in-memory objects. The optimization is only applied
# to single associations (i.e. :belongs_to, :has_one) with no scopes.
def initialize(associate_by_default: true, **kwargs)
if kwargs.empty?
ActiveSupport::Deprecation.warn("Calling `Preloader#initialize` without arguments is deprecated and will be removed in Rails 7.0.")
else
@records = kwargs[:records]
@associations = kwargs[:associations]
@scope = kwargs[:scope]
@available_records = kwargs[:available_records] || []
@associate_by_default = associate_by_default
def initialize(records:, associations:, scope: nil, available_records: [], associate_by_default: true)
@records = records
@associations = associations
@scope = scope
@available_records = available_records || []
@associate_by_default = associate_by_default

@tree = Branch.new(
parent: nil,
association: nil,
children: associations,
associate_by_default: @associate_by_default,
scope: @scope
)
@tree.preloaded_records = records
end
@tree = Branch.new(
parent: nil,
association: nil,
children: @associations,
associate_by_default: @associate_by_default,
scope: @scope
)
@tree.preloaded_records = @records
end

def empty?
Expand All @@ -124,12 +120,6 @@ def call
loaders
end

def preload(records, associations, preload_scope = nil)
ActiveSupport::Deprecation.warn("`preload` is deprecated and will be removed in Rails 7.0. Call `Preloader.new(kwargs).call` instead.")

Preloader.new(records: records, associations: associations, scope: preload_scope).call
end

def branches
@tree.children
end
Expand Down
12 changes: 0 additions & 12 deletions activerecord/test/cases/associations_test.rb
Expand Up @@ -397,18 +397,6 @@ def test_preload_with_scope
assert_equal [comments(:greetings)], post.comments
end

def test_legacy_preload_with_scope
post = posts(:welcome)

assert_deprecated do
preloader = ActiveRecord::Associations::Preloader.new
preloader.preload([post], :comments, Comment.where(body: "Thank you for the welcome"))
end

assert_predicate post.comments, :loaded?
assert_equal [comments(:greetings)], post.comments
end

def test_preload_makes_correct_number_of_queries_on_array
post = posts(:welcome)

Expand Down

0 comments on commit e3b9779

Please sign in to comment.