-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Serialize view components #66
base: master
Are you sure you want to change the base?
Serialize view components #66
Conversation
87b4fd9
to
7c3b0cc
Compare
Regarding serializing collections, that should be much easier. ViewComponent::Collection has an initializer which we can implement a Given the various forms of ViewComponent collections and instances, along with our already supported partials, I wonder if we should wrap these all up into a common PORO format for serialization to make deserialization clearer. |
@julianrubisch do you have any thoughts on this idea? |
Sorry, I‘m a bit swamped in work here. I will review this ASAP |
All good. I've thought about this a bit more and here are some ideas:
<%= render(ModalComponent.new) do |component| %>
<% component.with(:header) do %>
Hello Jane
<% end %>
<% component.with(:body) do %>
<p>Have a great day.</p>
<% end %>
<% end %> If that's not possible, and I'm honestly not sure it is, then I think we're stuck and #36 can never happen. |
class ComplexArgumentComponent < ViewComponent::Base | ||
def initialize(active, path, decativate: "Deactivate", activate: "Activate", **others) | ||
@active, @path = active, path | ||
@deactivate = decativate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo `
@deactivate = decativate | |
@deactivate = deactivate |
end | ||
|
||
class ComplexArgumentComponent < ViewComponent::Base | ||
def initialize(active, path, decativate: "Deactivate", activate: "Activate", **others) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
def initialize(active, path, decativate: "Deactivate", activate: "Activate", **others) | |
def initialize(active, path, deactivate: "Deactivate", activate: "Activate", **others) |
|
||
def self.new(*arguments, &block) | ||
instance = allocate | ||
instance.singleton_class.include(FuturismSerializer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this the same as instance.extend(FuturismSerializer)
?
Adds to obj the instance methods from each module given as a parameter.
Finally got to taking a look at this. This is a beautiful solution! I agree with your concerns about VC's One approach could always be to announce limited support of VCs for the time being. What do you think? About submitting a PR to ViewComponent proper: You have my definite consent for continuing, but even if that were successful, we'd have to depend on a future release of VC, correct? |
Sorry for not responding. Last week was my first week back to work after being out for 2 months. I may be slow(er) to respond in the coming weeks. I think some limited support for VCs could at least get the conversation going and could be valuable. Particularly if in the development environment, we raise a warning if they use the RE ViewComponent carrying some of this, yes your assumption is correct. They are releasing quite quickly though and I wouldn't imagine that too many futurism users who need/want VC support would bulk at making taking the small step to jump to the latest VC release. |
Hi @rickychilcott @julianrubisch Have you got this working in production at all? |
Sadly, no. We concluded that it'd be more clever to wait for VC 3 to come out... |
WIP Serialize View Component Spike
Description
This is a proof of concept PR that shows we can shim into the ViewComponent initialization process, capture the raw arguments, and then serialize them back out.
There are limitations here in that if someone modifies some state of the component after initialization, it won't be reflected. I'm not certain that's typical (or recommended), but it could create some confusion. That said, people who do that could override the
#to_futurism_serialized
method to control what gets included and passed back in. Nobody should ever do that though :)If this general approach works, I'd like to ask the ViewComponent team if they could own storing off the raw initialization arguments and then we can just write our own serializer. Alternatively, the ViewComponent team could consider adding serializing support view
ActiveModel::Serialization
Addresses #36
This PR is for discussion and just one portion of supporting ViewComponents via futurism.
Checklist