Merge remote-tracking branch 'upstream/pull/3217'
[rails.git] / app / helpers / user_helper.rb
1 module UserHelper
2   # User images
3
4   def user_image(user, options = {})
5     options[:class] ||= "user_image"
6     options[:alt] ||= ""
7
8     if user.image_use_gravatar
9       user_gravatar_tag(user, options)
10     elsif user.avatar.attached?
11       image_tag user_avatar_variant(user, :resize_to_limit => [100, 100]), options
12     else
13       image_tag "avatar_large.png", options
14     end
15   end
16
17   def user_thumbnail(user, options = {})
18     options[:class] ||= "user_thumbnail"
19     options[:alt] ||= ""
20
21     if user.image_use_gravatar
22       user_gravatar_tag(user, options)
23     elsif user.avatar.attached?
24       image_tag user_avatar_variant(user, :resize_to_limit => [50, 50]), options
25     else
26       image_tag "avatar_small.png", options
27     end
28   end
29
30   def user_thumbnail_tiny(user, options = {})
31     options[:class] ||= "user_thumbnail_tiny"
32     options[:alt] ||= ""
33
34     if user.image_use_gravatar
35       user_gravatar_tag(user, options)
36     elsif user.avatar.attached?
37       image_tag user_avatar_variant(user, :resize_to_limit => [50, 50]), options
38     else
39       image_tag "avatar_small.png", options
40     end
41   end
42
43   def user_image_url(user, options = {})
44     if user.image_use_gravatar
45       user_gravatar_url(user, options)
46     elsif user.avatar.attached?
47       polymorphic_url(user_avatar_variant(user, :resize_to_limit => [100, 100]), :host => Settings.server_url)
48     else
49       image_url("avatar_large.png")
50     end
51   end
52
53   # External authentication support
54
55   def openid_logo
56     image_tag "openid_small.png", :alt => t("sessions.new.openid_logo_alt"), :class => "openid_logo"
57   end
58
59   def auth_button(name, provider, options = {})
60     link_to(
61       image_tag("#{name}.svg", :alt => t("sessions.new.auth_providers.#{name}.alt"), :class => "rounded-lg"),
62       auth_path(options.merge(:provider => provider)),
63       :method => :post,
64       :class => "auth_button",
65       :title => t("sessions.new.auth_providers.#{name}.title")
66     )
67   end
68
69   private
70
71   # Local avatar support
72
73   def user_avatar_variant(user, options)
74     if user.avatar.variable?
75       user.avatar.variant(options)
76     else
77       user.avatar
78     end
79   end
80
81   # Gravatar support
82
83   # See http://en.gravatar.com/site/implement/images/ for details.
84   def user_gravatar_url(user, options = {})
85     size = options[:size] || 100
86     hash = Digest::MD5.hexdigest(user.email.downcase)
87     default_image_url = image_url("avatar_large.png")
88     "#{request.protocol}www.gravatar.com/avatar/#{hash}.jpg?s=#{size}&d=#{u(default_image_url)}"
89   end
90
91   def user_gravatar_tag(user, options = {})
92     url = user_gravatar_url(user, options)
93     options.delete(:size)
94     image_tag url, options
95   end
96 end