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

[Feature Request] Ability to add an extension to a single Blueprint #417

Open
1 task done
jhollinger opened this issue Jun 5, 2024 · 9 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@jhollinger
Copy link
Contributor

jhollinger commented Jun 5, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe

Extensions are only global. We have some people wanting to do something with a pre_render extension hook, but only in certain Blueprints. While that's technically achievable by adding the extension globally and doing a check on the passed-in Blueprint, that's pretty ugly.

Describe the feature you'd like to see implemented

An extension or extensions keyword in Blueprinter classes and views. I think they should be purely additive and inheritable across views. This is similar to how transformers can be used.

Describe alternatives you've considered

transform looks like a tempting option, but it's called too late. The pre_render hooks is ideal, except for being global.

Additional context

I'm happy to work on this once we arrive at a good design. Some preliminary ideas:

class MyBlueprint < Blueprinter::Base
  # Add a few extensions to all views
  extension FooExtension, BarExtension

  view :foo do
    # add a dynamic extension to this view
    extension do
      def pre_render(obj, blueprint, view, opts)
        # do stuff
        obj
      end
    end
  end
end
@ritikesh
Copy link
Collaborator

ritikesh commented Jun 6, 2024

Wouldn't they be able to use transformers for that?

@jhollinger
Copy link
Contributor Author

Wouldn't they be able to use transformers for that?

@ritikesh Only in certain circumstances. Transformers run after the object has been "hashified", whereas the pre_render hooks runs before.

Also, it wouldn't surprise me if we add more extension hooks in the future. Having an extension system with N hooks that can be used globally, class-wide, and in specific views strikes me as pretty powerful. They could in theory replace both transformers and extractors.

@lessthanjacob lessthanjacob added the enhancement New feature or request label Jun 6, 2024
@ritikesh
Copy link
Collaborator

ritikesh commented Jun 6, 2024

IMO, the classes should be as simple as possible. We should consolidate all the currently available configurations and try to consolidate them to a good API. Which was the core feature of Blueprinter to begin with. Today we have a lot of default configurations, formatters along with extractors/transformers/extensions and some in-built magic with default views and fields etc. We should look at standardising some of these stuff into a more comprehensive and complete API. I had spoken about this earlier in the extensions PR as well, suggesting to remove the default formatters option in favour of the more expressive extensions. So can we also focus on this while we build around extensions?

@jhollinger
Copy link
Contributor Author

@ritikesh I'm sold. Maybe there should be a separate issue where we outline what we'd need to add to extensions so they could fully replace date formatters, extractors, and transformers (obligatory xkcd reference). This issue would be one of those bullet points, I suppose. Once all that's added to 1.x, we could remove date formatters, extractors, and transformers in a 2.0 release.

@ritikesh
Copy link
Collaborator

ritikesh commented Jun 6, 2024

Should we consider a discussion board for that to brainstorm?

@lessthanjacob
Copy link
Contributor

Should we consider a discussion board for that to brainstorm?

💯 Let's spin up a discussion for this!

@ryanmccarthypdx
Copy link

ryanmccarthypdx commented Jun 7, 2024

Link to discussion: #420

Copy link

github-actions bot commented Aug 7, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 14, 2024
@jhollinger jhollinger reopened this Aug 14, 2024
@jhollinger
Copy link
Contributor Author

Re-opening as this is in-progress for 2.0 (minus the "inline-block extension" bit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants