-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't auto-preload AGAIN during nested renders
Signed-off-by: Jordan Hollinger <[email protected]>
- Loading branch information
1 parent
c5235dc
commit 744b3c7
Showing
2 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |