Skip to content

Instantly share code, notes, and snippets.

@paydro
Created February 2, 2015 20:47
Show Gist options
  • Save paydro/557c5d2fe75e466c3b20 to your computer and use it in GitHub Desktop.
Save paydro/557c5d2fe75e466c3b20 to your computer and use it in GitHub Desktop.
Discourse plugin patches to use custom Avatar URL instead of Discourse's avatar system
var safe = Handlebars.SafeString;
Em.Handlebars.helper('bound-avatar', function(user, size, uploadId) {
if (Em.isEmpty(user)) {
return new safe("<div class='avatar-placeholder'></div>");
}
var username = Em.get(user, 'username');
if(arguments.length < 4){
uploadId = Em.get(user, 'uploaded_avatar_id');
}
return new safe(Discourse.Utilities.avatarImg({
size: size,
avatarTemplate: user.uploaded_avatar_url
}));
}, 'username', 'uploaded_avatar_id');
require Rails.root.join("app/models/user").to_s
class ::User
def uploaded_avatar_url
uploaded_avatar.try(:url) || ''
end
end
# user serializer changes to include User#uploaded_avatar_url
require Rails.root.join("app/serializers/current_user_serializer").to_s
class ::CurrentUserSerializer
attributes :uploaded_avatar_url
def uploaded_avatar_url
object.uploaded_avatar_url
end
end
require Rails.root.join("app/serializers/basic_user_serializer").to_s
class ::BasicUserSerializer < ::ApplicationSerializer
attributes :uploaded_avatar_url
def uploaded_avatar_url
if Hash === object
user.uploaded_avatar_url
else
object.uploaded_avatar_url
end
end
end
require Rails.root.join("app/serializers/user_serializer").to_s
class ::UserSerializer < ::BasicUserSerializer
attributes :uploaded_avatar_url
def uploaded_avatar_url
object.uploaded_avatar_url
end
end
require Rails.root.join("app/serializers/basic_post_serializer").to_s
class ::BasicPostSerializer
attributes :uploaded_avatar_url
def uploaded_avatar_url
object.user.uploaded_avatar_url
end
end
require Rails.root.join("app/serializers/post_serializer").to_s
class ::PostSerializer
attributes :uploaded_avatar_url
def reply_to_user
{
username: object.reply_to_user.username,
avatar_template: object.reply_to_user.avatar_template,
uploaded_avatar_id: object.reply_to_user.uploaded_avatar_id,
uploaded_avatar_url: object.reply_to_user.uploaded_avatar_url
}
end
end
require Rails.root.join("app/serializers/post_action_user_serializer").to_s
class ::PostActionUserSerializer
attributes :uploaded_avatar_url
def uploaded_avatar_url
object.user.uploaded_avatar_url
end
end
require Rails.root.join("app/serializers/admin_post_serializer").to_s
class ::AdminPostSerializer
attributes :uploaded_avatar_url
def uploaded_avatar_url
object.user.uploaded_avatar_url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment