Skip to content

Commit

Permalink
Don't auto-preload AGAIN during nested renders
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Hollinger <[email protected]>
  • Loading branch information
jhollinger committed May 21, 2024
1 parent c5235dc commit 744b3c7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/blueprinter-activerecord/preloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def initialize(auto: false, use: :preload, &auto_proc)
# intelligently handles them. There are several unit tests which confirm this behavior.
#
def pre_render(object, blueprint, view, options)
case object
when ActiveRecord::Relation
case object.class.name
when "ActiveRecord::Relation"
if object.preload_blueprint_method || auto || auto_proc&.call(object, blueprint, view, options) == true
object.before_preload_blueprint = extract_preloads object
blueprint_preloads = self.class.preloads(blueprint, view, object.model)
Expand Down
61 changes: 61 additions & 0 deletions test/nested_render_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require 'test_helper'

class NestedRenderTest < Minitest::Test
def setup
DatabaseCleaner.start
customer1 = Customer.create!(name: "ACME")
customer2 = Customer.create!(name: "FOO")
project1 = Project.create!(customer_id: customer1.id, name: "Project A")
project2 = Project.create!(customer_id: customer2.id, name: "Project B")
category = Category.create!(name: "Foo")
ref_plan = RefurbPlan.create!(name: "Plan A")
battery1 = LiIonBattery.create!(num_ions: 100, num_other: 100, refurb_plan_id: ref_plan.id)
battery2 = LeadAcidBattery.create!(num_lead: 100, num_acid: 100)
Widget.create!(customer_id: customer1.id, project_id: project1.id, category_id: category.id, name: "Widget A", battery1: battery1, battery2: battery2)
Widget.create!(customer_id: customer1.id, project_id: project1.id, category_id: category.id, name: "Widget B", battery1: battery1)
Widget.create!(customer_id: customer2.id, project_id: project2.id, category_id: category.id, name: "Widget C", battery1: battery1)
Blueprinter.configure do |config|
config.extensions << BlueprinterActiveRecord::Preloader.new(auto: true)
end
@queries = []
@sub = ActiveSupport::Notifications.subscribe 'sql.active_record' do |_name, _started, _finished, _uid, data|
@queries << data.fetch(:sql)
end
end

def teardown
DatabaseCleaner.clean
Blueprinter.configure do |config|
config.extensions = []
end
ActiveSupport::Notifications.unsubscribe @sub
@queries.clear
end

def test_queries_with_auto
ProjectBlueprint.render(Project.all.strict_loading, view: :extended_plus_with_widgets)
assert_equal [
'SELECT "projects".* FROM "projects"',
'SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (?, ?)',
'SELECT "widgets".* FROM "widgets" WHERE "widgets"."project_id" IN (?, ?)',
], @queries
end

def test_queries_with_auto_and_nested_render
blueprint = Class.new(ProjectBlueprint) do
view :widgets_field do
field :widgets do |project, options|
WidgetBlueprint.render_as_hash(project.widgets, view: :default)
end
end
end

blueprint.render(Project.all.preload(:widgets).strict_loading, view: :widgets_field)
assert_equal [
'SELECT "projects".* FROM "projects"',
'SELECT "widgets".* FROM "widgets" WHERE "widgets"."project_id" IN (?, ?)',
], @queries
end
end

0 comments on commit 744b3c7

Please sign in to comment.