Module: InitialjsRails::ViewHelpers

Defined in:
lib/initialjs-rails/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#avatar_image(avatarable, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/initialjs-rails/view_helpers.rb', line 5

def avatar_image(avatarable, options = {})
  size          = options.fetch(:size)             { 100 }
  klass         = options.fetch(:class)            { '' }
  round_corners = options.fetch(:round_corners)    { true }
  seed          = options.fetch(:seed)             { 0 }
  char_count    = options.fetch(:count)            { 1 }
  txt_color     = options.fetch(:color)            { '#ffffff' }
  bg_color      = options.fetch(:background_color) { nil }
  initial_src   = options.fetch(:src)              { '/images/initialjs-blank.png' }
  radius        = (size * 0.13).round if round_corners
  font_size     = (size * 0.6).round

  data_attributes = {
    'char-count' => char_count,
    color: bg_color,
    'font-size' => font_size,
    height: size,
    name: get_name_with_count(avatarable, char_count),
    radius: radius,
    seed: seed,
    'text-color' => txt_color,
    width: size
  }.reject { |_, v| v.blank? }

  tag(:img, { alt: get_name(avatarable), class: "initialjs-avatar #{klass}".strip, data: data_attributes, src: initial_src }, true, false)
end

#get_name(avatarable) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/initialjs-rails/view_helpers.rb', line 41

def get_name(avatarable)
  if avatarable.is_a?(String)
    avatarable
  elsif avatarable.respond_to?(:name)
    avatarable.name
  else
    raise ArgumentError, '#avatar_image argument must be a String or respond to :name'
  end
end

#get_name_with_count(avatarable, count) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/initialjs-rails/view_helpers.rb', line 32

def get_name_with_count(avatarable, count)
  name = get_name(avatarable)
  if count == 2
    "#{name.partition(" ").first[0]}#{name.partition(" ").last[0]}"
  else
    name
  end
end