diff --git a/app/models/user_badge.rb b/app/models/user_badge.rb index 5781bd6527c..24532195171 100644 --- a/app/models/user_badge.rb +++ b/app/models/user_badge.rb @@ -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 diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index 35c1f228fc4..f8983e4091d 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -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