Skip to content

Add extensions with initial support for pre_render#358

Merged
jhollinger merged 6 commits intoprocore-oss:mainfrom
jhollinger:extensions
Jan 17, 2024
Merged

Add extensions with initial support for pre_render#358
jhollinger merged 6 commits intoprocore-oss:mainfrom
jhollinger:extensions

Conversation

@jhollinger
Copy link
Copy Markdown
Contributor

@jhollinger jhollinger commented Nov 9, 2023

Initial attempt at a formal extension API as part of #341. I've built a POC ActiveRecord automagic preloader against this and #357.

In this proposal, an "extension" is a class inheriting from Blueprinter::Extension and passed to Blueprinter.configure. This PR defines only one extension method: pre_render. It intercepts whatever is passed to .render and returns (potentially) a modified result.

In the simplified version of my POC below, the extension figures out which associations to preload from the Blueprint view (using reflection), and returns the ActiveRecord::Relation with preload called.

Blueprinter.configure do |config|
  config.extensions << MyPreloadExtension.new
end

q = Widget.
  where(...).
  preload_blueprint

# All associations in WidgetBlueprint:default get preloaded
results = WidgetBlueprint.render(q)

class MyPreloadExtension < Blueprinter::Extension
  def pre_render(object, blueprint, view, options)
    return object unless object.is_a?(ActiveRecord::Relation) && object.preload_blueprint?

    preloads = figure_out_preloads(blueprint, view, object.model)
    object.preload(preloads)
  end
end

@jhollinger jhollinger marked this pull request as ready for review November 9, 2023 14:42
@jhollinger jhollinger requested review from a team and ritikesh as code owners November 9, 2023 14:42
@jhollinger jhollinger changed the title Add extensions with initial support for render Add extensions with initial support for pre_render Nov 10, 2023
Signed-off-by: Jordan Hollinger <jordan.hollinger@procore.com>
Comment thread lib/blueprinter/extensions.rb Outdated
Signed-off-by: Jordan Hollinger <jordan.hollinger@procore.com>
@jhollinger jhollinger requested a review from ritikesh November 27, 2023 16:43
Comment thread spec/units/extensions_spec.rb
Signed-off-by: Jordan Hollinger <jordan.hollinger@procore.com>
@jhollinger jhollinger requested a review from ritikesh January 4, 2024 18:42
Signed-off-by: Jordan Hollinger <jordan.hollinger@procore.com>
@jhollinger jhollinger merged commit 049cf4e into procore-oss:main Jan 17, 2024
@jhollinger jhollinger deleted the extensions branch January 17, 2024 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants