86990f9fb7ba4ea9e28c3eb49e858e7794c6ca78
[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(:resize => "100x100>"), options
12     elsif user.image.file?
13       image_tag user.image.url(:large), options
14     else
15       image_tag "avatar_large.png", options
16     end
17   end
18
19   def user_thumbnail(user, options = {})
20     options[:class] ||= "user_thumbnail"
21     options[:alt] ||= ""
22
23     if user.image_use_gravatar
24       user_gravatar_tag(user, options)
25     elsif user.avatar.attached?
26       image_tag user.avatar.variant(:resize => "50x50>"), options
27     elsif user.image.file?
28       image_tag user.image.url(:small), options
29     else
30       image_tag "avatar_small.png", options
31     end
32   end
33
34   def user_thumbnail_tiny(user, options = {})
35     options[:class] ||= "user_thumbnail_tiny"
36     options[:alt] ||= ""
37
38     if user.image_use_gravatar
39       user_gravatar_tag(user, options)
40     elsif user.avatar.attached?
41       image_tag user.avatar.variant(:resize => "50x50>"), options
42     elsif user.image.file?
43       image_tag user.image.url(:small), options
44     else
45       image_tag "avatar_small.png", options
46     end
47   end
48
49   def user_image_url(user, options = {})
50     if user.image_use_gravatar
51       user_gravatar_url(user, options)
52     elsif user.avatar.attached?
53       polymorphic_url(user.avatar.variant(:resize => "100x100>"))
54     elsif user.image.file?
55       image_url(user.image.url(:large))
56     else
57       image_url("avatar_large.png")
58     end
59   end
60
61   # External authentication support
62
63   def openid_logo
64     image_tag "openid_small.png", :alt => t("users.login.openid_logo_alt"), :class => "openid_logo"
65   end
66
67   def auth_button(name, provider, options = {})
68     link_to(
69       image_tag("#{name}.png", :alt => t("users.login.auth_providers.#{name}.alt")),
70       auth_path(options.merge(:provider => provider)),
71       :class => "auth_button",
72       :title => t("users.login.auth_providers.#{name}.title")
73     )
74   end
75
76   private
77
78   # Gravatar support
79
80   # See http://en.gravatar.com/site/implement/images/ for details.
81   def user_gravatar_url(user, options = {})
82     size = options[:size] || 100
83     hash = Digest::MD5.hexdigest(user.email.downcase)
84     default_image_url = image_url("avatar_large.png")
85     "#{request.protocol}www.gravatar.com/avatar/#{hash}.jpg?s=#{size}&d=#{u(default_image_url)}"
86   end
87
88   def user_gravatar_tag(user, options = {})
89     url = user_gravatar_url(user, options)
90     options.delete(:size)
91     image_tag url, options
92   end
93 end