mongoid_rateable 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,30 +1,34 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.0.9)
5
- activesupport (= 3.0.9)
6
- builder (~> 2.1.2)
7
- i18n (~> 0.5.0)
8
- activesupport (3.0.9)
9
- bson (1.3.1)
10
- bson_ext (1.3.1)
11
- builder (2.1.2)
4
+ activemodel (3.1.0)
5
+ activesupport (= 3.1.0)
6
+ bcrypt-ruby (~> 3.0.0)
7
+ builder (~> 3.0.0)
8
+ i18n (~> 0.6)
9
+ activesupport (3.1.0)
10
+ multi_json (~> 1.0)
11
+ bcrypt-ruby (3.0.1)
12
+ bson (1.4.0)
13
+ bson_ext (1.4.0)
14
+ builder (3.0.0)
12
15
  database_cleaner (0.6.7)
13
- diff-lcs (1.1.2)
16
+ diff-lcs (1.1.3)
14
17
  git (1.2.5)
15
- i18n (0.5.0)
18
+ i18n (0.6.0)
16
19
  jeweler (1.6.4)
17
20
  bundler (~> 1.0)
18
21
  git (>= 1.2.5)
19
22
  rake
20
23
  mongo (1.3.1)
21
24
  bson (>= 1.3.1)
22
- mongoid (2.1.5)
25
+ mongoid (2.2.1)
23
26
  activemodel (~> 3.0)
24
- mongo (~> 1.3)
27
+ mongo (>= 1.3, < 1.4)
25
28
  tzinfo (~> 0.3.22)
29
+ multi_json (1.0.3)
26
30
  rake (0.9.2)
27
- rdoc (3.9.1)
31
+ rdoc (3.9.4)
28
32
  rspec (2.6.0)
29
33
  rspec-core (~> 2.6.0)
30
34
  rspec-expectations (~> 2.6.0)
@@ -33,9 +37,10 @@ GEM
33
37
  rspec-expectations (2.6.0)
34
38
  diff-lcs (~> 1.1.2)
35
39
  rspec-mocks (2.6.0)
36
- simplecov (0.4.2)
37
- simplecov-html (~> 0.4.4)
38
- simplecov-html (0.4.5)
40
+ simplecov (0.5.3)
41
+ multi_json (~> 1.0.3)
42
+ simplecov-html (~> 0.5.3)
43
+ simplecov-html (0.5.3)
39
44
  tzinfo (0.3.29)
40
45
 
41
46
  PLATFORMS
data/README.rdoc CHANGED
@@ -37,6 +37,11 @@ You can rate by passing an integer and a rater model to the "rate" method:
37
37
  @post.rate 5, @user # I LOVE this!
38
38
  @post.rate -10, @user # Delete it from the Internet!
39
39
 
40
+ Rates have weight (1 by defaut)
41
+
42
+ @post.rate 5, @user, 3 # Rate @post with weight 3 (@user has high karma)
43
+ @post.rate 3, @user, 1 # Rate @post with weight 1 (@user has low karma)
44
+
40
45
  You can unrate by using "unrate" method:
41
46
 
42
47
  @post.unrate @user
@@ -60,13 +65,17 @@ And if someone rated it:
60
65
 
61
66
  @post.rated? # True if it rated by someone
62
67
 
63
- You can also get a tally of the number of votes cast:
68
+ You can also get a tally of the number of rates cast:
64
69
 
65
70
  @post.rate_count # Just one so far!
66
71
 
67
- You can get the average rating:
72
+ You can get a total weight of post rates:
73
+
74
+ @post.rate_weight # Just one so far!
75
+
76
+ And you can get the average rating:
68
77
 
69
- @post.rating # rates / raters.count
78
+ @post.rating # rates / rate_weight
70
79
 
71
80
  == Scopes
72
81
 
