Merge pull request #2692 from riking/sorted_badges

Sort the badges on the user profile page
This commit is contained in:
Robin Ward 2014-08-25 15:56:27 -04:00
commit c820c65172
6 changed files with 41 additions and 13 deletions

View File

@ -0,0 +1,3 @@
export default Ember.ArrayController.extend({
sortProperties: ['grouping_position', 'badge.badge_type.sort_order', 'badge.name', 'badge.id']
});

View File

@ -93,9 +93,9 @@ Discourse.UserBadge.reopenClass({
@returns {Promise} a promise that resolves to an array of `Discourse.UserBadge`.
**/
findByUsername: function(username, options) {
var url = "/user_badges.json?username=" + username;
var url = "/users/" + username + "/badges_json.json";
if (options && options.grouped) {
url += "&grouped=true";
url += "?grouped=true";
}
return Discourse.ajax(url).then(function(json) {
return Discourse.UserBadge.createFromJson(json);

View File

@ -1,27 +1,32 @@
class UserBadgesController < ApplicationController
def index
params.permit(:username).permit(:granted_before)
params.permit [:granted_before, :offset]
if params[:username]
user = fetch_user_from_params
user_badges = user.user_badges
else
badge = fetch_badge_from_params
user_badges = badge.user_badges.order('granted_at DESC, id DESC').limit(96)
end
badge = fetch_badge_from_params
user_badges = badge.user_badges.order('granted_at DESC, id DESC').limit(96)
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type, post: :topic)
if offset = params[:offset]
user_badges = user_badges.offset(offset.to_i)
end
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type, post: :topic)
render_serialized(user_badges, UserBadgeSerializer, root: "user_badges")
end
def username
params.permit [:grouped]
user = fetch_user_from_params
user_badges = user.user_badges
if params[:grouped]
user_badges = user_badges.group(:badge_id)
.select(UserBadge.attribute_names.map {|x| "MAX(#{x}) as #{x}" }, 'COUNT(*) as count')
end
render_serialized(user_badges, UserBadgeSerializer, root: "user_badges")
user_badges = user_badges.includes(badge: [:badge_grouping, :badge_type])
render_serialized(user_badges, BasicUserBadgeSerializer, root: "user_badges")
end
def create

View File

@ -1,3 +1,9 @@
class BadgeTypeSerializer < ApplicationSerializer
attributes :id, :name
attributes :id, :name, :sort_order
# change this if/when we allow custom badge types
# correct for now, though
def sort_order
10 - object.id
end
end

View File

@ -0,0 +1,13 @@
class BasicUserBadgeSerializer < ApplicationSerializer
attributes :id, :granted_at, :count, :grouping_position
has_one :badge
def include_count?
object.respond_to? :count
end
def grouping_position
object.badge.badge_grouping.position
end
end

View File

@ -235,6 +235,7 @@ Discourse::Application.routes.draw do
get "users/by-external/:external_id" => "users#show"
get "users/:username/flagged-posts" => "users#show", constraints: {username: USERNAME_ROUTE_FORMAT}
get "users/:username/deleted-posts" => "users#show", constraints: {username: USERNAME_ROUTE_FORMAT}
get "users/:username/badges_json" => "user_badges#username"
post "user_avatar/:username/refresh_gravatar" => "user_avatars#refresh_gravatar"
get "letter_avatar/:username/:size/:version.png" => "user_avatars#show_letter",