Merge pull request #4549 from LeoMcA/1-tl-badge

FIX: display only 1 trust level badge on user card
This commit is contained in:
Rafael dos Santos Silva 2016-11-14 19:40:00 -02:00 committed by GitHub
commit baa340db7f
2 changed files with 35 additions and 16 deletions

View File

@ -692,22 +692,27 @@ class User < ActiveRecord::Base
end end
def featured_user_badges(limit=3) def featured_user_badges(limit=3)
user_badges tl_badge_ids = Badge.trust_level_badge_ids
.group(:badge_id)
.select(UserBadge.attribute_names.map { |x| "MAX(user_badges.#{x}) AS #{x}" }, query = user_badges
'COUNT(*) AS "count"', .group(:badge_id)
'MAX(badges.badge_type_id) AS badges_badge_type_id', .select(UserBadge.attribute_names.map { |x| "MAX(user_badges.#{x}) AS #{x}" },
'MAX(badges.grant_count) AS badges_grant_count') 'COUNT(*) AS "count"',
.joins(:badge) 'MAX(badges.badge_type_id) AS badges_badge_type_id',
.order("CASE WHEN user_badges.badge_id = ( 'MAX(badges.grant_count) AS badges_grant_count')
SELECT MAX(ub2.badge_id) .joins(:badge)
FROM user_badges ub2 .order('badges_badge_type_id ASC, badges_grant_count ASC, badge_id DESC')
WHERE ub2.badge_id IN (#{Badge.trust_level_badge_ids.join(",")}) .includes(:user, :granted_by, { badge: :badge_type }, { post: :topic })
AND ub2.user_id = #{self.id}
) THEN 1 ELSE 0 END DESC") tl_badge = query.where("user_badges.badge_id IN (:tl_badge_ids)",
.order('badges_badge_type_id ASC, badges_grant_count ASC') tl_badge_ids: tl_badge_ids)
.includes(:user, :granted_by, { badge: :badge_type }, { post: :topic }) .limit(1)
.limit(limit)
other_badges = query.where("user_badges.badge_id NOT IN (:tl_badge_ids)",
tl_badge_ids: tl_badge_ids)
.limit(limit)
(tl_badge + other_badges).take(limit)
end end
def self.count_by_signup_date(start_date, end_date, group_id=nil) def self.count_by_signup_date(start_date, end_date, group_id=nil)

View File

@ -1346,4 +1346,18 @@ describe User do
end end
end end
end end
describe "#featured_user_badges" do
let(:user) { Fabricate(:user) }
let!(:user_badge_tl1) { UserBadge.create(badge_id: 1, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
let!(:user_badge_tl2) { UserBadge.create(badge_id: 2, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
it 'should display highest trust level badge first' do
expect(user.featured_user_badges[0].badge_id).to eq(2)
end
it 'should display only 1 trust level badge' do
expect(user.featured_user_badges.length).to eq(1)
end
end
end end