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
Add basic attributes API (experimental) #963
base: main
Are you sure you want to change the base?
Conversation
related to #639 |
content_tag(:h1, @title), | ||
content_tag(:p, @body), | ||
content_tag(:date, @posted_at), |
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.
content_tag(:h1, @title), | |
content_tag(:p, @body), | |
content_tag(:date, @posted_at), | |
content_tag(:h1, title), | |
content_tag(:p, body), | |
content_tag(:date, posted_at), |
This API does somewhat deviate from Rails, but I think if Rails does decide to adopt ViewComponent (or parts of it) we can easily omit this extra module. There is some overlap with |
I believe Rails already has a powerful attribute assignment API: See this thread where Rafael França talks a little bit about it. The How would you feel about checking in with Rails Core about the status of |
I like something along the lines of this API. We've been just using ActiveModel::Attributes and we'll deal with it if it breaks down the line. I'm also experimenting with dry-initializer. I believe we need to support positional arguments too. What would be really terrific would be to have some common API that ViewComponent accepts and that we could write a number of "Adapters" that would translate dry-initializer -> VS attributes etc. We could cover common cases and consumers could write or contribute their own for more esoteric patterns. You could imagine auto detecting common cases too. A nice benefit of a having an API like this is the ability to interrogate them to discover facts about the component. In https://github.com/jonspalmer/view_component_storybook we just added a bunch of code to inspect the constructor of the component so that we can validate stories written against the component as trying to set constructor arguments. I'm just introspecting the results of |
This adds a basic and experimental attributes API that is used to define the
properties that a component accepts when initialized.
The idea behind this API is to give the library more control over the
initializer
, attempt to close API gaps likehelpers
sometimes not beingcallable, and reduce boilerplate when assigning attributes.
As-is, the API introduces two new methods:
requires :name
- defines a required attribute accessible via thename
property.. If this value is not provided when initializing the component anArgumentError
is raised.accepts :name
- defines an optional attribute accessible via thename
property. Optionally, a default value can be provided by passing adefault
kwarg. e.g.accepts :name, default: "NullUser"
.This API is not added to all components by default, but can be opted-into by
adding
include ViewComponent::Attributes
to a component.Feedback/thoughts welcome!