Move badge grant_count updating to the UserBadge model callbacks.

This commit is contained in:
Vikhyat Korrapati 2014-06-17 20:41:52 +05:30
parent a68b47cb9f
commit b3f403952b
2 changed files with 8 additions and 2 deletions

View File

@ -8,6 +8,14 @@ class UserBadge < ActiveRecord::Base
validates :user_id, presence: true
validates :granted_at, presence: true
validates :granted_by, presence: true
after_create do
Badge.increment_counter 'grant_count', self.badge_id
end
after_destroy do
Badge.decrement_counter 'grant_count', self.badge_id
end
end
# == Schema Information

View File

@ -19,7 +19,6 @@ class BadgeGranter
user_badge = UserBadge.create!(badge: @badge, user: @user,
granted_by: @granted_by, granted_at: Time.now)
Badge.increment_counter 'grant_count', @badge.id
if @granted_by != Discourse.system_user
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
end
@ -37,7 +36,6 @@ class BadgeGranter
def self.revoke(user_badge, options={})
UserBadge.transaction do
user_badge.destroy!
Badge.decrement_counter 'grant_count', user_badge.badge_id
if options[:revoked_by]
StaffActionLogger.new(options[:revoked_by]).log_badge_revoke(user_badge)
end