twitter/activerecord-reputation-system

Name: activerecord-reputation-system

Owner: Twitter, Inc.

Description: An Active Record Reputation System for Rails

Created: 2012-05-17 11:43:53.0

Updated: 2018-01-11 13:39:45.0

Pushed: 2016-01-14 23:40:36.0

Homepage:

Size: 745

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ActiveRecord Reputation System Build Status Code Climate

The Active Record Reputation System helps you build the reputation system for your Rails application. It allows Active Record to have reputations and get evaluated by other records. This gem allows you to:

Installation

Add to Gemfile:

'activerecord-reputation-system'

Run:

le install
s generate reputation_system
 db:migrate
Quick Start

Let's say we want to keep track of user karma in Q&A site where user karma is sum of questioning skill and answering skill. Questioning skill is sum of votes for user's questions and Answering skill is sum of average rating of user's answers. This can be defined as follow:

s User < ActiveRecord::Base
s_many :answers
s_many :questions

s_reputation :karma,
  :source => [
      { :reputation => :questioning_skill, :weight => 0.8 },
      { :reputation => :answering_skill }]

s_reputation :questioning_skill,
  :source => { :reputation => :votes, :of => :questions }

s_reputation :answering_skill,
  :source => { :reputation => :avg_rating, :of => :answers }


s Answer < ActiveRecord::Base
longs_to :user, :as => :author

s_reputation :avg_rating,
  :source => :user,
  :aggregated_by => :average,
  :source_of => [{ :reputation => :answering_skill, :of => :author }]



s Question < ActiveRecord::Base
longs_to :user

s_reputation :votes,
  :source => :user

Once reputation system is defined, evaluations for answers and questions can be added as follow:

wer.add_evaluation(:avg_rating, 3, @user)
stion.add_evaluation(:votes, 1, @user)

Reputation value can be accessed as follow:

wer.reputation_for(:avg_rating) #=> 3
stion.reputation_for(:votes) #=> 1
r.reputation_for(:karma)

You can query for records using reputation value:

.find_with_reputation(:karma, :all, { :condition => 'karma > 10' })

You can get source records that have evaluated the target record:

stion.evaluators_for(:votes) #=> [@user]

You can get target records that have been evaluated by a given source record:

tion.evaluated_by(:votes, @user) #=> [@question]

To use a custom aggregation function you need to provide the name of the method on the :aggregated_by option, and implement this method on the model. On the example below, our aggregation function sums all values and multiply by ten:

s Answer < ActiveRecord::Base
longs_to :author, :class_name => 'User'
longs_to :question

s_reputation :custom_rating,
:source => :user,
:aggregated_by => :custom_aggregation

f custom_aggregation(*args)
rep, source, weight = args[0..2]

# Ruby doesn't support method overloading, so let's handle parameters on a condition

# For a new source, these are the input parameters:
# rep, source, weight
if args.length == 3
  rep.value + weight * source.value * 10

# For an updated source, these are the input parameters:
# rep, source, weight, oldValue, newSize
elsif args.length == 5
  oldValue, newSize = args[3..4]
  rep.value + (source.value - oldValue) * 10
end
d

Documentation

Please refer Wiki for available APIs and more information.

Authors

Katsuya Noguchi

Related Links
Versioning

For transparency and insight into our release cycle, releases will be numbered with the follow format:

<major>.<minor>.<patch>

And constructed with the following guidelines:

For more information on semantic versioning, please visit http://semver.org/.

License

Copyright 2012 Twitter, Inc.

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.