-
Notifications
You must be signed in to change notification settings - Fork 336
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
DRY Rendering activities using ViewComponent #373
Comments
@Merovex Thanks for this. Indeed, I think something like that could be nice default. Though, I’d like to challenge the following assumption:
In an app I work on this is very much not true. However, that does not invalidate this idea. Also, I’m not sure whether ViewComponent is justified given how little of it is used here but that might just be the MVP example code. As ViewComponent provides slots it could very well be a good idea to use it and allow passing a custom header and/or body. Let me if you’re keen on working on a PR. Otherwise, I might try to finally look into it when I’m back from holiday. |
I've been using View Component for another project that requires custom
partials and I like it :)
niedz., 10 lip 2022, 05:59 użytkownik Ben W ***@***.***>
napisał:
… I would like to suggest view components as a curiously DRY way of
rendering the activities. There's no need to create several view files in
app/views/public_activity.
Render Activities using ViewComponent
Install view component:
bundel add view_component
Create the view component
rails generate component Activity
In activity_component.rb add
class ActivityComponent < ViewComponent::Base
def initialize(object:)
@object = object
end
end
In the activity_component.html.erb add
<article>
<div>
<h4>
<%= ***@***.***).html_safe % {target: @object.trackable.to_s} %>
</h4>
<div>
<span><%= @object.owner.to_s unless @object.owner.nil? %></span>
<span>·</span>
<%= @object.created_at %>
</div>
</div>
</article>
In the view add:
<% @activities.each do |activity|%>
<%= render ActivityComponent.new(object: activity) %>
<% end %>
In the translation file add:
en:
post:
create: "Created post %{target}"
update: "Updated %{target}"
—
Reply to this email directly, view it on GitHub
<#373>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABBB2XXGCNLEDTT4JHNH6TVTLCKPANCNFSM53E7P6PQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Hi 👋 François |
I would like to suggest view components as a curiously DRY way of rendering the activities. There's no need to create several view files in
app/views/public_activity
.Render Activities using ViewComponent
Install view component:
bundle add view_component
Create the view component
rails generate component Activity
In
activity_component.rb
addIn the
activity_component.html.erb
addIn the view add:
In the translation file add:
The text was updated successfully, but these errors were encountered: