Merit gem cron job检查并删除临时徽章

时间:2014-08-25 17:27:46

标签: ruby-on-rails merit-gem

我有这样的徽章规则;

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

发送评论后,用户将丢失徽章。但如果他/她没有发表评论,他/她将永远拥有徽章。

是否可以检查临时徽章并删除那些无效徽章?

1 个答案:

答案 0 :(得分:1)

无法从优点规则运行,因为comment局部变量不会 在那里从一个cron工作。我要做的是将逻辑移动到用户模型,然后 从规则和cron工作中调用它:

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