activerecord-reputation-system 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -10,7 +10,19 @@ In this gem, the reputation system is described as a network of reputations wher
|
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
13
|
-
|
13
|
+
Add to Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'activerecord-reputation-system', :require => 'reputation_system'
|
17
|
+
```
|
18
|
+
|
19
|
+
Run:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
bundle install
|
23
|
+
rails generator reputation_system
|
24
|
+
rake db:migrate
|
25
|
+
```
|
14
26
|
|
15
27
|
## Usage Example
|
16
28
|
|
@@ -24,7 +36,7 @@ class User < ActiveRecord::Base
|
|
24
36
|
:source => [
|
25
37
|
{ :reputation => :questioning_skill, :weight => 0.8 },
|
26
38
|
{ :reputation => :answering_skill }],
|
27
|
-
aggregated_by => :sum
|
39
|
+
:aggregated_by => :sum
|
28
40
|
|
29
41
|
has_reputation :questioning_skill,
|
30
42
|
:source => { :reputation => :votes, :of => :questions },
|
@@ -115,6 +127,12 @@ delete_evaluation(reputation_name, source)
|
|
115
127
|
|
116
128
|
# Deletes an evaluation from the reputation with the specified name submitted by specified source. Raises an exception if it does not exist.
|
117
129
|
delete_evaluation!(reputation_name, source)
|
130
|
+
|
131
|
+
# Increase an evaluation value of the reputation with the specified name by given value.
|
132
|
+
increase_evaluation(reputation_name, value, source)
|
133
|
+
|
134
|
+
# Decrease an evaluation value of the reputation with the specified name by given value.
|
135
|
+
decrease_evaluation(reputation_name, value, source)
|
118
136
|
```
|
119
137
|
|
120
138
|
## Reputation
|
@@ -173,7 +191,7 @@ has_reputation :name,
|
|
173
191
|
```
|
174
192
|
Once scopes are defined, evaluations can be added in the context of defined scopes:
|
175
193
|
```ruby
|
176
|
-
add_evaluation(:reputation_name, :scope)
|
194
|
+
add_evaluation(:reputation_name, evaluation_value, source, :scope)
|
177
195
|
```
|
178
196
|
Also, reputations can be accessed in the context of scopes:
|
179
197
|
```ruby
|
data/lib/models/rs_evaluation.rb
CHANGED
@@ -36,12 +36,11 @@ class RSEvaluation < ActiveRecord::Base
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def self.create_evaluation(reputation_name, value, source, target)
|
39
|
-
reputation_name = reputation_name.to_sym
|
40
39
|
RSEvaluation.create!(:reputation_name => reputation_name.to_s, :value => value,
|
41
40
|
:source_id => source.id, :source_type => source.class.name,
|
42
41
|
:target_id => target.id, :target_type => target.class.name)
|
43
42
|
end
|
44
|
-
|
43
|
+
|
45
44
|
protected
|
46
45
|
|
47
46
|
def source_must_be_defined_for_reputation_in_network
|
data/lib/models/rs_reputation.rb
CHANGED
@@ -26,7 +26,6 @@ class RSReputation < ActiveRecord::Base
|
|
26
26
|
attr_accessible :reputation_name, :value, :aggregated_by, :active, :target, :target_id, :target_type, :received_messages
|
27
27
|
|
28
28
|
before_save :change_zero_value_in_case_of_product_process
|
29
|
-
before_create
|
30
29
|
|
31
30
|
VALID_PROCESSES = ['sum', 'average', 'product']
|
32
31
|
validates_inclusion_of :aggregated_by, :in => VALID_PROCESSES, :message => "Value chosen for aggregated_by is not valid process"
|
@@ -52,7 +51,7 @@ class RSReputation < ActiveRecord::Base
|
|
52
51
|
end
|
53
52
|
|
54
53
|
def self.update_reputation_value_with_new_source(rep, source, weight, process)
|
55
|
-
weight
|
54
|
+
weight ||= 1 # weight is 1 by default.
|
56
55
|
size = rep.received_messages.size
|
57
56
|
valueBeforeUpdate = size > 0 ? rep.value : nil
|
58
57
|
newValue = source.value
|
@@ -72,15 +71,15 @@ class RSReputation < ActiveRecord::Base
|
|
72
71
|
end
|
73
72
|
|
74
73
|
def self.update_reputation_value_with_updated_source(rep, source, oldValue, weight, process)
|
75
|
-
weight
|
74
|
+
weight ||= 1 # weight is 1 by default.
|
76
75
|
size = rep.received_messages.size
|
77
76
|
valueBeforeUpdate = size > 0 ? rep.value : nil
|
78
77
|
newValue = source.value
|
79
78
|
case process.to_sym
|
80
79
|
when :sum
|
81
|
-
rep.value
|
80
|
+
rep.value += (newValue - oldValue) * weight
|
82
81
|
when :average
|
83
|
-
rep.value
|
82
|
+
rep.value += ((newValue - oldValue) * weight) / size
|
84
83
|
when :product
|
85
84
|
rep.value = (rep.value * newValue) / oldValue
|
86
85
|
else
|
@@ -45,7 +45,8 @@ module ReputationSystem
|
|
45
45
|
|
46
46
|
def add_or_update_evaluation(reputation_name, value, source, *args)
|
47
47
|
scope = args.first
|
48
|
-
|
48
|
+
srn = ReputationSystem::Network.get_scoped_reputation_name(self.class.name, reputation_name, scope)
|
49
|
+
evaluation = RSEvaluation.find_by_reputation_name_and_source_and_target(srn, source, self)
|
49
50
|
if evaluation.nil?
|
50
51
|
self.add_evaluation(reputation_name, value, source, scope)
|
51
52
|
else
|
@@ -84,5 +85,26 @@ module ReputationSystem
|
|
84
85
|
evaluation.destroy
|
85
86
|
end
|
86
87
|
end
|
88
|
+
|
89
|
+
def increase_evaluation(reputation_name, value, source, *args)
|
90
|
+
change_evaluation_value_by(reputation_name, value, source, *args)
|
91
|
+
end
|
92
|
+
|
93
|
+
def decrease_evaluation(reputation_name, value, source, *args)
|
94
|
+
change_evaluation_value_by(reputation_name, -value, source, *args)
|
95
|
+
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
def change_evaluation_value_by(reputation_name, value, source, *args)
|
99
|
+
scope = args.first
|
100
|
+
srn = ReputationSystem::Network.get_scoped_reputation_name(self.class.name, reputation_name, scope)
|
101
|
+
evaluation = RSEvaluation.find_by_reputation_name_and_source_and_target(srn, source, self)
|
102
|
+
if evaluation.nil?
|
103
|
+
self.add_evaluation(reputation_name, value, source, scope)
|
104
|
+
else
|
105
|
+
new_value = evaluation.value + value
|
106
|
+
self.update_evaluation(reputation_name, new_value, source, scope)
|
107
|
+
end
|
108
|
+
end
|
87
109
|
end
|
88
110
|
end
|
@@ -65,6 +65,39 @@ describe ActiveRecord::Base do
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
+
describe "#add_or_update_evaluation" do
|
69
|
+
it "should create evaluation if it does not exist" do
|
70
|
+
@question.add_or_update_evaluation(:total_votes, 1, @user)
|
71
|
+
@question.reputation_value_for(:total_votes).should == 1
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should update evaluation if it exists already" do
|
75
|
+
@question.add_evaluation(:total_votes, 1, @user)
|
76
|
+
@question.add_or_update_evaluation(:total_votes, 2, @user)
|
77
|
+
@question.reputation_value_for(:total_votes).should == 2
|
78
|
+
end
|
79
|
+
|
80
|
+
context "With Scopes" do
|
81
|
+
it "should add evaluation on appropriate scope if it does not exist" do
|
82
|
+
@phrase.add_or_update_evaluation(:difficulty_with_scope, 1, @user, :s1)
|
83
|
+
@phrase.add_or_update_evaluation(:difficulty_with_scope, 2, @user, :s2)
|
84
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s1).should == 1
|
85
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s2).should == 2
|
86
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s3).should == 0
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should update evaluation on appropriate scope if it exists already" do
|
90
|
+
@phrase.add_evaluation(:difficulty_with_scope, 1, @user, :s1)
|
91
|
+
@phrase.add_evaluation(:difficulty_with_scope, 2, @user, :s2)
|
92
|
+
@phrase.add_or_update_evaluation(:difficulty_with_scope, 3, @user, :s1)
|
93
|
+
@phrase.add_or_update_evaluation(:difficulty_with_scope, 5, @user, :s2)
|
94
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s1).should == 3
|
95
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s2).should == 5
|
96
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s3).should == 0
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
68
101
|
describe "#update_evaluation" do
|
69
102
|
before :each do
|
70
103
|
@question.add_evaluation(:total_votes, 1, @user)
|
@@ -192,6 +225,58 @@ describe ActiveRecord::Base do
|
|
192
225
|
end
|
193
226
|
end
|
194
227
|
end
|
228
|
+
|
229
|
+
describe "#increase_evaluation" do
|
230
|
+
it "should add evaluation if it does not exist" do
|
231
|
+
@question.increase_evaluation(:total_votes, 2, @user)
|
232
|
+
@question.reputation_value_for(:total_votes).should == 2
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should increase evaluation if it exists already" do
|
236
|
+
@question.add_evaluation(:total_votes, 1, @user)
|
237
|
+
@question.increase_evaluation(:total_votes, 2, @user)
|
238
|
+
@question.reputation_value_for(:total_votes).should == 3
|
239
|
+
end
|
240
|
+
|
241
|
+
context "With Scopes" do
|
242
|
+
before :each do
|
243
|
+
@phrase.add_evaluation(:difficulty_with_scope, 2, @user, :s2)
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should increase evaluation on appropriate scope" do
|
247
|
+
@phrase.increase_evaluation(:difficulty_with_scope, 5, @user, :s2)
|
248
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s1).should == 0
|
249
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s2).should == 7
|
250
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s3).should == 0
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
describe "#decrease_evaluation" do
|
256
|
+
it "should add evaluation if it does not exist" do
|
257
|
+
@question.decrease_evaluation(:total_votes, 2, @user)
|
258
|
+
@question.reputation_value_for(:total_votes).should == -2
|
259
|
+
end
|
260
|
+
|
261
|
+
it "should increase evaluation if it exists already" do
|
262
|
+
@question.add_evaluation(:total_votes, 1, @user)
|
263
|
+
@question.decrease_evaluation(:total_votes, 2, @user)
|
264
|
+
@question.reputation_value_for(:total_votes).should == -1
|
265
|
+
end
|
266
|
+
|
267
|
+
context "With Scopes" do
|
268
|
+
before :each do
|
269
|
+
@phrase.add_evaluation(:difficulty_with_scope, 2, @user, :s2)
|
270
|
+
end
|
271
|
+
|
272
|
+
it "should increase evaluation on appropriate scope" do
|
273
|
+
@phrase.decrease_evaluation(:difficulty_with_scope, 5, @user, :s2)
|
274
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s1).should == 0
|
275
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s2).should == -3
|
276
|
+
@phrase.reputation_value_for(:difficulty_with_scope, :s3).should == 0
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
195
280
|
end
|
196
281
|
|
197
282
|
context "Non-Primary Reputation with Gathering Aggregation" do
|
@@ -286,4 +371,4 @@ describe ActiveRecord::Base do
|
|
286
371
|
end
|
287
372
|
end
|
288
373
|
end
|
289
|
-
end
|
374
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 1.0.0
|
9
|
+
version: 1.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Katsuya Noguchi
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2012-05-
|
17
|
+
date: 2012-05-22 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|