I have a badge rule like this;
grant_on 'comments#create', badge: 'frequent-commenter', to: :user, temporary: true do |comment|
comment.user.comments.where(:created_at.gte => (Date.today - 30)).count >= 20
end
User will lose the badge after sent a comment. But s/he will have the badge forever if s/he doesn't send a comment.
Is it possible to check temporary badges and remove the ones which are not valid anymore?
Can't run from the merit rules, as the comment
local variable won't be
there from a cron job. What I'd do is move the logic to the user model, and then
call it both from the rule and from the cron job:
grant_on 'comments#create', badge: 'frequent-commenter', to: :user,
temporary: true do |comment|
comment.user.is_frequent_commenter?
end
class User
def is_frequent_commenter?
comments.where(:created_at.gte => (Date.today - 30)).count >= 20
end
end
class RemoveInvalidBadgesJob
def perform
# Better done by direct database query?
User.all.reject(&:is_frequent_commenter?).map do |user|
user.rm_badge(1)
end
end
end