Use Ruby instead of ERB to render comment avatars

Reading conditions in Ruby is much easier than reading them in ERB and,
since the block only had only HTML tag (the <span> tag for deleted
users) but was using Ruby in all other four cases, we're moving it to a
Ruby file.
This commit is contained in:
Javi Martín
2024-04-11 17:18:02 +02:00
parent c655edddde
commit ccaa873e2a
2 changed files with 15 additions and 11 deletions

View File

@@ -1,13 +1,3 @@
<span class="comment-avatar">
<% if comment.as_administrator? %>
<%= special_avatar("avatar_admin.png", class: "admin-avatar") %>
<% elsif comment.as_moderator? %>
<%= special_avatar("avatar_moderator.png", class: "moderator-avatar") %>
<% elsif comment.user.hidden? || comment.user.erased? %>
<span class="icon-deleted user-deleted"></span>
<% elsif comment.user.organization? %>
<%= special_avatar("avatar_collective.png", class: "avatar") %>
<% else %>
<%= render Shared::AvatarComponent.new(comment.user, size: 32) %>
<% end %>
<%= avatar %>
</span>

View File

@@ -7,6 +7,20 @@ class Comments::AvatarComponent < ApplicationComponent
private
def avatar
if comment.as_administrator?
special_avatar("avatar_admin.png", class: "admin-avatar")
elsif comment.as_moderator?
special_avatar("avatar_moderator.png", class: "moderator-avatar")
elsif comment.user.hidden? || comment.user.erased?
tag.span(class: "icon-deleted user-deleted")
elsif comment.user.organization?
special_avatar("avatar_collective.png", class: "avatar")
else
render Shared::AvatarComponent.new(comment.user, size: 32)
end
end
def special_avatar(image_name, options = {})
image_tag(image_name, { size: 32, alt: "" }.merge(options))
end