Class: Collavre::AvatarComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Collavre::AvatarComponent
- Defined in:
- app/components/collavre/avatar_component.rb
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #avatar_url ⇒ Object
-
#cache_key ⇒ Object
Cache key for this component instance.
- #default_avatar? ⇒ Boolean
- #email ⇒ Object
- #initial ⇒ Object
-
#initialize(user:, size: 32, classes: "", data: {}) ⇒ AvatarComponent
constructor
A new instance of AvatarComponent.
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
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
21 22 23 |
# File 'app/components/collavre/avatar_component.rb', line 21 def classes @classes end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'app/components/collavre/avatar_component.rb', line 21 def data @data end |
#size ⇒ Object (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_url ⇒ Object
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_key ⇒ Object
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
33 34 35 |
# File 'app/components/collavre/avatar_component.rb', line 33 def default_avatar? @user && !@user.avatar.attached? && @user.avatar_url.blank? end |
#email ⇒ Object
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 |
#initial ⇒ Object
37 38 39 |
# File 'app/components/collavre/avatar_component.rb', line 37 def initial @user.display_name[0].upcase end |