Class: Collavre::AvatarComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/collavre/avatar_component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, size: 32, classes: "", data: {}) ⇒ AvatarComponent

Returns a new instance of AvatarComponent.



14
15
16
17
18
19
# File 'app/components/collavre/avatar_component.rb', line 14

def initialize(user:, size: 32, classes: "", data: {})
  @user = user
  @size = size
  @classes = classes
  @data = data
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



21
22
23
# File 'app/components/collavre/avatar_component.rb', line 21

def classes
  @classes
end

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'app/components/collavre/avatar_component.rb', line 21

def data
  @data
end

#sizeObject (readonly)

Returns the value of attribute size.



21
22
23
# File 'app/components/collavre/avatar_component.rb', line 21

def size
  @size
end

Class Method Details

.cache_key_for(user, size) ⇒ Object

Enable fragment caching for avatar components Cache key includes user version (for display_name changes), avatar blob key, and size



5
6
7
8
9
10
11
12
# File 'app/components/collavre/avatar_component.rb', line 5

def self.cache_key_for(user, size)
  if user
    avatar_key = user.avatar.attached? ? user.avatar.blob&.key : user.avatar_url.to_s
    "avatar/#{user.cache_key_with_version}/#{avatar_key}/#{size}"
  else
    "avatar/anonymous/#{size}"
  end
end

Instance Method Details

#avatar_urlObject



28
29
30
31
# File 'app/components/collavre/avatar_component.rb', line 28

def avatar_url
  # Cache avatar URL computation
  @avatar_url ||= compute_avatar_url
end

#cache_keyObject

Cache key for this component instance



24
25
26
# File 'app/components/collavre/avatar_component.rb', line 24

def cache_key
  self.class.cache_key_for(@user, @size)
end

#default_avatar?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/components/collavre/avatar_component.rb', line 33

def default_avatar?
  @user && !@user.avatar.attached? && @user.avatar_url.blank?
end

#emailObject



41
42
43
44
45
46
47
# File 'app/components/collavre/avatar_component.rb', line 41

def email
  if @user
    @user.display_name
  else
    I18n.t("collavre.comments.anonymous")
  end
end

#initialObject



37
38
39
# File 'app/components/collavre/avatar_component.rb', line 37

def initial
  @user.display_name[0].upcase
end