]> git.openstreetmap.org Git - rails.git/blob - app/helpers/user_helper.rb
Replace lookup_user_by_id with load_resource
[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 border border-grey"
6     options[:alt] ||= ""
7
8     if user.image_use_gravatar
9       user_gravatar_tag(user, options)
10     elsif user.avatar.attached?
11       user_avatar_variant_tag(user, { :resize_to_limit => [100, 100] }, options)
12     else
13       image_tag "avatar_large.png", options.merge(:width => 100, :height => 100)
14     end
15   end
16
17   def user_thumbnail(user, options = {})
18     options[:class] ||= "user_thumbnail border border-grey"
19     options[:alt] ||= ""
20
21     if user.image_use_gravatar
22       user_gravatar_tag(user, options.merge(:size => 50))
23     elsif user.avatar.attached?
24       user_avatar_variant_tag(user, { :resize_to_limit => [50, 50] }, options)
25     else
26       image_tag "avatar_small.png", options.merge(:width => 50, :height => 50)
27     end
28   end
29
30   def user_thumbnail_tiny(user, options = {})
31     options[:class] ||= "user_thumbnail_tiny border border-grey"
32     options[:alt] ||= ""
33
34     if user.image_use_gravatar
35       user_gravatar_tag(user, options.merge(:size => 50))
36     elsif user.avatar.attached?
37       user_avatar_variant_tag(user, { :resize_to_limit => [50, 50] }, options)
38     else
39       image_tag "avatar_small.png", options.merge(:width => 50, :height => 50)
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 => "align-text-bottom"
57   end
58
59   def auth_button(name, provider, options = {})
60     link_to(
61       image_tag("#{name}.svg",
62                 :alt => t("sessions.new.auth_providers.#{name}.alt"),
63                 :class => "rounded-3",
64                 :size => "36"),
65       auth_path(options.merge(:provider => provider)),
66       :method => :post,
67       :class => "auth_button",
68       :title => t("sessions.new.auth_providers.#{name}.title")
69     )
70   end
71
72   private
73
74   # Local avatar support
75   def user_avatar_variant_tag(user, variant_options, options)
76     if user.avatar.variable?
77       variant = user.avatar.variant(variant_options)
78       # https://stackoverflow.com/questions/61893089/get-metadata-of-active-storage-variant/67228171
79       if variant.send(:processed?)
80         metadata = variant.processed.send(:record).image.blob.metadata
81         if metadata["width"]
82           options[:width] = metadata["width"]
83           options[:height] = metadata["height"]
84         end
85       end
86       image_tag variant, options
87     else
88       image_tag user.avatar, options
89     end
90   end
91
92   def user_avatar_variant(user, options)
93     if user.avatar.variable?
94       user.avatar.variant(options)
95     else
96       user.avatar
97     end
98   end
99
100   # Gravatar support
101
102   # See http://en.gravatar.com/site/implement/images/ for details.
103   def user_gravatar_url(user, options = {})
104     size = options[:size] || 100
105     hash = Digest::MD5.hexdigest(user.email.downcase)
106     default_image_url = image_url("avatar_large.png")
107     "#{request.protocol}www.gravatar.com/avatar/#{hash}.jpg?s=#{size}&d=#{u(default_image_url)}"
108   end
109
110   def user_gravatar_tag(user, options = {})
111     url = user_gravatar_url(user, options)
112     options[:height] = options[:width] = options.delete(:size) || 100
113     image_tag url, options
114   end
115 end