Grant system badges in a background job.

This commit is contained in:
Vikhyat Korrapati 2014-06-17 12:37:46 +05:30
parent b3f403952b
commit b5eea1d79f
3 changed files with 28 additions and 17 deletions

View File

@ -0,0 +1,25 @@
module Jobs
class UpdateBadges < Jobs::Base
def execute(args)
self.send(args[:action], args)
end
def trust_level_change(args)
user = User.find(args[:user_id])
trust_level = user.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
end

View File

@ -48,22 +48,8 @@ class BadgeGranter
end
end
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
def self.update_badges(args)
Jobs.enqueue(:update_badges, args)
end
end

View File

@ -60,7 +60,7 @@ class Promotion
@user.user_profile.recook_bio
@user.user_profile.save!
Group.user_trust_level_change!(@user.id, @user.trust_level)
BadgeGranter.update_badges(@user, trust_level: @user.trust_level)
BadgeGranter.update_badges(action: :trust_level_change, user_id: @user.id)
end
true