Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from NARKOZ/cleanup
Browse files Browse the repository at this point in the history
Minor refactoring and README update
  • Loading branch information
Katsuya Noguchi committed May 23, 2012
2 parents fef418d + 008b899 commit d442f50
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -10,7 +10,19 @@ In this gem, the reputation system is described as a network of reputations wher

## Installation

`gem install reputation_system && rails generator reputation_system && rake db:migrate`
Add to Gemfile:

```ruby
gem 'activerecord-reputation-system', :require => 'reputation_system'
```

Run:

```ruby
bundle install
rails generator reputation_system
rake db:migrate
```

## Usage Example

Expand Down
3 changes: 1 addition & 2 deletions lib/models/rs_evaluation.rb
Expand Up @@ -36,12 +36,11 @@ def self.find_by_reputation_name_and_source_and_target(reputation_name, source,
end

def self.create_evaluation(reputation_name, value, source, target)
reputation_name = reputation_name.to_sym
RSEvaluation.create!(:reputation_name => reputation_name.to_s, :value => value,
:source_id => source.id, :source_type => source.class.name,
:target_id => target.id, :target_type => target.class.name)
end

protected

def source_must_be_defined_for_reputation_in_network
Expand Down
9 changes: 4 additions & 5 deletions lib/models/rs_reputation.rb
Expand Up @@ -26,7 +26,6 @@ def from(sender)
attr_accessible :reputation_name, :value, :aggregated_by, :active, :target, :target_id, :target_type, :received_messages

before_save :change_zero_value_in_case_of_product_process
before_create

VALID_PROCESSES = ['sum', 'average', 'product']
validates_inclusion_of :aggregated_by, :in => VALID_PROCESSES, :message => "Value chosen for aggregated_by is not valid process"
Expand All @@ -52,7 +51,7 @@ def self.create_reputation(reputation_name, target, process)
end

def self.update_reputation_value_with_new_source(rep, source, weight, process)
weight = 1 unless weight # weight is 1 by default.
weight ||= 1 # weight is 1 by default.
size = rep.received_messages.size
valueBeforeUpdate = size > 0 ? rep.value : nil
newValue = source.value
Expand All @@ -72,15 +71,15 @@ def self.update_reputation_value_with_new_source(rep, source, weight, process)
end

def self.update_reputation_value_with_updated_source(rep, source, oldValue, weight, process)
weight = 1 unless weight # weight is 1 by default.\
weight ||= 1 # weight is 1 by default.
size = rep.received_messages.size
valueBeforeUpdate = size > 0 ? rep.value : nil
newValue = source.value
case process.to_sym
when :sum
rep.value = rep.value + (newValue - oldValue) * weight
rep.value += (newValue - oldValue) * weight
when :average
rep.value = rep.value + ((newValue - oldValue) * weight) / size
rep.value += ((newValue - oldValue) * weight) / size
when :product
rep.value = (rep.value * newValue) / oldValue
else
Expand Down

0 comments on commit d442f50

Please sign in to comment.