From 99c11e2184fa586ae843b578d0eb52ec88636206 Mon Sep 17 00:00:00 2001 From: riking Date: Mon, 25 Aug 2014 10:38:20 -0700 Subject: [PATCH] Sort the badges on the user profile page Also clean up UserBadgesController so it isn't doing two things in one method --- .../discourse/controllers/user-badges.js.es6 | 3 +++ .../discourse/models/user_badge.js | 4 +-- app/controllers/user_badges_controller.rb | 25 +++++++++++-------- .../basic_user_badge_serializer.rb | 13 ++++++++++ config/routes.rb | 1 + 5 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 app/assets/javascripts/discourse/controllers/user-badges.js.es6 create mode 100644 app/serializers/basic_user_badge_serializer.rb diff --git a/app/assets/javascripts/discourse/controllers/user-badges.js.es6 b/app/assets/javascripts/discourse/controllers/user-badges.js.es6 new file mode 100644 index 00000000000..ca4887d09b7 --- /dev/null +++ b/app/assets/javascripts/discourse/controllers/user-badges.js.es6 @@ -0,0 +1,3 @@ +export default Ember.ArrayController.extend({ + sortProperties: ['grouping_position', 'badge.badgeType.id', 'badge.name', 'badge.id'] +}); diff --git a/app/assets/javascripts/discourse/models/user_badge.js b/app/assets/javascripts/discourse/models/user_badge.js index 7d594e7a2cb..9297f5e523c 100644 --- a/app/assets/javascripts/discourse/models/user_badge.js +++ b/app/assets/javascripts/discourse/models/user_badge.js @@ -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); diff --git a/app/controllers/user_badges_controller.rb b/app/controllers/user_badges_controller.rb index 797ea8f7448..7ee6555ab29 100644 --- a/app/controllers/user_badges_controller.rb +++ b/app/controllers/user_badges_controller.rb @@ -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 diff --git a/app/serializers/basic_user_badge_serializer.rb b/app/serializers/basic_user_badge_serializer.rb new file mode 100644 index 00000000000..5add50db591 --- /dev/null +++ b/app/serializers/basic_user_badge_serializer.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 77eceee0ecd..e375a258b25 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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",