activerecord-reputation-system 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -71,14 +71,14 @@ Once reputation system is defined, evaluations for answers and questions can be
71
71
 
72
72
  Reputation value can be accessed as follow:
73
73
  ```ruby
74
- @answer.reptuation_for(:avg_rating) #=> 3
75
- @question.reptuation_for(:votes) #=> 1
76
- @user.reptuation_for(:karma)
74
+ @answer.reputation_for(:avg_rating) #=> 3
75
+ @question.reputation_for(:votes) #=> 1
76
+ @user.reputation_for(:karma)
77
77
  ```
78
78
 
79
79
  You can query for records using reputation value:
80
80
  ```ruby
81
- @user.with_reputation(:karma).where('karma > 10')
81
+ User.find_with_reputation(:karma, :all, { :condition => 'karma > 10' })
82
82
  ```
83
83
 
84
84
  You can get source records that have evaluated the target record:
@@ -93,13 +93,13 @@ Question.evaluated_by(:votes, @user) #=> [@question]
93
93
 
94
94
  ## Documentation
95
95
 
96
- Please refer [Wiki](https://github.com/twitter/activerecord-reputation-system/wiki) for available APIs.
96
+ Please refer [Wiki](https://github.com/twitter/activerecord-reputation-system/wiki) for available APIs and more information.
97
97
 
98
98
  ## Authors
99
99
 
100
100
  Katsuya Noguchi
101
101
  * [http://twitter.com/kn](http://twitter.com/kn)
102
- * [http://github.com/katsuyan](http://github.com/katsuyan)
102
+ * [http://github.com/kn](http://github.com/kn)
103
103
 
104
104
  ## Contributors
105
105
 
@@ -32,7 +32,7 @@ module ReputationSystem
32
32
  validate :source_must_be_defined_for_reputation_in_network
33
33
 
34
34
  def self.find_by_reputation_name_and_source_and_target(reputation_name, source, target)
35
- source_type = get_source_type_for_sti(source, target.class.name, reputation_name)
35
+ source_type = get_source_type_for_sti(source.class.name, target.class.name, reputation_name)
36
36
  ReputationSystem::Evaluation.find(:first,
37
37
  :conditions => {:reputation_name => reputation_name.to_s,
38
38
  :source_id => source.id,
@@ -48,11 +48,23 @@ module ReputationSystem
48
48
  :target_id => target.id, :target_type => target.class.name)
49
49
  end
50
50
 
51
+ # Override exists? class method.
52
+ class << self
53
+ alias :original_exists? :exists?
54
+
55
+ def exists?(options={})
56
+ if options[:source_type] && options[:target_type] && options[:reputation_name]
57
+ options[:source_type] = get_source_type_for_sti(options[:source_type], options[:target_type], options[:reputation_name])
58
+ end
59
+ original_exists? options
60
+ end
61
+ end
62
+
51
63
  protected
52
64
 
53
- def self.get_source_type_for_sti(source, target_type, reputation_name)
65
+ def self.get_source_type_for_sti(source_type, target_type, reputation_name)
54
66
  valid_source_type = ReputationSystem::Network.get_reputation_def(target_type, reputation_name)[:source].to_s.camelize
55
- source_class = source.class
67
+ source_class = source_type.constantize
56
68
  while source_class && valid_source_type != source_class.name && source_class.name != "ActiveRecord::Base"
57
69
  source_class = source_class.superclass
58
70
  end
@@ -60,7 +72,7 @@ module ReputationSystem
60
72
  end
61
73
 
62
74
  def set_source_type_for_sti
63
- sti_source_type = self.class.get_source_type_for_sti(source, target_type, reputation_name)
75
+ sti_source_type = self.class.get_source_type_for_sti(source_type, target_type, reputation_name)
64
76
  self.source_type = sti_source_type if sti_source_type
65
77
  end
66
78
 
@@ -15,5 +15,5 @@
15
15
  ##
16
16
 
17
17
  module ReputationSystem
18
- VERSION = "2.0.1"
18
+ VERSION = "2.0.2"
19
19
  end
@@ -140,7 +140,7 @@ describe ReputationSystem::EvaluationMethods do
140
140
  lambda { answer.add_evaluation(:avg_rating, 3, @user) }.should_not raise_error
141
141
  end
142
142
 
143
- context "With Scopes" do
143
+ context "with scopes" do
144
144
  it "should add evaluation on appropriate scope" do
145
145
  @phrase.add_evaluation(:difficulty_with_scope, 1, @user, :s1).should be_true
146
146
  @phrase.add_evaluation(:difficulty_with_scope, 2, @user, :s2).should be_true
@@ -171,7 +171,7 @@ describe ReputationSystem::EvaluationMethods do
171
171
  @question.reputation_for(:total_votes).should == 2
172
172
  end
173
173
 
174
- context "With Scopes" do
174
+ context "with scopes" do
175
175
  it "should add evaluation on appropriate scope if it does not exist" do
176
176
  @phrase.add_or_update_evaluation(:difficulty_with_scope, 1, @user, :s1).should be_true
177
177
  @phrase.add_or_update_evaluation(:difficulty_with_scope, 2, @user, :s2).should be_true
@@ -190,6 +190,16 @@ describe ReputationSystem::EvaluationMethods do
190
190
  @phrase.reputation_for(:difficulty_with_scope, :s3).should == 0
191
191
  end
192
192
  end
193
+
194
+ context "with STI" do
195
+ it "should be able to update evaluation by an object of a class with sti" do
196
+ @post = Post.create! :name => "Post1"
197
+ @designer = Designer.create! :name => "John"
198
+ @post.add_or_update_evaluation(:votes, 1, @designer)
199
+ @post.add_or_update_evaluation(:votes, -1, @designer)
200
+ @post.reputation_for(:votes).should == -1
201
+ end
202
+ end
193
203
  end
194
204
 
195
205
  describe "#update_evaluation" do
@@ -108,6 +108,11 @@ ActiveRecord::Schema.define do
108
108
  t.string :type
109
109
  t.timestamps
110
110
  end
111
+
112
+ create_table :posts do |t|
113
+ t.string :name
114
+ t.timestamps
115
+ end
111
116
  end
112
117
 
113
118
  class User < ActiveRecord::Base
@@ -193,6 +198,8 @@ class Translation < ActiveRecord::Base
193
198
  :source_of => { :reputation => :maturity, :of => :phrase, :scope => :locale}
194
199
  end
195
200
 
201
+ # For STI Specs
202
+
196
203
  class Person < ActiveRecord::Base
197
204
  has_reputation :leadership,
198
205
  :source => :person,
@@ -204,3 +211,10 @@ end
204
211
 
205
212
  class Designer < Person
206
213
  end
214
+
215
+ class Post < ActiveRecord::Base
216
+ belongs_to :person
217
+
218
+ has_reputation :votes,
219
+ :source => :person
220
+ end
metadata CHANGED
@@ -1,93 +1,120 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: activerecord-reputation-system
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.2
4
5
  prerelease:
5
- version: 2.0.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Katsuya Noguchi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-10-05 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: activerecord
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
24
22
  type: :development
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: rake
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
30
33
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
34
37
  version: 0.8.7
35
38
  type: :development
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: rspec
39
39
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
41
49
  none: false
42
- requirements:
50
+ requirements:
43
51
  - - ~>
44
- - !ruby/object:Gem::Version
45
- version: "2.8"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.8'
46
54
  type: :development
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
49
- name: rdoc
50
55
  prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
52
65
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
57
70
  type: :development
58
- version_requirements: *id004
59
- - !ruby/object:Gem::Dependency
60
- name: database_cleaner
61
71
  prerelease: false
62
- requirement: &id005 !ruby/object:Gem::Requirement
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: database_cleaner
80
+ requirement: !ruby/object:Gem::Requirement
63
81
  none: false
64
- requirements:
82
+ requirements:
65
83
  - - ~>
66
- - !ruby/object:Gem::Version
84
+ - !ruby/object:Gem::Version
67
85
  version: 0.7.1
68
86
  type: :development
69
- version_requirements: *id005
70
- - !ruby/object:Gem::Dependency
71
- name: sqlite3
72
87
  prerelease: false
73
- requirement: &id006 !ruby/object:Gem::Requirement
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.7.1
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
74
97
  none: false
75
- requirements:
98
+ requirements:
76
99
  - - ~>
77
- - !ruby/object:Gem::Version
100
+ - !ruby/object:Gem::Version
78
101
  version: 1.3.5
79
102
  type: :development
80
- version_requirements: *id006
81
- description: ActiveRecord Reputation System gem allows rails apps to compute and publish reputation scores for active record models.
82
- email:
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.3.5
110
+ description: ActiveRecord Reputation System gem allows rails apps to compute and publish
111
+ reputation scores for active record models.
112
+ email:
83
113
  - katsuya@twitter.com
84
114
  executables: []
85
-
86
115
  extensions: []
87
-
88
116
  extra_rdoc_files: []
89
-
90
- files:
117
+ files:
91
118
  - LICENSE
92
119
  - README.md
93
120
  - Rakefile
@@ -125,30 +152,27 @@ files:
125
152
  - spec/spec_helper.rb
126
153
  homepage: https://github.com/twitter/activerecord-reputation-system
127
154
  licenses: []
128
-
129
155
  post_install_message:
130
156
  rdoc_options: []
131
-
132
- require_paths:
157
+ require_paths:
133
158
  - lib
134
- required_ruby_version: !ruby/object:Gem::Requirement
159
+ required_ruby_version: !ruby/object:Gem::Requirement
135
160
  none: false
136
- requirements:
137
- - - ">="
138
- - !ruby/object:Gem::Version
139
- version: "0"
140
- required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
166
  none: false
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: "0"
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
146
171
  requirements: []
147
-
148
172
  rubyforge_project:
149
- rubygems_version: 1.8.10
173
+ rubygems_version: 1.8.23
150
174
  signing_key:
151
175
  specification_version: 3
152
- summary: ActiveRecord Reputation System gem allows rails apps to compute and publish reputation scores for active record models
176
+ summary: ActiveRecord Reputation System gem allows rails apps to compute and publish
177
+ reputation scores for active record models
153
178
  test_files: []
154
-