Merge pull request #2290 from vikhyat/badge-system

Incrementally load users in the badge page
This commit is contained in:
Régis Hanol 2014-04-24 12:14:41 +02:00
commit d1b96d0e8b
7 changed files with 72 additions and 17 deletions

View File

@ -27,8 +27,7 @@
<table>
<tr>
<th>{{i18n admin.badges.name}}</th>
<th>{{i18n admin.badges.badge_type}}</th>
<th>{{i18n admin.badges.badge}}</th>
<th>{{i18n admin.badges.granted_by}}</th>
<th>{{i18n admin.badges.granted_at}}</th>
<th></th>
@ -36,8 +35,7 @@
{{#each}}
<tr>
<td>{{badge.displayName}}</td>
<td>{{badge.badge_type.name}}</td>
<td>{{user-badge badge=badge}}</td>
<td>
{{#link-to 'adminUser' badge.granted_by}}
{{avatar granted_by imageSize="tiny"}}

View File

@ -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)
});

View File

@ -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);
});
},

View File

@ -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);
});
}
}
});

View File

@ -16,17 +16,24 @@
{{#if userBadges}}
<h2>{{i18n users}}</h2>
<br>
<div>
{{#each userBadges}}
{{#link-to 'userActivity' user}}
<div class="badge-user">
{{avatar user imageSize="large"}}
<div class="details">
<span class="username">{{user.username}}</span>
{{unboundDate granted_at}}
{{#link-to 'userActivity' user}}
<div class="badge-user">
{{avatar user imageSize="large"}}
<div class="details">
<span class="username">{{user.username}}</span>
{{unboundDate granted_at}}
</div>
</div>
</div>
{{/link-to}}
{{/each}}
{{/link-to}}
{{/each}}
</div>
{{#if showMoreUsers}}
<button class="btn" {{action loadMore}}>
{{countI18n badges.more_badges count=moreUserCount}}
</button>
{{/if}}
{{else}}
{{#unless userBadgesLoaded}}
<div class='spinner'>{{i18n loading}}</div>

View File

@ -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

View File

@ -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