29824-initials_avatars-WIP.patch

Felix Schäfer, 2022-02-27 16:26

Download (2.95 KB)

View differences:

app/helpers/avatars_helper.rb
47 47
      elsif user.to_s =~ %r{<(.+?)>}
48 48
        email = $1
49 49
      end
50
      if options[:default] == 'blank'
51
        options[:style] ||= +''
52
        options[:style] << "background-image:url(\"data:image/svg+xml,#{url_encode(render(partial: 'common/initials.svg', locals: { initials: user.initials }))}\");"
53
        options[:class] ||= +''
54
        options[:class] << " avatar-color-#{user.id % 8}"
55
      end
50 56
      if email.present?
51 57
        gravatar(email.to_s.downcase, options) rescue nil
52 58
      elsif user.is_a?(AnonymousUser)
app/models/anonymous_user.rb
35 35
  def logged?; false end
36 36
  def admin; false end
37 37
  def name(*args); I18n.t(:label_user_anonymous) end
38
  def initials; 'AN' end
38 39
  def mail=(*args); nil end
39 40
  def mail; nil end
40 41
  def time_zone; nil end
app/models/principal.rb
128 128
    to_s
129 129
  end
130 130

  
131
  def initials
132
    if firstname.blank? && lastname.blank?
133
      'NN'
134
    else
135
      "#{firstname.to_s.strip.first}#{lastname.to_s.strip.first}".upcase.strip
136
    end
137
  end
138

  
131 139
  def mail=(*args)
132 140
    nil
133 141
  end
app/views/common/_initials.svg.erb
1
<svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
2
<link rel="stylesheet" href="/stylesheets/avatars.css" type="text/css"/>
3
  <defs>
4
    <style>
5
      text {
6
        font-family: sans-serif;
7
      }
8
    </style>
9
  </defs>
10
  <%= content_tag :text, initials, x: 50, y: 50,
11
        'font-size': 50, fill: 'white',
12
        'dy': '.36em',
13
        'text-anchor': 'middle' %>
14
</svg>
public/stylesheets/application.css
1874 1874
	display: inline;
1875 1875
	opacity: 1;
1876 1876
}
1877

  
1878
img.avatar-color-0 {
1879
  background-color: #880000;
1880
}
1881

  
1882
img.avatar-color-1 {
1883
  background-color: #ff0000;
1884
}
1885

  
1886
img.avatar-color-2 {
1887
  background-color: #00ff00;
1888
}
1889

  
1890
img.avatar-color-3 {
1891
  background-color: #008800;
1892
}
1893

  
1894
img.avatar-color-4 {
1895
  background-color: #0000ff;
1896
}
1897

  
1898
img.avatar-color-5 {
1899
  background-color: #000088;
1900
}
1901

  
1902
img.avatar-color-6 {
1903
  background-color: #ff8800;
1904
}
1905

  
1906
img.avatar-color-7 {
1907
  background-color: #ff0088;
1908
}