Module: Spree::Admin::AvatarsHelper

Includes:
ImagesHelper
Defined in:
app/helpers/spree/admin/avatars_helper.rb

Constant Summary collapse

AVATAR_GRADIENTS =
[
  'bg-gradient-to-br from-indigo-500 to-purple-600',
  'bg-gradient-to-br from-pink-400 to-rose-500',
  'bg-gradient-to-br from-blue-400 to-cyan-400',
  'bg-gradient-to-br from-emerald-400 to-teal-400',
  'bg-gradient-to-br from-rose-400 to-yellow-300',
  'bg-gradient-to-br from-teal-200 to-pink-200',
  'bg-gradient-to-br from-rose-300 to-pink-200',
  'bg-gradient-to-br from-orange-200 to-orange-400',
  'bg-gradient-to-br from-violet-500 to-fuchsia-500',
  'bg-gradient-to-br from-amber-400 to-orange-500',
].freeze

Instance Method Summary collapse

Instance Method Details

#avatar_gradient_class(identifier) ⇒ Object



19
20
21
22
# File 'app/helpers/spree/admin/avatars_helper.rb', line 19

def avatar_gradient_class(identifier)
  index = Digest::MD5.hexdigest(identifier.to_s)[0..7].to_i(16) % AVATAR_GRADIENTS.length
  AVATAR_GRADIENTS[index]
end

#render_avatar(user, options = {}) ⇒ String

render an avatar for a user if user doesn’t have an avatar, the user’s initials will be displayed on a rounded-lg background

Parameters:

  • user (Spree::User)

    the user to render the avatar for

  • options (Hash) (defaults to: {})

    the options for the avatar

Options Hash (options):

  • :width (Integer)

    the width of the avatar, default: 128

  • :height (Integer)

    the height of the avatar, default: 128

  • :class (String)

    the CSS class(es) of the avatar, default: ‘avatar’

Returns:

  • (String)

    the avatar



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/spree/admin/avatars_helper.rb', line 32

def render_avatar(user, options = {})
  return unless user.present?

  options[:width] ||= 128
  options[:height] ||= 128
  options[:class] ||= 'avatar rounded-lg flex items-center justify-center text-white text-lg'

  if user.respond_to?(:avatar) && user.avatar.attached? && user.avatar.variable?
    spree_image_tag(
      user.avatar,
      width: options[:width],
      height: options[:height],
      class: options[:class],
      style: "width: #{options[:width]}px; height: #{options[:height]}px;"
    )
  else
    gradient = avatar_gradient_class(user.email)
     :div, user.name&.initials,
                class: "#{gradient} #{options[:class]}",
                style: "width: #{options[:width]}px; height: #{options[:height]}px;"
  end
end