mongoid_rateable 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -66,6 +66,23 @@ You can get the average rating:
66
66
 
67
67
  @post.rating # rates / raters.count
68
68
 
69
+ == Scopes
70
+
71
+ You can get rated or unrated posts:
72
+
73
+ Post.rated
74
+ Post.unrated
75
+
76
+ You can get posts rated by someone:
77
+
78
+ Post.rated_by(@user)
79
+
80
+ You can get posts with some rating:
81
+
82
+ Post.with_rating(2..5)
83
+ Post.with_rating(0..10)
84
+ Post.with_rating(-2..2)
85
+
69
86
  == Contributing to Mongoid::Rateable
70
87
 
71
88
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/TODO CHANGED
@@ -1,6 +1,4 @@
1
- add method/scope most_voted(count), rated_between(3,5), rated_by(user) to Rateable
2
1
  add min/max rating
3
- store rate_count and average_rate as fields (need migration to migrate from other versions)
4
2
  improve speed (http://cookbook.mongodb.org/patterns/votes/)
5
3
  rate embedded (indexes?!)
6
4
  add random values to specs
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,7 +5,6 @@ module Mongoid
5
5
  included do
6
6
  field :rates, :type => Integer, :default => 0
7
7
  field :rating, :type => Float, :default => nil
8
- field :rate_count, :type => Integer, :default => 0
9
8
 
10
9
  embeds_many :rating_marks, :as => :rateable
11
10
 
@@ -16,6 +15,11 @@ module Mongoid
16
15
  ],
17
16
  unique: true
18
17
  )
18
+
19
+ scope :unrated, where(:rating.exists => false)
20
+ scope :rated, where(:rating.exists => true)
21
+ scope :rated_by, ->(rater) { where("rating_marks.rater_id" => rater.id, "rating_marks.rater_class" => rater.class.to_s) }
22
+ scope :with_rating, ->(range) { where(:rating.gte => range.begin, :rating.lte => range.end) }
19
23
  end
20
24
 
21
25
  module InstanceMethods
@@ -74,8 +78,8 @@ module Mongoid
74
78
  end
75
79
 
76
80
  def update_rating
77
- rt = (self.rates.to_f / self.rating_marks.size) unless self.rating_marks.blank?
78
- write_attribute(:rating, rt)
81
+ rt = (self.rates.to_f / self.rating_marks.size) unless self.rating_marks.blank?
82
+ write_attribute(:rating, rt)
79
83
  end
80
84
 
81
85
  def calculate_and_store_rating
@@ -3,4 +3,5 @@ class RatingMark
3
3
  embedded_in :rateable, :polymorphic => true
4
4
  field :mark, :type => Integer
5
5
  field :rater_class, :type => String
6
+ field :rater_id, :type => BSON::ObjectId
6
7
  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.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Peter Savichev (proton)"]
12
- s.date = %q{2011-07-13}
12
+ s.date = %q{2011-07-17}
13
13
  s.description = %q{Provides fields and methods for the manipulation of rates on Mongoid documents.}
14
14
  s.email = %q{psavichev@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -247,4 +247,61 @@ describe Post do
247
247
  end
248
248
  end
249
249
  end
250
+
251
+ describe "#scopes" do
252
+ before (:each) do
253
+ @post.delete
254
+ @post1 = Post.create
255
+ @post2 = Post.create
256
+ @post3 = Post.create
257
+ @post4 = Post.create
258
+ @post5 = Post.create
259
+ @post1.rate_and_save 5, @sally
260
+ @post1.rate_and_save 3, @bob
261
+ @post4.rate_and_save 1, @sally
262
+ end
263
+
264
+ describe "#unrated" do
265
+ it "should return proper count of unrated posts" do
266
+ Post.unrated.size.should eql 3
267
+ end
268
+ end
269
+
270
+ describe "#rated" do
271
+ it "should return proper count of rated posts" do
272
+ Post.rated.size.should eql 2
273
+ end
274
+ end
275
+
276
+ describe "#rated_by" do
277
+ it "should return proper count of posts rated by Bob" do
278
+ Post.rated_by(@bob).size.should eql 1
279
+ end
280
+
281
+ it "should return proper count of posts rated by Sally" do
282
+ Post.rated_by(@sally).size.should eql 2
283
+ end
284
+ end
285
+
286
+ describe "#with_rating" do
287
+ before (:each) do
288
+ @post1.rate_and_save 4, @alice
289
+ @post2.rate_and_save 2, @alice
290
+ @post3.rate_and_save 5, @alice
291
+ @post4.rate_and_save 2, @alice
292
+ end
293
+
294
+ it "should return proper count of posts with rating 4..5" do
295
+ Post.with_rating(4..5).size.should eql 2
296
+ end
297
+
298
+ it "should return proper count of posts with rating 0..2" do
299
+ Post.with_rating(0..2).size.should eql 2
300
+ end
301
+
302
+ it "should return proper count of posts with rating 0..5" do
303
+ Post.with_rating(0..5).size.should eql 4
304
+ end
305
+ end
306
+ end
250
307
  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.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-13 00:00:00.000000000 +04:00
12
+ date: 2011-07-17 00:00:00.000000000 +04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bson_ext
17
- requirement: &86246380 !ruby/object:Gem::Requirement
17
+ requirement: &76193720 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *86246380
25
+ version_requirements: *76193720
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: mongoid
28
- requirement: &86246140 !ruby/object:Gem::Requirement
28
+ requirement: &76193460 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *86246140
36
+ version_requirements: *76193460
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &86245900 !ruby/object:Gem::Requirement
39
+ requirement: &76193160 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.0.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *86245900
47
+ version_requirements: *76193160
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: jeweler
50
- requirement: &86245660 !ruby/object:Gem::Requirement
50
+ requirement: &76192840 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.6.2
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *86245660
58
+ version_requirements: *76192840
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: simplecov
61
- requirement: &86245410 !ruby/object:Gem::Requirement
61
+ requirement: &76192580 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 0.4.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *86245410
69
+ version_requirements: *76192580
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rdoc
72
- requirement: &86245170 !ruby/object:Gem::Requirement
72
+ requirement: &76192280 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *86245170
80
+ version_requirements: *76192280
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rspec
83
- requirement: &86244920 !ruby/object:Gem::Requirement
83
+ requirement: &76191990 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: 2.0.0
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *86244920
91
+ version_requirements: *76191990
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: database_cleaner
94
- requirement: &86244680 !ruby/object:Gem::Requirement
94
+ requirement: &76191730 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *86244680
102
+ version_requirements: *76191730
103
103
  description: Provides fields and methods for the manipulation of rates on Mongoid
104
104
  documents.
105
105
  email: psavichev@gmail.com
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: 839326095
146
+ hash: 792181427
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements: