mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 09:26:54 -06:00
Refactor BadgeGranter.update_trust_level_badges! -> update_badges.
This commit is contained in:
parent
b144b75565
commit
c07244a4e6
@ -447,7 +447,7 @@ class User < ActiveRecord::Base
|
||||
transaction do
|
||||
self.save!
|
||||
Group.user_trust_level_change!(self.id, self.trust_level)
|
||||
BadgeGranter.update_trust_level_badges!(self)
|
||||
BadgeGranter.update_badges(self, trust_level: trust_level)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -56,17 +56,20 @@ class BadgeGranter
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def self.update_trust_level_badges!(user)
|
||||
Badge.trust_level_badge_ids.each do |badge_id|
|
||||
user_badge = UserBadge.where(user_id: user.id, badge_id: badge_id).first
|
||||
if user_badge
|
||||
# Revoke the badge if the user is not supposed to have it.
|
||||
BadgeGranter.revoke(user_badge) if user.trust_level < badge_id
|
||||
else
|
||||
# Grant the badge if the user is supposed to have it.
|
||||
badge = Badge.find(badge_id)
|
||||
BadgeGranter.grant(badge, user) if user.trust_level >= badge_id
|
||||
def self.update_badges(user, opts={})
|
||||
if opts.has_key?(:trust_level)
|
||||
# Update trust level badges.
|
||||
trust_level = opts[:trust_level]
|
||||
Badge.trust_level_badge_ids.each do |badge_id|
|
||||
user_badge = UserBadge.find_by(user_id: user.id, badge_id: badge_id)
|
||||
if user_badge
|
||||
# Revoke the badge if trust level was lowered.
|
||||
BadgeGranter.revoke(user_badge) if trust_level < badge_id
|
||||
else
|
||||
# Grant the badge if trust level was increased.
|
||||
badge = Badge.find(badge_id)
|
||||
BadgeGranter.grant(badge, user) if trust_level >= badge_id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -18,7 +18,7 @@ class BoostTrustLevel
|
||||
@user.update_attributes!(trust_level: @level)
|
||||
end
|
||||
@logger.log_trust_level_change(@user, previous_level, @level)
|
||||
BadgeGranter.update_trust_level_badges!(@user)
|
||||
BadgeGranter.update_badges(@user, trust_level: @level)
|
||||
success
|
||||
end
|
||||
|
||||
|
@ -76,12 +76,12 @@ describe BadgeGranter do
|
||||
let(:logger) { StaffActionLogger.new(Fabricate(:admin)) }
|
||||
|
||||
it "is called by User#change_trust_level!" do
|
||||
BadgeGranter.expects(:update_trust_level_badges!)
|
||||
BadgeGranter.expects(:update_badges)
|
||||
user.change_trust_level!(:basic)
|
||||
end
|
||||
|
||||
it "is called by BoostTrustLevel#save!" do
|
||||
BadgeGranter.expects(:update_trust_level_badges!)
|
||||
BadgeGranter.expects(:update_badges)
|
||||
BoostTrustLevel.new(user: user, level: 1, logger: logger).save!
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user