mongoid_rateable 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -3
- data/Rakefile +1 -0
- data/TODO +5 -2
- data/VERSION +1 -1
- data/lib/mongoid_rateable/rateable.rb +40 -14
- data/mongoid_rateable.gemspec +2 -2
- data/spec/rateable_spec.rb +13 -13
- metadata +19 -19
data/README.rdoc
CHANGED
@@ -52,11 +52,11 @@ Sure, you can rate and save in one fuction:
|
|
52
52
|
|
53
53
|
You'll often want to know if a user already rated post. Simple:
|
54
54
|
|
55
|
-
@post.
|
55
|
+
@post.rated_by? @user # True if it rated by user
|
56
56
|
|
57
57
|
And if someone rated it:
|
58
58
|
|
59
|
-
@post.rated?
|
59
|
+
@post.rated? # True if it rated by someone
|
60
60
|
|
61
61
|
You can also get a tally of the number of votes cast:
|
62
62
|
|
@@ -64,7 +64,7 @@ You can also get a tally of the number of votes cast:
|
|
64
64
|
|
65
65
|
You can get the average rating:
|
66
66
|
|
67
|
-
@post.rating
|
67
|
+
@post.rating # rates / raters.count
|
68
68
|
|
69
69
|
== Contributing to Mongoid::Rateable
|
70
70
|
|
data/Rakefile
CHANGED
data/TODO
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
add method/scope most_voted(count), rated_between(,) to Rateable
|
1
|
+
add method/scope most_voted(count), rated_between(3,5), rated_by(user) to Rateable
|
2
2
|
add min/max rating
|
3
3
|
store rate_count and average_rate as fields (need migration to migrate from other versions)
|
4
4
|
improve speed (http://cookbook.mongodb.org/patterns/votes/)
|
5
|
-
rate embedded (indexes?!)
|
5
|
+
rate embedded (indexes?!)
|
6
|
+
add random values to specs
|
7
|
+
add task for rating update
|
8
|
+
look at https://github.com/jasherai/acts-as-rated features
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
@@ -4,7 +4,11 @@ module Mongoid
|
|
4
4
|
|
5
5
|
included do
|
6
6
|
field :rates, :type => Integer, :default => 0
|
7
|
+
field :rating, :type => Float, :default => nil
|
8
|
+
field :rate_count, :type => Integer, :default => 0
|
9
|
+
|
7
10
|
embeds_many :rating_marks, :as => :rateable
|
11
|
+
|
8
12
|
index(
|
9
13
|
[
|
10
14
|
["rating_marks.rater_id"],
|
@@ -17,17 +21,15 @@ module Mongoid
|
|
17
21
|
module InstanceMethods
|
18
22
|
|
19
23
|
def rate(mark, rater)
|
20
|
-
|
24
|
+
unrate_without_rating_update(rater)
|
21
25
|
self.rates += mark.to_i
|
22
26
|
self.rating_marks.new(:rater_id => rater.id, :mark => mark, :rater_class => rater.class.to_s)
|
27
|
+
update_rating
|
23
28
|
end
|
24
29
|
|
25
30
|
def unrate(rater)
|
26
|
-
|
27
|
-
|
28
|
-
self.rates -= mark.mark.to_i
|
29
|
-
mark.delete
|
30
|
-
end
|
31
|
+
unrate_without_rating_update(rater)
|
32
|
+
update_rating
|
31
33
|
end
|
32
34
|
|
33
35
|
def rate_and_save(mark, rater)
|
@@ -42,22 +44,46 @@ module Mongoid
|
|
42
44
|
|
43
45
|
def rated?(rater = nil)
|
44
46
|
if rater
|
45
|
-
|
47
|
+
puts "Deprecated method, please use rated_by?"
|
48
|
+
rated_by?(rater)
|
46
49
|
else
|
47
|
-
|
50
|
+
rate_count!=0
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
54
|
+
def rated_by?(rater)
|
55
|
+
self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).count == 1
|
56
|
+
end
|
57
|
+
|
51
58
|
def rating
|
52
|
-
|
53
|
-
nil
|
54
|
-
else
|
55
|
-
self.rates.to_f / self.rating_marks.size
|
56
|
-
end
|
59
|
+
read_attribute(:rating) || calculate_and_store_rating
|
57
60
|
end
|
58
61
|
|
59
62
|
def rate_count
|
60
|
-
|
63
|
+
rating_marks.size
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def unrate_without_rating_update(rater)
|
69
|
+
rmark = self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).first
|
70
|
+
if rmark
|
71
|
+
self.rates -= rmark.mark.to_i
|
72
|
+
rmark.delete
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def update_rating
|
77
|
+
rt = (self.rates.to_f / self.rating_marks.size) unless self.rating_marks.blank?
|
78
|
+
write_attribute(:rating, rt)
|
79
|
+
end
|
80
|
+
|
81
|
+
def calculate_and_store_rating
|
82
|
+
unless self.rating_marks.empty?
|
83
|
+
update_rating
|
84
|
+
read_attribute(:rating)
|
85
|
+
end
|
86
|
+
nil
|
61
87
|
end
|
62
88
|
|
63
89
|
end
|
data/mongoid_rateable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid_rateable}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
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-
|
12
|
+
s.date = %q{2011-07-13}
|
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 = [
|
data/spec/rateable_spec.rb
CHANGED
@@ -70,20 +70,20 @@ describe Post do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
describe "for Bob" do
|
73
|
-
specify { @post.
|
73
|
+
specify { @post.rated_by?(@bob).should be_true }
|
74
74
|
end
|
75
75
|
|
76
76
|
describe "when rated by someone else" do
|
77
77
|
before { @post.rate 1, @alice }
|
78
78
|
|
79
79
|
describe "for Alice" do
|
80
|
-
specify { @post.
|
80
|
+
specify { @post.rated_by?(@alice).should be_true }
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
84
|
describe "when not rated by someone else" do
|
85
85
|
describe "for Sally" do
|
86
|
-
specify { @post.
|
86
|
+
specify { @post.rated_by?(@sally).should be_false }
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
@@ -117,7 +117,7 @@ describe Post do
|
|
117
117
|
@post.rating.should eql 2.5
|
118
118
|
end
|
119
119
|
|
120
|
-
it "should average if the result is zero" do
|
120
|
+
it "should calculate the average rate if the result is zero" do
|
121
121
|
@post.rate -1, @sally
|
122
122
|
@post.rating.should eql 0.0
|
123
123
|
end
|
@@ -154,17 +154,17 @@ describe Post do
|
|
154
154
|
@finded_post = Post.where(:name => "Announcement").first
|
155
155
|
end
|
156
156
|
|
157
|
-
describe "#
|
157
|
+
describe "#rated_by?" do
|
158
158
|
describe "for Bob" do
|
159
|
-
specify { @finded_post.
|
159
|
+
specify { @finded_post.rated_by?(@bob).should be_true }
|
160
160
|
end
|
161
161
|
|
162
162
|
describe "for Sally" do
|
163
|
-
specify { @finded_post.
|
163
|
+
specify { @finded_post.rated_by?(@sally).should be_true }
|
164
164
|
end
|
165
165
|
|
166
166
|
describe "for Alice" do
|
167
|
-
specify { @finded_post.
|
167
|
+
specify { @finded_post.rated_by?(@alice).should be_false}
|
168
168
|
end
|
169
169
|
end
|
170
170
|
|
@@ -190,15 +190,15 @@ describe Post do
|
|
190
190
|
|
191
191
|
describe "#rated?" do
|
192
192
|
it "should be #rated? by Bob" do
|
193
|
-
@finded_post.
|
193
|
+
@finded_post.rated_by?(@bob).should be_true
|
194
194
|
end
|
195
195
|
|
196
196
|
it "should be #rated? by Sally" do
|
197
|
-
@finded_post.
|
197
|
+
@finded_post.rated_by?(@sally).should be_true
|
198
198
|
end
|
199
199
|
|
200
200
|
it "should be not #rated? by Alice" do
|
201
|
-
@finded_post.
|
201
|
+
@finded_post.rated_by?(@alice).should be_false
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
@@ -222,11 +222,11 @@ describe Post do
|
|
222
222
|
|
223
223
|
describe "#rated?" do
|
224
224
|
it "should be #rated? by Bob" do
|
225
|
-
@finded_post.
|
225
|
+
@finded_post.rated_by?(@bob).should be_true
|
226
226
|
end
|
227
227
|
|
228
228
|
it "should be not #rated? by Sally" do
|
229
|
-
@finded_post.
|
229
|
+
@finded_post.rated_by?(@sally).should be_false
|
230
230
|
end
|
231
231
|
|
232
232
|
it "should be #rated?" do
|
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.0
|
4
|
+
version: 0.1.0
|
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-
|
12
|
+
date: 2011-07-13 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: &
|
17
|
+
requirement: &86246380 !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: *
|
25
|
+
version_requirements: *86246380
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: mongoid
|
28
|
-
requirement: &
|
28
|
+
requirement: &86246140 !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: *
|
36
|
+
version_requirements: *86246140
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bundler
|
39
|
-
requirement: &
|
39
|
+
requirement: &86245900 !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: *
|
47
|
+
version_requirements: *86245900
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: jeweler
|
50
|
-
requirement: &
|
50
|
+
requirement: &86245660 !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: *
|
58
|
+
version_requirements: *86245660
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: simplecov
|
61
|
-
requirement: &
|
61
|
+
requirement: &86245410 !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: *
|
69
|
+
version_requirements: *86245410
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: rdoc
|
72
|
-
requirement: &
|
72
|
+
requirement: &86245170 !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: *
|
80
|
+
version_requirements: *86245170
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
82
|
name: rspec
|
83
|
-
requirement: &
|
83
|
+
requirement: &86244920 !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: *
|
91
|
+
version_requirements: *86244920
|
92
92
|
- !ruby/object:Gem::Dependency
|
93
93
|
name: database_cleaner
|
94
|
-
requirement: &
|
94
|
+
requirement: &86244680 !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: *
|
102
|
+
version_requirements: *86244680
|
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:
|
146
|
+
hash: 839326095
|
147
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
148
|
none: false
|
149
149
|
requirements:
|