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
Rendering a collection with dynamically-chosen components #1231
Comments
|
@boardfish interesting. For the sake of comparison, can you provide example code for how you would accomplish this using partials? |
|
You could do something like this: <%# app/views/profiles/_profile.html.erb %>
<%= render (profile.complete? ? 'full_profile' : 'null_profile'), profile: profile %><%# app/views/profiles/index.html.erb %>
<%= render @profiles %>I guess it's also achievable this way: <%# app/views/profiles/index.html.erb %>
<% @profiles.each do |profile| %>
<%= render (profile.complete? ? 'full_profile' : 'null_profile')
<% end %>I feel like components that entirely delegate off to other components like this based on their input could be quite an intuitive pattern, and may address a lot of the calls for a component being able to render multiple different templates. |
That's the case I'd be most interested in supporting 👍🏻 |
|
Yeah, I think it really clearly marks the benefit of this as a feature.
|
Feature request
I've just finished talking with @coder2000 about this. They were trying to render a collection, rendering different components based on another argument. So their aim was for
User::Componentto render a collection ofUser::Actives, for example. It's got me thinking about how collections would work with dynamically chosen components — that seemed to be their original aim as they've mentioned in a discussion. There's been some talk about rendering the same component with different templates in the past, which I've done some work to try and establish patterns for, but this was a nice reminder that it still sort of needs to be thought about for collections.Let's say you're rendering a list of user profiles, some of which you'd like to show in less detail because they're incomplete, not visible, or otherwise. One way you could do that is to render
FullProfileComponents, and renderNullProfileComponents for those that aren't present. I think it would follow that you should be able to create a 'factory component' like this:The only thing stopping this at present is
validate_collection_parameter, which should check.new's parameters as well as#initialize. I wanted to open up discussion before making any changes - what do folks think about this as a pattern?The text was updated successfully, but these errors were encountered: