Scope Reputations

Katsuya Noguchi edited this page Oct 4, 2012 · 1 revision

Reputations can have different scopes to provide additional context.

For example, let's say question has a reputation called difficulty. You might want to keep track of that reputation by country, such that each question has a different difficulty rating in each country.

Scopes can be defined like this:

has_reputation :name,
    ...
    :scopes => [:scope1, :scope2, ...]

Once scopes are defined, evaluations can be added in the context of defined scopes:

add_evaluation(:reputation_name, evaluation_value, source, :scope)

Also, reputations can be accessed in the context of scopes:

reptuation_for(:reputation_name, :scope)

To use a scoped reputation as a source in another reputation, try this:

has_reputation :rep1,
    :source => {:reputation => :rep2, :scope => :scope1}
    ...
has_reputation :rep2,
    ...
    :scopes => [:scope1, :scope2, ...],
    :source_of => {:reputation => :rep1, :defined_for_scope => [:scope1]}

To execute an Active Record query using a scoped reputation, try this:

ActiveRecord::Base.find_with_reputation(:reputation_name, :scope, :find_options)

To find active records evaluated by a given source for a scoped reputation, try this:

ActiveRecord::Base.evaluated_by(:reputation_name, source, :scope)

There are a few more helper methods available for scopes:

# Allows you to add a scope dynamically.
add_scope_for(reputation_name, scope)

# Returns true if the reputation has scopes.
has_scopes?(reputation_name)

# Returns true if the reputation has a given scope.
has_scope?(reputation_name, scope)