Refactor BadgeGranter.update_trust_level_badges! -> update_badges.

This commit is contained in:
Vikhyat Korrapati 2014-05-14 21:01:41 +05:30
parent b144b75565
commit c07244a4e6
4 changed files with 18 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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