mongoid_rating 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mongoid_rating/model.rb +18 -3
- data/lib/mongoid_rating/version.rb +1 -1
- data/spec/comment_spec.rb +6 -0
- data/spec/post_spec.rb +17 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25cb13913e025c8e1c6d1bdb5879273ef0d68137
|
4
|
+
data.tar.gz: 7a634be5625c8af142be76b957b4dd2f266d5827
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29ae78bb51b8ba354925ed60d2beeaca3d527e7a4e792320465af4a722af0dcaf8cb89d1e5ed63867f6b4686fb8ffea369f3d07c7bb0baa7c150ee4705ca38cd
|
7
|
+
data.tar.gz: 4319ee5e97ef7ab1bd3943d056da645098f0b480f2ec5236639616cd585788f40f86141597cc71fbcf47c64027f50a4ff66a8af6187d84a4dcae9e2c639cd42c
|
data/lib/mongoid_rating/model.rb
CHANGED
@@ -16,13 +16,16 @@ module Mongoid
|
|
16
16
|
#
|
17
17
|
# float: whether to allow non-integer rates (default true)
|
18
18
|
#
|
19
|
-
def rateable(field,
|
19
|
+
def rateable(field, opt = {})
|
20
20
|
options = {
|
21
21
|
range: 1..5,
|
22
22
|
rerate: true,
|
23
23
|
counters: true,
|
24
|
-
float: true
|
25
|
-
}.merge(
|
24
|
+
float: true,
|
25
|
+
}.merge(opt)
|
26
|
+
options[:no_rate] ||= options[:float] ? '0.0' : '0'
|
27
|
+
options[:format] ||= options[:float] ? '%.1f' : '%d'
|
28
|
+
|
26
29
|
field = field.to_sym
|
27
30
|
sfield = field.inspect
|
28
31
|
|
@@ -55,6 +58,18 @@ module Mongoid
|
|
55
58
|
where(#{savg}.ne => nil).order_by([#{savg}, :desc])
|
56
59
|
}
|
57
60
|
|
61
|
+
# return user's rate if rated otherwise formatted rate value
|
62
|
+
# good for Raty JS plugin
|
63
|
+
def fmt_#{field}(user = nil)
|
64
|
+
if !user.nil? && #{field}_by?(user)
|
65
|
+
#{options[:format].inspect} % #{field}_by(user)
|
66
|
+
elsif #{field}.nil?
|
67
|
+
#{options[:no_rate].class.name == 'String' ? options[:no_rate].inspect : options[:no_rate]}
|
68
|
+
else
|
69
|
+
#{options[:format].inspect} % #{field}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
58
73
|
def #{field}!(value, rater)
|
59
74
|
if #{options[:float]}
|
60
75
|
value = value.to_f
|
data/spec/comment_spec.rb
CHANGED
@@ -41,6 +41,12 @@ describe Comment do
|
|
41
41
|
@comment1.rate! '2.7', @bob
|
42
42
|
@comment1.rate_values.should eq [2]
|
43
43
|
end
|
44
|
+
|
45
|
+
it 'fmt_rate' do
|
46
|
+
@comment1.fmt_rate.should eq '0'
|
47
|
+
@comment1.rate! '2.0', @bob
|
48
|
+
@comment1.fmt_rate.should eq '2'
|
49
|
+
end
|
44
50
|
end
|
45
51
|
|
46
52
|
context "when rated" do
|
data/spec/post_spec.rb
CHANGED
@@ -28,6 +28,23 @@ describe Post do
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
it 'allows string rate' do
|
32
|
+
@post.rate! '5', @bob
|
33
|
+
@post.rate.should eql 5.0
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'allows string rate' do
|
37
|
+
@post.rate! '3.2', @bob
|
38
|
+
@post.rate.should eql 3.2
|
39
|
+
end
|
40
|
+
it '#fmt_rate' do
|
41
|
+
@post.fmt_rate.should eq '0.0'
|
42
|
+
@post.rate! '3.2', @bob
|
43
|
+
@post.fmt_rate.should eq '3.2'
|
44
|
+
@post.rate! '3', @bob
|
45
|
+
@post.fmt_rate.should eq '3.0'
|
46
|
+
end
|
47
|
+
|
31
48
|
context "when rated" do
|
32
49
|
before (:each) do
|
33
50
|
@post.rate! 1, @bob
|
@@ -53,15 +70,6 @@ describe Post do
|
|
53
70
|
it { expect { @post.rate -17, @sally }.to raise_error() }
|
54
71
|
end
|
55
72
|
|
56
|
-
it 'allows string rate' do
|
57
|
-
@post.rate! '5', @bob
|
58
|
-
@post.rate.should eql 5.0
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'allows string rate' do
|
62
|
-
@post.rate! '3.2', @bob
|
63
|
-
@post.rate.should eql 3.2
|
64
|
-
end
|
65
73
|
end
|
66
74
|
|
67
75
|
describe "#rated?" do
|