Sort the badges on the user profile page

Also clean up UserBadgesController so it isn't doing two things in one
method
This commit is contained in:
riking 2014-08-25 10:38:20 -07:00
parent 9b86d1507d
commit 99c11e2184
5 changed files with 34 additions and 12 deletions

View File

@ -0,0 +1,3 @@
export default Ember.ArrayController.extend({
sortProperties: ['grouping_position', 'badge.badgeType.id', '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`. @returns {Promise} a promise that resolves to an array of `Discourse.UserBadge`.
**/ **/
findByUsername: function(username, options) { findByUsername: function(username, options) {
var url = "/user_badges.json?username=" + username; var url = "/users/" + username + "/badges_json.json";
if (options && options.grouped) { if (options && options.grouped) {
url += "&grouped=true"; url += "?grouped=true";
} }
return Discourse.ajax(url).then(function(json) { return Discourse.ajax(url).then(function(json) {
return Discourse.UserBadge.createFromJson(json); return Discourse.UserBadge.createFromJson(json);

View File

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

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/by-external/:external_id" => "users#show"
get "users/:username/flagged-posts" => "users#show", constraints: {username: USERNAME_ROUTE_FORMAT} 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/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" post "user_avatar/:username/refresh_gravatar" => "user_avatars#refresh_gravatar"
get "letter_avatar/:username/:size/:version.png" => "user_avatars#show_letter", get "letter_avatar/:username/:size/:version.png" => "user_avatars#show_letter",