Skip to content

Commit

Permalink
Merge pull request #28 from ryanmccarthypdx/enable_preloading_on_coll…
Browse files Browse the repository at this point in the history
…ection_proxy

[Issue #26] Make pre_render compatible with all children of ActiveRecord::Relation
  • Loading branch information
jhollinger authored Jun 25, 2024
2 parents 9da1966 + efe6db4 commit 2b39b67
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ widgets = Widget.where(...)
WidgetBlueprint.render(widgets, view: :extended)
```

The query can also be an ActiveRecord::Associations::CollectionProxy:

```ruby
project = Project.find(...)
WidgetBlueprint.render(project.widgets, view: :extended)
```

If you **must** run the query first, there is a way:

```ruby
Expand Down
3 changes: 1 addition & 2 deletions lib/blueprinter-activerecord/preloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ 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.class.name
when "ActiveRecord::Relation", "ActiveRecord::AssociationRelation"
if object.is_a?(ActiveRecord::Relation) && !object.loaded?
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
15 changes: 13 additions & 2 deletions test/nested_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setup
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")
project3 = Project.create!(customer_id: customer2.id, name: "Project C")
category1 = Category.create!(name: "Foo")
category2 = Category.create!(name: "Bar")
ref_plan = RefurbPlan.create!(name: "Plan A")
Expand All @@ -17,13 +18,15 @@ def setup
Widget.create!(customer_id: customer1.id, project_id: project1.id, category_id: category1.id, name: "Widget A", battery1: battery1, battery2: battery2)
Widget.create!(customer_id: customer1.id, project_id: project1.id, category_id: category2.id, name: "Widget B", battery1: battery1)
Widget.create!(customer_id: customer2.id, project_id: project2.id, category_id: category1.id, name: "Widget C", battery1: battery1)
Widget.create!(customer_id: customer2.id, project_id: project3.id, category_id: category1.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
@test_customer = customer2
end

def teardown
Expand All @@ -40,7 +43,15 @@ def test_queries_with_auto
assert_equal [
'SELECT "projects".* FROM "projects"',
'SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (?, ?)',
'SELECT "widgets".* FROM "widgets" WHERE "widgets"."project_id" IN (?, ?)',
'SELECT "widgets".* FROM "widgets" WHERE "widgets"."project_id" IN (?, ?, ?)',
], @queries
end

def test_queries_for_collection_proxies
ProjectBlueprint.render(@test_customer.projects, view: :extended_plus_with_widgets)
assert_equal [
'SELECT "projects".* FROM "projects" WHERE "projects"."customer_id" = ?',
'SELECT "widgets".* FROM "widgets" WHERE "widgets"."project_id" IN (?, ?)'
], @queries
end

Expand All @@ -60,7 +71,7 @@ def test_queries_with_auto_and_nested_render_and_manual_preloads
project_blueprint.render(q)
assert_equal [
'SELECT "projects".* FROM "projects"',
'SELECT "widgets".* FROM "widgets" WHERE "widgets"."project_id" IN (?, ?)',
'SELECT "widgets".* FROM "widgets" WHERE "widgets"."project_id" IN (?, ?, ?)',
'SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (?, ?)',
'SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (?, ?)',
], @queries
Expand Down

0 comments on commit 2b39b67

Please sign in to comment.