From 4e89b64e64439b72bdadfc968a5fb7a413b33f53 Mon Sep 17 00:00:00 2001 From: Vikhyat Korrapati Date: Tue, 22 Apr 2014 15:10:47 +0530 Subject: [PATCH] Load 100 users at a time for the badge page, with a button to load more. --- .../controllers/badges_show_controller.js | 22 ++++++++++++++++ .../discourse/models/user_badge.js | 13 ++++++++-- .../discourse/routes/badges_show_route.js | 11 ++++++++ .../templates/badges/show.js.handlebars | 25 ++++++++++++------- app/controllers/user_badges_controller.rb | 11 ++++++-- 5 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 app/assets/javascripts/discourse/controllers/badges_show_controller.js 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