1

I am using thumbs_up gem for Rails and looking for how can you add more karma to a user programmatically? Or initialize a user with 100 karma?

ajbraus
  • 2,909
  • 3
  • 31
  • 45

2 Answers2

2

As you can see from the definition of karma, it is really just a weighted difference between the upvotes and downvotes on instances of models specified with has karma on the User model (or whatever model you have labeled acts_as_voter).

So you could create a KarmaAdjustment model with acts_as_voteable and add has_karma :karma_adjustment, :as => :submitter to your User model, then have fake instances of KarmaAdjustment created by the user whose karma you want to boost, and upvote them with a dummy user(s). I'm not sure of whether or not users can upvote something multiple times or not. If not, this method would be cumbersome, as you'd either need to weight each vote heavily, and/or create a bunch of dummy users to upvote with.

Alternatively, and probably preferably, you could patch the gem. Either fork it and modify the karma definition above, or just patch it on the fly with an initializer. I looked into creating an example of how you might go about doing this, but from what I've seen, it would be a fairly substantial fix. More than just a few quick lines, probably spanning a few files/classes. I'd suggest opening an issue on github to discuss this with the maintainer.

Greg
  • 773
  • 9
  • 22
1

Even easier, just adjust it as you display it! In your view for example,

<%= user.karma + 10 %>
bouchard
  • 820
  • 10
  • 27
  • Really a better answer would be to create a getter/setter method around the karma attribute with a variable of how much karma you want people to start with. – ajbraus Nov 26 '13 at 20:55