Add avatar component

This commit is contained in:
Andreas Nedbal 2023-10-20 11:34:42 +02:00
parent 62c26771b9
commit e19786cbaa
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,4 @@
%img{ class: avatar_classes,
alt: alt_text,
src: avatar_image,
loading: :lazy }

View File

@ -0,0 +1,30 @@
# frozen_string_literal: true
class AvatarComponent < ViewComponent::Base
ALLOWED_SIZES = %w[xs sm md lg xl xxl].freeze
def initialize(user:, size:, classes: [])
@user = user
@size = size if ALLOWED_SIZES.include? size
@classes = classes
end
private
def size_to_version(size)
case size
when "xs", "sm"
:small
when "md", "lg"
:medium
when "xl", "xxl"
:large
end
end
def alt_text = "@#{@user.screen_name}"
def avatar_classes = @classes.unshift("avatar-#{@size}")
def avatar_image = @user.profile_picture.url(size_to_version(@size))
end