Skip to content
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

Open
Merovex opened this issue Jul 10, 2022 · 3 comments
Open

DRY Rendering activities using ViewComponent #373

Merovex opened this issue Jul 10, 2022 · 3 comments
Assignees
Labels

Comments

@Merovex
Copy link

Merovex commented Jul 10, 2022

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 add

class ActivityComponent < ViewComponent::Base
  def initialize(activity:)
    @activity = activity
  end
end

In the activity_component.html.erb add

<article>
  <div>
    <h4>
      <%= t(@object.key).html_safe % {target: @object.trackable.to_s} %>
    </h4>
    <div>
      <span><%= @object.owner.to_s unless @object.owner.nil? %></span>
      <span>&middot;</span>
      <%= @object.created_at %>
    </div>
  </div>
</article>

In the view add:

<% @activities.each do |activity|%>
  <%= render ActivityComponent.new(activity:) %>
<% end %>

In the translation file add:

  en:
    post:
      create: "Created post %{target}"
      update: "Updated %{target}"
@ur5us ur5us self-assigned this Jul 20, 2022
@ur5us
Copy link
Collaborator

ur5us commented Jul 20, 2022

@Merovex Thanks for this. Indeed, I think something like that could be nice default. Though, I’d like to challenge the following assumption:

There's no need to create several view files in app/views/public_activity.

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.

@ur5us ur5us added the feature label Jul 20, 2022
@pokonski
Copy link
Member

pokonski commented Oct 11, 2022 via email

@bellef
Copy link

bellef commented Nov 29, 2022

Hi 👋
@Merovex have you been working on a PR for this by any chance?
I'm curious about how this feature would integrate in our project.

François

@ur5us ur5us changed the title DRY Rendering Activities using ViewComponent DRY Rendering activities using ViewComponent Jun 14, 2023
@ur5us ur5us changed the title DRY Rendering activities using ViewComponent DRY Rendering ctivities using ViewComponent Jun 14, 2023
@ur5us ur5us changed the title DRY Rendering ctivities using ViewComponent DRY Rendering activities using ViewComponent Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants