From ccaa873e2a1f065aeb5a08bc3db0b3eb885123c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 11 Apr 2024 17:18:02 +0200 Subject: [PATCH] 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 tag for deleted users) but was using Ruby in all other four cases, we're moving it to a Ruby file. --- app/components/comments/avatar_component.html.erb | 12 +----------- app/components/comments/avatar_component.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/components/comments/avatar_component.html.erb b/app/components/comments/avatar_component.html.erb index dc59e90b4..a9cc6fde0 100644 --- a/app/components/comments/avatar_component.html.erb +++ b/app/components/comments/avatar_component.html.erb @@ -1,13 +1,3 @@ - <% 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? %> - - <% elsif comment.user.organization? %> - <%= special_avatar("avatar_collective.png", class: "avatar") %> - <% else %> - <%= render Shared::AvatarComponent.new(comment.user, size: 32) %> - <% end %> + <%= avatar %> diff --git a/app/components/comments/avatar_component.rb b/app/components/comments/avatar_component.rb index 2fd691bf7..9d1504658 100644 --- a/app/components/comments/avatar_component.rb +++ b/app/components/comments/avatar_component.rb @@ -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