0

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?

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185

1 Answers1

1

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.

TuteC
  • 4,342
  • 30
  • 40
  • thanks @TuteC, I had seen that SO answer previously but had perhaps misunderstood the subsequent comments. Thanks for clarifying!! – Andy Harvey May 05 '16 at 02:27
  • hi @tuteC, what format should `Merit::Action.target_model` be? Is it a pluralized lowercased representation of the model (e.g. comments)? I am having trouble generating points with this method. Running `Merit::Action.check_unprocessed` is correctly assigning badges, and adding records to `Merit::Score`, but `Merit::Score::Point` remains empty. I assume this is not the correct outcome, but can't seem to figure out the cause. – Andy Harvey May 05 '16 at 03:51
  • never mind, I needed to clear previous point definitions from memory `Merit::AppPointRules = Merit::PointRules.new.defined_rules`. all is now working. thanks for a great gem!! – Andy Harvey May 05 '16 at 04:33