Can the merit gem recalculate reputation and re-evaluate badges and ranks?

故事扮演 提交于 2020-01-11 05:44:09

问题


I would like to add a system of points, ranks and badges in my rails app.

The merit gem looks cool, but I want to make sure how it works before using it.

The way you define badges

# app/models/merit/badge_rules.rb
grant_on 'comments#vote', :badge => 'relevant-commenter', :to => :user do |comment|
  comment.votes.count == 5
end

and points

# app/models/merit/point_rules.rb
score 10, :to => :post_creator, :on => 'comments#create' do |comment|
  comment.title.present?
end

suggest that the action is done as a hook, just after the action (comments#vote or comments#create) in those cases. I havn't looked how the calculation/attribution of badges and points work yet so I am not sure.

As my app will evolve over time, I would like to be able to change the point and badges rules and re-evaluate them. Eg: Say I first decide to grant 10 points on account activation. I would like to be able to change it to 20 and then all activated profiles are re-evaluated and get a +10 point increase. Same thing for badges.

Does this gem support that ?


回答1:


It is possible to recompute reputation in an app with merit gem. Merit keeps the history of which actions triggered which point/badge granting in Merit::Action model, which is mapped to merit_actions table.

Following script should work (do a back up first, as I didn't do this in production yet):

# 1. Reset all badges/points granting
Merit::BadgesSash.delete_all
Merit::Score::Point.delete_all

# 1.1 Optionally reset activity log (badges/points granted/removed until now)
Merit::ActivityLog.delete_all

# 2. Mark all `merit_actions` as unprocessed
Merit::Action.all.map{|a| a.update_attribute :processed, false }

# 3. Recompute reputation rules
Merit::Action.check_unprocessed
Merit::RankRules.new.check_rank_rules

Notes on merit internals ("General merit workflow" wiki page).



来源:https://stackoverflow.com/questions/15793547/can-the-merit-gem-recalculate-reputation-and-re-evaluate-badges-and-ranks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!