ruby-on-railsmerit-gem

How to 'back calculate' Merit points?


I'm trying to add a reputation system into an existing app using Merit.

I want to back calculate points for all users based on their historical activity. Merit has quite a complicated internal structure with several tables, and I think I may be missing some important steps. Below is what I have so far, and I'm grateful for pointers from anyone more familiar with this gem.

To keep things simple, let's say I want to

score 2, to: :user, on: 'comments#create'

and that I have a Comment table full of records that look something like this

Comment id: 56, content: "My comment", user_id: 247, commentable_id: 2, commentable_type: 'Object', created_at: "2016-04-04 08:56:17", updated_at: "2016-04-04 17:03:55">

My current approach looks like this

Comment.all.each do |c|
  score_point = Merit::Score::Point.create( num_point: 2, log: nil, created_at: c.created_at)
  action = Merit::Action.create(user_id: c.user_id, action_method: 'create', action_value: nil, had_errors: false, target_model: c.class.name, target_id: c.id, processed: true, created_at: c.created_at)
  activity_log = Merit::ActivityLog.create( action_id: action.id, related_change_type: "Merit::Score::Point", related_change_id: score_point.id, description: "", created_at: c.created_at) 
end

There is also a Merit::Score and Merit::Sash table, but I'm not clear on what these do and whether these records also need to be created.

Is there anything else that I have overlooked?

As an aside, what is the purpose of Merit::Action.action_value, Merit::ActivityLog.description and Merit::Score::Point.log, or are these simply free text strings?


Solution

  • Merit has the ability to recompute reputation for every user from it's Merit::Action. In this StackOverflow answer you can see how an existing application using merit might:

    1. Delete all the logs, badges and points granted related to a user
    2. Mark all the actions as processed: false
    3. Recompute the now-unprocessed rules

    In your particular case, the only Merit model you'd want to create is action, and initializing it as processed: false instead of true. Merit::Action.check_unprocessed should then recreate the reputation you seek.

    This wiki article that explains merit internals might help you understand the logic behind these steps.


    Merit::Action.action_value, Merit::ActivityLog.description, and Merit::Score::Point.log have no meaningful behavior in merit, and you can use them if needed.