-
Notifications
You must be signed in to change notification settings - Fork 445
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
Allow to define the view_context without rendering the component #1926
Comments
@Spone, @camertron @joelhawksley what do you think about this, |
@reeganviljoen @mildred my gut says that this kind of functionality isn't the responsibility of ViewComponent. ViewComponent is meant to build objects that have the Single Responsibility of being passed to Rails' I can see the appeal of using Slots to construct tabular data, but I think you might be better off creating some sort of PORO or model to represent your tabular data and rendering that via a ViewComponent for HTML and some other tooling for a spreadsheet format. |
I agree with @joelhawksley, the motivation to add this doesn't feel particularly strong, since components aren't designed to be used like shared objects. I would personally refactor your example to extract the logic into a separate class. |
@camertron i feel the part about not rendering doesn't make sense but passing in a custom view_context when rendering may be useful in niche cases |
Feature request
I'd like to be able to do something like:
That is, a way to associate the component with a view_context so I can do things like translations or access render-specific methods, but not perform the rendering step.
Motivation
I have a component that generates a HTML table. I want to make the table downloadable as a spreadsheet, but I'd like the table generation to happen within the component itself to keep all the things at the same place.
So in my rails controller, when I want to generate the spreadsheet, I create the component, then call the spreadsheet method on it that will generate the spreadsheet file. To do that I need translations, which requires the view_context.
Workaround
In my component I can define:
Then I can use my component like this:
The text was updated successfully, but these errors were encountered: