mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 13:43:16 +08:00
Backend changes to support improved badge pages.
This commit is contained in:
parent
386a45aab7
commit
2e52f795ea
|
@ -36,6 +36,13 @@ Discourse.UserBadge.reopenClass({
|
||||||
users[userJson.id] = Discourse.User.create(userJson);
|
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.
|
// Create the badges.
|
||||||
if (json.badges === undefined) { json.badges = []; }
|
if (json.badges === undefined) { json.badges = []; }
|
||||||
var badges = {};
|
var badges = {};
|
||||||
|
@ -64,6 +71,9 @@ Discourse.UserBadge.reopenClass({
|
||||||
if (userBadge.get('granted_by_id')) {
|
if (userBadge.get('granted_by_id')) {
|
||||||
userBadge.set('granted_by', users[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;
|
return userBadge;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ class UserBadgesController < ApplicationController
|
||||||
user_badges = user_badges.where('granted_at < ?', Time.at(params[:granted_before].to_f))
|
user_badges = user_badges.where('granted_at < ?', Time.at(params[:granted_before].to_f))
|
||||||
end
|
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]
|
if params[:grouped]
|
||||||
user_badges = user_badges.group(:badge_id).select(UserBadge.attribute_names.map {|x| "MAX(#{x}) as #{x}" }, 'COUNT(*) as count')
|
user_badges = user_badges.group(:badge_id).select(UserBadge.attribute_names.map {|x| "MAX(#{x}) as #{x}" }, 'COUNT(*) as count')
|
||||||
|
|
|
@ -3,6 +3,7 @@ class UserBadge < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :granted_by, class_name: 'User'
|
belongs_to :granted_by, class_name: 'User'
|
||||||
belongs_to :notification, dependent: :destroy
|
belongs_to :notification, dependent: :destroy
|
||||||
|
belongs_to :post
|
||||||
|
|
||||||
validates :badge_id, presence: true, uniqueness: {scope: :user_id}, if: 'badge.single_grant?'
|
validates :badge_id, presence: true, uniqueness: {scope: :user_id}, if: 'badge.single_grant?'
|
||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
|
|
|
@ -1,11 +1,21 @@
|
||||||
class UserBadgeSerializer < ApplicationSerializer
|
class UserBadgeSerializer < ApplicationSerializer
|
||||||
attributes :id, :granted_at, :count
|
attributes :id, :granted_at, :count, :post_id
|
||||||
|
|
||||||
has_one :badge
|
has_one :badge
|
||||||
has_one :user, serializer: BasicUserSerializer, root: :users
|
has_one :user, serializer: BasicUserSerializer, root: :users
|
||||||
has_one :granted_by, serializer: BasicUserSerializer, root: :users
|
has_one :granted_by, serializer: BasicUserSerializer, root: :users
|
||||||
|
has_one :topic, serializer: BasicTopicSerializer
|
||||||
|
|
||||||
def include_count?
|
def include_count?
|
||||||
object.respond_to? :count
|
object.respond_to? :count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def include_post_id?
|
||||||
|
!object.post_id.nil?
|
||||||
|
end
|
||||||
|
alias :include_topic? :include_post_id?
|
||||||
|
|
||||||
|
def topic
|
||||||
|
object.post.topic
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user