Files
fizzy/app/helpers/avatars_helper.rb
T
Rosa Gutierrez 235890e666 Use relative URLs for avatars and rich text attachments
These URLs can be problematic or inconsistent, as we might end up with
rich text content with beta URLs for attachments being used in
production, or viceversa. It'll also be problematic when importing or
exporting data between a self-hosted instance and the SaaS version.
2026-01-14 11:01:42 +01:00

48 lines
1.6 KiB
Ruby

require "zlib"
module AvatarsHelper
AVATAR_COLORS = %w[
#AF2E1B #CC6324 #3B4B59 #BFA07A #ED8008 #ED3F1C #BF1B1B #736B1E #D07B53
#736356 #AD1D1D #BF7C2A #C09C6F #698F9C #7C956B #5D618F #3B3633 #67695E
]
def avatar_background_color(user)
AVATAR_COLORS[Zlib.crc32(user.to_param) % AVATAR_COLORS.size]
end
def avatar_tag(user, hidden_for_screen_reader: false, **options)
link_to user_path(user), class: class_names("avatar btn btn--circle", options.delete(:class)), data: { turbo_frame: "_top" },
aria: { hidden: hidden_for_screen_reader, label: user.name },
tabindex: hidden_for_screen_reader ? -1 : nil,
**options do
avatar_image_tag(user)
end
end
def avatar_tags(users, **options)
users.collect { avatar_tag(it, **options) }.join.html_safe
end
def mail_avatar_tag(user, size: 48, **options)
if user.avatar.attached?
image_tag user_avatar_url(user), alt: user.name, class: "avatar", size: size, **options
else
tag.span class: "avatar", style: "background-color: #{avatar_background_color(user)};" do
user.initials
end
end
end
def avatar_preview_tag(user, hidden_for_screen_reader: false, **options)
tag.span class: class_names("avatar", options.delete(:class)),
aria: { hidden: hidden_for_screen_reader, label: user.name },
tabindex: hidden_for_screen_reader ? -1 : nil do
avatar_image_tag(user, **options)
end
end
def avatar_image_tag(user, **options)
image_tag user_avatar_path(user, script_name: user.account.slug), aria: { hidden: "true" }, size: 48, title: user.name, **options
end
end