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

Pass view to associations #306

Closed
shayani opened this issue Jun 7, 2023 · 7 comments
Closed

Pass view to associations #306

shayani opened this issue Jun 7, 2023 · 7 comments

Comments

@shayani
Copy link

shayani commented Jun 7, 2023

How can I pass the view I'm rendering to nested associations?

Follows an example:

class SchoolBlueprint << Blueprinter::Base 
    field :name
    association :students, blueprint: StudentsBlueprint

    view :detailed do 
        # Want to call the association :students, but with the same view name (detailed)
        # association :students, blueprint: StudentsBlueprint, view: :detailed
    end
end
@Manifold0
Copy link

https://www.rubydoc.info/gems/blueprinter/Blueprinter/Base#association-class_method

class UserBlueprint < [Blueprinter](https://www.rubydoc.info/gems/blueprinter/Blueprinter)::[Base](https://www.rubydoc.info/gems/blueprinter/Blueprinter/Base)
  # code
  association :vehicles, view: :extended, blueprint: VehiclesBlueprint
  # code
end

@shayani
Copy link
Author

shayani commented Jun 14, 2023

Thanks @Manifold0, but that's not the case.

The goal is to call SchoolBlueprint.render(object, view: :detailed) and pass this detailed attribute to all basic associations on blueprint. The way it is now in the sample code, will be generated 2 students associations: one with detailed and other without.

@Manifold0
Copy link

@shayani I think I understand the problem better now. You want the view parameter from the parent SchoolBlueprint to propagate down to the associations? Unfortunately, I do not know how to do that. I'd be very curious to see if anyone else does, it sounds like a useful option.

@ritikesh
Copy link
Collaborator

This project is all but dead, pls refer here. If you feel this is a genuine usecase pls feel free to raise a PR on the fork, I'd be glad to take it forward..

@sdrioux
Copy link

sdrioux commented Jun 22, 2023

class SchoolBlueprint << Blueprinter::Base 
    field :name
    field :students do |object, options|
      StudentsBlueprint.render(object.students, view: options[:view])
    end  
end

@shayani
Copy link
Author

shayani commented Jul 4, 2023

Thank you @sdrioux!

@shayani shayani closed this as completed Jul 4, 2023
@jonatasdaniel
Copy link

options[:view] is empty even passing it to the parent blueprinter. Any ideas why?

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

No branches or pull requests

5 participants