diff --git a/app/assets/javascripts/admin/templates/user_badges.js.handlebars b/app/assets/javascripts/admin/templates/user_badges.js.handlebars index cc69a79b773..2401b9ceea9 100644 --- a/app/assets/javascripts/admin/templates/user_badges.js.handlebars +++ b/app/assets/javascripts/admin/templates/user_badges.js.handlebars @@ -27,8 +27,7 @@ - - + @@ -36,8 +35,7 @@ {{#each}} - - +
{{i18n admin.badges.name}}{{i18n admin.badges.badge_type}}{{i18n admin.badges.badge}} {{i18n admin.badges.granted_by}} {{i18n admin.badges.granted_at}}
{{badge.displayName}}{{badge.badge_type.name}}{{user-badge badge=badge}} {{#link-to 'adminUser' badge.granted_by}} {{avatar granted_by imageSize="tiny"}} diff --git a/app/assets/javascripts/discourse/controllers/badges_show_controller.js b/app/assets/javascripts/discourse/controllers/badges_show_controller.js new file mode 100644 index 00000000000..98c2e18fb3e --- /dev/null +++ b/app/assets/javascripts/discourse/controllers/badges_show_controller.js @@ -0,0 +1,22 @@ +/** + Controller for showing a particular badge. + + @class BadgesShowController + @extends Discourse.ObjectController + @namespace Discourse + @module Discourse +**/ +Discourse.BadgesShowController = Discourse.ObjectController.extend({ + grantDates: Em.computed.mapBy('userBadges', 'grantedAt'), + minGrantedAt: Em.computed.min('grantDates'), + + moreUserCount: function() { + if (this.get('userBadges')) { + return this.get('model.grant_count') - this.get('userBadges.length'); + } else { + return 0; + } + }.property('model.grant_count', 'userBadges.length'), + + showMoreUsers: Em.computed.gt('moreUserCount', 0) +}); diff --git a/app/assets/javascripts/discourse/models/user_badge.js b/app/assets/javascripts/discourse/models/user_badge.js index b19a3a25a7c..23b987e6002 100644 --- a/app/assets/javascripts/discourse/models/user_badge.js +++ b/app/assets/javascripts/discourse/models/user_badge.js @@ -53,6 +53,10 @@ Discourse.UserBadge.reopenClass({ userBadges = userBadges.map(function(userBadgeJson) { var userBadge = Discourse.UserBadge.create(userBadgeJson); + + var grantedAtDate = Date.parse(userBadge.get('granted_at')); + userBadge.set('grantedAt', grantedAtDate); + userBadge.set('badge', badges[userBadge.get('badge_id')]); if (userBadge.get('user_id')) { userBadge.set('user', users[userBadge.get('user_id')]); @@ -90,8 +94,13 @@ Discourse.UserBadge.reopenClass({ @param {String} badgeId @returns {Promise} a promise that resolves to an array of `Discourse.UserBadge`. **/ - findByBadgeId: function(badgeId) { - return Discourse.ajax("/user_badges.json?badge_id=" + badgeId).then(function(json) { + findByBadgeId: function(badgeId, options) { + if (!options) { options = {}; } + var url = "/user_badges.json?badge_id=" + badgeId; + if (options.granted_before) { + url = url + "&granted_before=" + encodeURIComponent(options.granted_before); + } + return Discourse.ajax(url).then(function(json) { return Discourse.UserBadge.createFromJson(json); }); }, diff --git a/app/assets/javascripts/discourse/routes/badges_show_route.js b/app/assets/javascripts/discourse/routes/badges_show_route.js index 88e022cb187..0db79c9df85 100644 --- a/app/assets/javascripts/discourse/routes/badges_show_route.js +++ b/app/assets/javascripts/discourse/routes/badges_show_route.js @@ -21,5 +21,16 @@ Discourse.BadgesShowRoute = Ember.Route.extend({ controller.set('userBadgesLoaded', true); }); controller.set('model', model); + }, + + actions: { + loadMore: function() { + var self = this; + Discourse.UserBadge.findByBadgeId(this.currentModel.get('id'), { + granted_before: this.get('controller.minGrantedAt') / 1000 + }).then(function(userBadges) { + self.get('controller.userBadges').pushObjects(userBadges); + }); + } } }); diff --git a/app/assets/javascripts/discourse/templates/badges/show.js.handlebars b/app/assets/javascripts/discourse/templates/badges/show.js.handlebars index 123ef0c3606..35638c72d85 100644 --- a/app/assets/javascripts/discourse/templates/badges/show.js.handlebars +++ b/app/assets/javascripts/discourse/templates/badges/show.js.handlebars @@ -16,17 +16,24 @@ {{#if userBadges}}

{{i18n users}}


+
{{#each userBadges}} - {{#link-to 'userActivity' user}} -
- {{avatar user imageSize="large"}} -
- {{user.username}} - {{unboundDate granted_at}} + {{#link-to 'userActivity' user}} +
+ {{avatar user imageSize="large"}} +
+ {{user.username}} + {{unboundDate granted_at}} +
-
- {{/link-to}} - {{/each}} + {{/link-to}} + {{/each}} +
+ {{#if showMoreUsers}} + + {{/if}} {{else}} {{#unless userBadgesLoaded}}
{{i18n loading}}
diff --git a/app/controllers/user_badges_controller.rb b/app/controllers/user_badges_controller.rb index 7d695063081..850a7889b56 100644 --- a/app/controllers/user_badges_controller.rb +++ b/app/controllers/user_badges_controller.rb @@ -1,14 +1,21 @@ class UserBadgesController < ApplicationController def index - params.permit(:username) + params.permit(:username).permit(:granted_before) + 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').limit(200) + user_badges = badge.user_badges.order('granted_at DESC').limit(100) end + + if params[:granted_before] + user_badges = user_badges.where('granted_at < ?', Time.at(params[:granted_before].to_f)) + end + user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type) + render_serialized(user_badges, UserBadgeSerializer, root: "user_badges") end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index b323cc32719..0628e3efd6c 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1717,6 +1717,7 @@ en: new_badge: New Badge new: New name: Name + badge: Badge display_name: Display Name description: Description badge_type: Badge Type