Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extensions with initial support for pre_render #358

Merged
merged 6 commits into from
Jan 17, 2024
Merged

Add extensions with initial support for pre_render #358

merged 6 commits into from
Jan 17, 2024

Conversation

jhollinger
Copy link
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 ritikesh and a team 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 <[email protected]>
@jhollinger jhollinger requested a review from ritikesh November 27, 2023 16:43
Signed-off-by: Jordan Hollinger <[email protected]>
@jhollinger jhollinger requested a review from ritikesh January 4, 2024 18:42
@jhollinger jhollinger merged commit 049cf4e into procore-oss:main Jan 17, 2024
5 checks passed
@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