From 650e9348f3f8d7cf9dea56e50baedd310f454332 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 4 Jul 2014 17:40:44 +1000 Subject: [PATCH] FIX: multiple grant not working correctly --- app/jobs/regular/update_badges.rb | 2 +- app/services/badge_granter.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/jobs/regular/update_badges.rb b/app/jobs/regular/update_badges.rb index 702d876ca..5895835ce 100644 --- a/app/jobs/regular/update_badges.rb +++ b/app/jobs/regular/update_badges.rb @@ -26,7 +26,7 @@ module Jobs user = post.user # Grant "Welcome" badge to the user if they do not already have it. - BadgeGranter.grant(Badge.find(5), user) + BadgeGranter.grant(Badge.find(Badge::Welcome), user) Badge.like_badge_counts.each do |badge_id, count| if post.like_count >= count diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index dfec5d9c9..33d01fc6b 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -13,7 +13,13 @@ class BadgeGranter def grant return if @granted_by and !Guardian.new(@granted_by).can_grant_badges?(@user) - user_badge = UserBadge.find_by(badge_id: @badge.id, user_id: @user.id, post_id: @post_id) + find_by = { badge_id: @badge.id, user_id: @user.id } + + if @badge.multiple_grant? + find_by[:post_id] = @post_id + end + + user_badge = UserBadge.find_by(find_by) if user_badge.nil? || (@badge.multiple_grant? && @post_id.nil?) UserBadge.transaction do