Skip to content

Commit bfedc7c

Browse files
committed
Allow configuration of avatar URL generation; remove obsolete template (current_user)
1 parent b9c39e9 commit bfedc7c

11 files changed

+29
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Kandan.Helpers.Avatars
2+
@urlFor: (a, options) ->
3+
size = options.size || 30
4+
fallback = options.fallback || Kandan.options.avatarFallback || 'mm'
5+
avatarHash = a.gravatar_hash || a.get('user').gravatar_hash || a.get('gravatarHash')
6+
Kandan.options.avatarUrl.replace(/%{hash}/, avatarHash).
7+
replace(/%{size}/, size).
8+
replace(/%{fallback}/, fallback)

app/assets/javascripts/backbone/kandan.js.coffee.erb

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ window.Kandan =
2121
perPage : <%= Kandan::Config.options[:per_page] %>
2222
nowThreshold: 3000
2323
timestampRefreshInterval: 2000
24+
avatarUrl: "<%= Kandan::Config.options[:avatar_url] %>"
25+
avatarFallback: "<%= Kandan::Config.options[:avatar_fallback] %>"
2426

2527

2628
# TODO this is a helper method to register plugins

app/assets/javascripts/backbone/plugins/user_list.js.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Kandan.Plugins.UserList
66

77
@template: _.template '''
88
<div class="user clearfix">
9-
<img class="avatar" src="http://gravatar.com/avatar/<%= gravatarHash %>?s=25"/>
9+
<img class="avatar" src="<%= avatarUrl %>"/>
1010
<span class="name"><%= name %></span>
1111
</div>
1212
'''
@@ -22,7 +22,7 @@ class Kandan.Plugins.UserList
2222

2323
$users.append @template({
2424
name: displayName,
25-
gravatarHash: user.gravatar_hash
25+
avatarUrl: Kandan.Helpers.Avatars.urlFor(user, {size: 25})
2626
})
2727
$el.html($users)
2828

app/assets/javascripts/backbone/views/show_activity.js.coffee

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Kandan.Views.ShowActivity extends Backbone.View
66
render: ()->
77
activity = @options.activity.toJSON()
88
activity.content = _.escape(activity.content)
9+
activity.avatarUrl = Kandan.Helpers.Avatars.urlFor(@options.activity, {size: 30})
910
if activity.action != "message"
1011
@compiledTemplate = JST['user_notification']({activity: activity})
1112
else
@@ -20,10 +21,10 @@ class Kandan.Views.ShowActivity extends Backbone.View
2021

2122
user_mention_regex = new RegExp("@#{Kandan.Helpers.Users.currentUser().username}\\b")
2223
all_mention_regex = new RegExp("@all")
23-
24+
2425
if activity.user.id == Kandan.Helpers.Users.currentUser().id
2526
$(@el).addClass("current_user")
26-
27+
2728
if user_mention_regex.test(@compiledTemplate) || all_mention_regex.test(@compiledTemplate)
2829
$(@el).addClass("mentioned_user")
2930

app/assets/templates/activity_base.jst.eco

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<span class="posted_at">
22
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
33
</span>
4-
<img class="avatar" src="http://gravatar.com/avatar/<%= @activity.user.gravatar_hash %>?s=30"/>
4+
<img class="avatar" src="<%= @activity.avatarUrl %>"/>
55

66
<div class="readable">
77
<div class="content">

app/assets/templates/current_user.jst.eco

-2
This file was deleted.

app/assets/templates/message.jst.eco

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<span class="posted_at">
22
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
33
</span>
4-
<img class="avatar" src="http://gravatar.com/avatar/<%= @activity.user.gravatar_hash %>?s=30"/>
4+
<img class="avatar" src="<%= @activity.avatarUrl %>"/>
55

66
<div class="readable">
77
<span class="user">

app/assets/templates/user_notification.jst.eco

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<span class="posted_at">
22
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
33
</span>
4-
<img class="avatar" src="http://gravatar.com/avatar/<%= @activity.user.gravatar_hash %>?s=30"/>
4+
<img class="avatar" src="<%= @activity.avatarUrl %>"/>
55

66
<div class="readable">
77
<span class="user">Kandan bot</span>

app/helpers/avatar_helper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module AvatarHelper
2+
def avatar_url_for(user, options = {})
3+
Kandan::Config.options[:avatar_url].gsub(/%{hash}/, user.gravatar_hash).
4+
gsub(/%{size}/, (options[:size] || 30).to_s).
5+
gsub(/%{fallback}/, options[:fallback] || Kandan::Config.options[:avatar_fallback] || 'mm')
6+
end
7+
end

app/views/layouts/application.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<a href="#" class="user_menu_link">
2323
<cite class="user_flag"></cite>
2424
<div class="user">
25-
<img src="http://gravatar.com/avatar/<%= current_user.gravatar_hash %>?s=25"/>
25+
<img src="<%= avatar_url_for(current_user, :size => 25) %>"/>
2626
<span><%= current_user.full_name_or_username %></span>
2727
</div>
2828
</a>

config/kandan_settings.yml

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
:max_rooms: 99
1414

1515
:public_site: true
16+
17+
:avatar_url: http://gravatar.com/avatar/%{hash}?s=%{size}&d=%{fallback}
18+
:avatar_fallback: identicon

0 commit comments

Comments
 (0)