data/TODO CHANGED
@@ -2,5 +2,4 @@ improve speed (http://cookbook.mongodb.org/patterns/votes/)
2
2
  rate embedded (indexes?!)
3
3
  add random values to specs
4
4
  add task for rating update
5
- add bayesian_rating ( https://github.com/mepatterson/acts_as_mongo_rateable )
6
- add weight to rating
5
+ add bayesian_rating ( https://github.com/mepatterson/acts_as_mongo_rateable )
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -5,6 +5,7 @@ module Mongoid
5
5
  included do
6
6
  field :rates, :type => Integer, :default => 0
7
7
  field :rating, :type => Float, :default => nil
8
+ field :weighted_rate_count, :type => Integer, :default => 0
8
9
 
9
10
  embeds_many :rating_marks, :as => :rateable
10
11
 
@@ -24,12 +25,14 @@ module Mongoid
24
25
 
25
26
  module InstanceMethods
26
27
 
27
- def rate(mark, rater)
28
- validate_rating!(mark)
29
- unrate_without_rating_update(rater)
30
- self.rates += mark.to_i
31
- self.rating_marks.new(:rater_id => rater.id, :mark => mark, :rater_class => rater.class.to_s)
32
- update_rating
28
+ def rate(mark, rater, weight = 1)
29
+ validate_rating!(mark)
30
+ unrate_without_rating_update(rater)
31
+ total_mark = mark.to_i*weight.to_i
32
+ self.rates += total_mark
33
+ self.rating_marks.new(:rater_id => rater.id, :mark => mark, :rater_class => rater.class.to_s, :weight => weight)
34
+ self.weighted_rate_count += weight
35
+ update_rating
33
36
  end
34
37
 
35
38
  def unrate(rater)
@@ -37,8 +40,8 @@ module Mongoid
37
40
  update_rating
38
41
  end
39
42
 
40
- def rate_and_save(mark, rater)
41
- rate(mark, rater)
43
+ def rate_and_save(mark, rater, weight = 1)
44
+ rate(mark, rater, weight)
42
45
  save
43
46
  end
44
47
 
@@ -65,7 +68,12 @@ module Mongoid
65
68
  end
66
69
 
67
70
  def rate_count
68
- rating_marks.size
71
+ self.rating_marks.size
72
+ end
73
+
74
+ def rate_weight
75
+ check_weighted_rate_count
76
+ read_attribute(:weighted_rate_count)
69
77
  end
70
78
 
71
79
  protected
@@ -79,14 +87,26 @@ module Mongoid
79
87
  def unrate_without_rating_update(rater)
80
88
  rmark = self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).first
81
89
  if rmark
82
- self.rates -= rmark.mark.to_i
90
+ weight = (rmark.weight ||= 1)
91
+ total_mark = rmark.mark.to_i*weight.to_i
92
+ self.rates -= total_mark
93
+ self.weighted_rate_count -= weight
83
94
  rmark.delete
84
95
  end
85
96
  end
86
97
 
87
98
  def update_rating
88
- rt = (self.rates.to_f / self.rating_marks.size) unless self.rating_marks.blank?
89
- write_attribute(:rating, rt)
99
+ check_weighted_rate_count
100
+ rt = (self.rates.to_f / self.weighted_rate_count.to_f) unless self.rating_marks.blank?
101
+ write_attribute(:rating, rt)
102
+ end
103
+
104
+ def check_weighted_rate_count
105
+ #migration from old version
106
+ wrc = read_attribute(:weighted_rate_count).to_i
107
+ if (wrc==0 && rate_count!=0)
108
+ write_attribute(:weighted_rate_count, self.rating_marks.size)
109
+ end
90
110
  end
91
111
 
92
112
  end
@@ -4,4 +4,5 @@ class RatingMark
4
4
  field :mark, :type => Integer
5
5
  field :rater_class, :type => String
6
6
  field :rater_id, :type => BSON::ObjectId
7
+ field :weight, :type => Integer, :default => 1
7
8
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_rateable}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Peter Savichev (proton)}]
12
- s.date = %q{2011-08-20}
12
+ s.date = %q{2011-09-27}
13
13
  s.description = %q{Provides fields and methods for the rating manipulation on Mongoid documents.}
