Backend changes to support improved badge pages.

This commit is contained in:
Vikhyat Korrapati 2014-06-28 00:38:03 +05:30
parent 386a45aab7
commit 2e52f795ea
4 changed files with 23 additions and 2 deletions

View File

@ -36,6 +36,13 @@ Discourse.UserBadge.reopenClass({
users[userJson.id] = Discourse.User.create(userJson);
});
// Create Topic objects.
if (json.topics === undefined) { json.topics = []; }
var topics = {};
json.topics.forEach(function(topicJson) {
topics[topicJson.id] = Discourse.Topic.create(topicJson);
});
// Create the badges.
if (json.badges === undefined) { json.badges = []; }
var badges = {};
@ -64,6 +71,9 @@ Discourse.UserBadge.reopenClass({
if (userBadge.get('granted_by_id')) {
userBadge.set('granted_by', users[userBadge.get('granted_by_id')]);
}
if (userBadge.get('topic_id')) {
userBadge.set('topic', topics[userBadge.get('topic_id')]);
}
return userBadge;
});

View File

@ -14,7 +14,7 @@ class UserBadgesController < ApplicationController
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)
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type, post: :topic)
if params[:grouped]
user_badges = user_badges.group(:badge_id).select(UserBadge.attribute_names.map {|x| "MAX(#{x}) as #{x}" }, 'COUNT(*) as count')

View File

@ -3,6 +3,7 @@ class UserBadge < ActiveRecord::Base
belongs_to :user
belongs_to :granted_by, class_name: 'User'
belongs_to :notification, dependent: :destroy
belongs_to :post
validates :badge_id, presence: true, uniqueness: {scope: :user_id}, if: 'badge.single_grant?'
validates :user_id, presence: true

View File

@ -1,11 +1,21 @@
class UserBadgeSerializer < ApplicationSerializer
attributes :id, :granted_at, :count
attributes :id, :granted_at, :count, :post_id
has_one :badge
has_one :user, serializer: BasicUserSerializer, root: :users
has_one :granted_by, serializer: BasicUserSerializer, root: :users
has_one :topic, serializer: BasicTopicSerializer
def include_count?
object.respond_to? :count
end
def include_post_id?
!object.post_id.nil?
end
alias :include_topic? :include_post_id?
def topic
object.post.topic
end
end