14
14
  s.email = %q{psavichev@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -40,6 +40,10 @@ describe Post do
40
40
  @post.rate 1, @sally
41
41
  @post.rates.should eql 2
42
42
  end
43
+ it "should track weighted #rates properly" do
44
+ @post.rate 1, @alice, 4
45
+ @post.rates.should eql 5
46
+ end
43
47
 
44
48
  it "should limit #rates by user properly" do
45
49
  @post.rate 5, @bob
@@ -56,14 +60,14 @@ describe Post do
56
60
 
57
61
  #TODO: Rewrite for random values
58
62
  describe "when using negative values" do
59
- it "should work properly for -1" do
60
- @post.rate -1, @sally
61
- @post.rates.should eql 0
62
- end
63
- it "should work properly for -2" do
63
+ it "should work properly for -3" do
64
64
  @post.rate -3, @sally
65
65
  @post.rates.should eql -2
66
66
  end
67
+ it "should work properly for -1 with weight 3" do
68
+ @post.rate -1, @sally, 3
69
+ @post.rates.should eql -2
70
+ end
67
71
  end
68
72
  end
69
73
 
@@ -186,7 +190,7 @@ describe Post do
186
190
 
187
191
  describe "#rate_and_save" do
188
192
  before (:each) do
189
- @post.rate_and_save 8, @bob
193
+ @post.rate_and_save 8, @bob, 2
190
194
  @post.rate_and_save -10, @sally
191
195
  @finded_post = Post.where(:name => "Announcement").first
192
196
  end
@@ -205,31 +209,35 @@ describe Post do
205
209
  end
206
210
  end
207
211
 
208
- it "should have #rates equal -2" do
209
- @finded_post.rates.should eql -2
212
+ it "should have #rates equal 6" do
213
+ @finded_post.rates.should eql 6
210
214
  end
211
215
 
212
216
  it "should have #rate_count equal 2" do
213
217
  @finded_post.rate_count.should eql 2
214
218
  end
215
219
 
216
- it "should have #rating equal -1.0" do
217
- @finded_post.rating.should eql -1.0
220
+ it "should have #rate_weight equal 3" do
221
+ @finded_post.rate_weight.should eql 3
222
+ end
223
+
224
+ it "should have #rating equal 2.0" do
225
+ @finded_post.rating.should eql 2.0
218
226
  end
219
227
 
220
228
  describe "#unrate_and_save" do
221
229
  before (:each) do
222
- @post.unrate_and_save @sally
230
+ @post.unrate_and_save @bob
223
231
  @finded_post = Post.where(:name => "Announcement").first
224
232
  end
225
233
 
226
234
  describe "#rated?" do
227
- it "should be #rated? by Bob" do
228
- @finded_post.rated_by?(@bob).should be_true
235
+ it "should be #rated? by Sally" do
236
+ @finded_post.rated_by?(@sally).should be_true
229
237
  end
230
238
 
231
- it "should be not #rated? by Sally" do
232
- @finded_post.rated_by?(@sally).should be_false
239
+ it "should be not #rated? by Bob" do
240
+ @finded_post.rated_by?(@bob).should be_false
233
241
  end
234
242
 
235
243
  it "should be #rated?" do
@@ -237,16 +245,20 @@ describe Post do
237
245
  end
238
246
  end
239
247
 
240
- it "should have #rates equal 8" do
241
- @finded_post.rates.should eql 8
248
+ it "should have #rates equal -10" do
249
+ @finded_post.rates.should eql -10
242
250
  end
243
251
 
244
252
  it "should have #rate_count equal 1" do
245
253
  @finded_post.rate_count.should eql 1
246
254
  end
247
255
 
248
- it "should have #rating equal 8.0" do
249
- @finded_post.rating.should eql 8.0
256
+ it "should have #rate_weight equal 1" do
257
+ @finded_post.rate_weight.should eql 1
258
+ end
259
+
260
+ it "should have #rating equal -10.0" do
261
+ @finded_post.rating.should eql -10.0
250
262
  end
251
263
  end
252
264
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_rateable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-20 00:00:00.000000000Z
12
+ date: 2011-09-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
16
- requirement: &80263240 !ruby/object:Gem::Requirement
16
+ requirement: &89686630 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *80263240
24
+ version_requirements: *89686630
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongoid
27
- requirement: &80262940 !ruby/object:Gem::Requirement
27
+ requirement: &89686300 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *80262940
35
+ version_requirements: *89686300
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &80262640 !ruby/object:Gem::Requirement
38
+ requirement: &89685960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *80262640
46
+ version_requirements: *89685960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &80262310 !ruby/object:Gem::Requirement
49
+ requirement: &89685640 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.6.2
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *80262310
57
+ version_requirements: *89685640
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &80261990 !ruby/object:Gem::Requirement
60
+ requirement: &89685310 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.4.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *80261990
68
+ version_requirements: *89685310
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &80261710 !ruby/object:Gem::Requirement
71
+ requirement: &89685000 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *80261710
79
+ version_requirements: *89685000
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &80261420 !ruby/object:Gem::Requirement
82
+ requirement: &89684700 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 2.0.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *80261420
90
+ version_requirements: *89684700
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: database_cleaner
93
- requirement: &80261110 !ruby/object:Gem::Requirement
93
+ requirement: &89684390 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *80261110
101
+ version_requirements: *89684390
102
102
  description: Provides fields and methods for the rating manipulation on Mongoid documents.
103
103
  email: psavichev@gmail.com
104
104
  executables: []
@@ -141,7 +141,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  segments:
143
143
  - 0
144
- hash: -505441169
144
+ hash: 137369441
145
145
  required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  none: false
147
147
  requirements: