mongoid_atomic_votes 0.1.1 → 0.2.0
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.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/lib/mongoid_atomic_votes/atomic_votes.rb +3 -3
- data/lib/mongoid_atomic_votes/version.rb +1 -1
- data/lib/mongoid_atomic_votes/vote.rb +1 -1
- data/mongoid_atomic_votes.gemspec +1 -1
- data/spec/atomic_votes_spec.rb +59 -48
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1471df3a89ef9456b35bc2e078fb8b73d0fc89a9
|
4
|
+
data.tar.gz: 561f2eac6abec232a1966ee23181bbb09b290325
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3583b0c8a866dab57c14cfeef6e98555d7ceab20779e83d89e7722be0e076607ae13d8d0b2ba0a787866e33131892d608792366504bc12fffce836e1ac8da472
|
7
|
+
data.tar.gz: 0151bda82246b40cb8bc37e62fda203d66b2c95a9dcd1fee9f4bd34688e6d2495ba30c30efe3347d9cc4870316745cfd699c92469bf3a5a63b18cb49f7670c25
|
data/Gemfile
CHANGED
@@ -15,8 +15,8 @@ module Mongoid
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def define_scopes(base)
|
18
|
-
base.scope :not_voted, base.where(:vote_value.exists => false)
|
19
|
-
base.scope :voted, base.where(:vote_value.exists => true)
|
18
|
+
base.scope :not_voted, -> { base.where(:vote_value.exists => false) }
|
19
|
+
base.scope :voted, -> { base.where(:vote_value.exists => true) }
|
20
20
|
base.scope :voted_by, ->(resource) do
|
21
21
|
base.where('votes.voted_by_id' => resource.id, 'votes.voter_type' => resource.class.name)
|
22
22
|
end
|
@@ -69,7 +69,7 @@ module Mongoid
|
|
69
69
|
opts['$push'] = {votes: mark.as_json}
|
70
70
|
end
|
71
71
|
|
72
|
-
self.collection.find(_id: self.id).update(opts)
|
72
|
+
self.collection.find(_id: self.id).update(opts)['ok'] > 0
|
73
73
|
end
|
74
74
|
|
75
75
|
def update_vote_value(mark, retract=false)
|
@@ -5,7 +5,7 @@ class Mongoid::AtomicVotes::Vote
|
|
5
5
|
embedded_in :atomic_voteable, polymorphic: true
|
6
6
|
|
7
7
|
field :value, type: Integer
|
8
|
-
field :voted_by_id, type:
|
8
|
+
field :voted_by_id, type: BSON::ObjectId
|
9
9
|
field :voter_type, type: String
|
10
10
|
|
11
11
|
validates_presence_of :value, :voted_by_id, :voter_type
|
data/spec/atomic_votes_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'pry'
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
describe Post do
|
@@ -7,73 +8,79 @@ describe Post do
|
|
7
8
|
end
|
8
9
|
|
9
10
|
it "has Mongoid::AtomicVotes module" do
|
10
|
-
subject.class.ancestors.include
|
11
|
+
expect(subject.class.ancestors).to include(Mongoid::AtomicVotes)
|
11
12
|
end
|
12
13
|
|
13
|
-
it {
|
14
|
-
it {
|
15
|
-
it {
|
16
|
-
it {
|
17
|
-
it {
|
18
|
-
it {
|
14
|
+
it { expect(subject).to respond_to(:vote) }
|
15
|
+
it { expect(subject).to respond_to(:retract) }
|
16
|
+
it { expect(subject).to respond_to(:has_votes?) }
|
17
|
+
it { expect(subject).to respond_to(:vote_count) }
|
18
|
+
it { expect(subject).to respond_to(:vote_value) }
|
19
|
+
it { expect(subject).to respond_to(:votes) }
|
19
20
|
|
20
|
-
it { subject.class.respond_to
|
21
|
-
it {
|
21
|
+
it { expect(subject.class).to respond_to(:set_vote_range) }
|
22
|
+
it { expect { subject.class.set_vote_range(1) }.to raise_exception }
|
22
23
|
|
23
24
|
describe '#votes' do
|
24
25
|
it 'is array of votes' do
|
25
|
-
@post.votes.
|
26
|
+
expect(@post.votes).to be_an_instance_of(Array)
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
30
|
describe 'when does not have votes' do
|
30
31
|
it 'is in not_voted scope' do
|
31
|
-
subject.class.not_voted.include
|
32
|
+
expect(subject.class.not_voted).to include(@post)
|
32
33
|
end
|
33
34
|
|
34
35
|
it 'is not in voted scope' do
|
35
|
-
subject.class.voted.include
|
36
|
+
expect(subject.class.voted).not_to include(@post)
|
36
37
|
end
|
37
38
|
|
38
39
|
it 'is not in voted_by scope' do
|
39
40
|
@users.each do |u|
|
40
|
-
subject.class.voted_by(u).include
|
41
|
+
expect(subject.class.voted_by(u)).not_to include(@post)
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
45
|
it '#vote_count returns 0' do
|
45
|
-
@post.vote_count.
|
46
|
+
expect(@post.vote_count).to eq(0)
|
46
47
|
end
|
47
48
|
|
48
49
|
it '#vote_value returns nil' do
|
49
|
-
@post.vote_value.
|
50
|
+
expect(@post.vote_value).to be_nil
|
50
51
|
end
|
51
52
|
|
52
53
|
it '#votes returns empty array' do
|
53
|
-
@post.votes.
|
54
|
+
expect(@post.votes).to eq([])
|
54
55
|
end
|
55
56
|
|
56
57
|
it '#has_votes? returns false' do
|
57
|
-
@post.vote_count.
|
58
|
-
@post.has_votes
|
58
|
+
expect(@post.vote_count).to eq(0)
|
59
|
+
expect(@post.has_votes?).to be_falsey
|
59
60
|
end
|
60
61
|
|
61
62
|
it '#voted_by? returns false with any resource' do
|
62
|
-
@users.each
|
63
|
+
@users.each do |u|
|
64
|
+
expect(@post.voted_by?(u)).to be_falsey
|
65
|
+
end
|
63
66
|
end
|
64
67
|
|
65
68
|
it '#vote returns true on successful vote' do
|
66
|
-
@users.each
|
69
|
+
@users.each do |u|
|
70
|
+
expect(@post.vote(rand(1..10), u)).to be_truthy
|
71
|
+
end
|
67
72
|
|
68
73
|
vote_value = @post.votes.map(&:value).sum.to_f/@post.votes.size
|
69
74
|
|
70
|
-
@post.vote_value.
|
71
|
-
@post.vote_count.
|
72
|
-
@post.votes.size.
|
73
|
-
|
74
|
-
Post.find(@post.id).
|
75
|
-
|
76
|
-
|
75
|
+
expect(@post.vote_value).to eq(vote_value)
|
76
|
+
expect(@post.vote_count).to eq(@users.size)
|
77
|
+
expect(@post.votes.size).to eq(@users.size)
|
78
|
+
|
79
|
+
Post.find(@post.id).tap do |post|
|
80
|
+
expect(post.vote_value).to eq(vote_value)
|
81
|
+
expect(post.vote_count).to eq(@users.size)
|
82
|
+
expect(post.votes.size).to eq(@users.size)
|
83
|
+
end
|
77
84
|
end
|
78
85
|
end
|
79
86
|
|
@@ -83,59 +90,63 @@ describe Post do
|
|
83
90
|
end
|
84
91
|
|
85
92
|
it 'is not in the not_voted scope' do
|
86
|
-
subject.class.not_voted.include
|
93
|
+
expect(subject.class.not_voted).not_to include(@post)
|
87
94
|
end
|
88
95
|
|
89
96
|
it 'is in voted scope' do
|
90
|
-
subject.class.voted.include
|
97
|
+
expect(subject.class.voted).to include(@post)
|
91
98
|
end
|
92
99
|
|
93
100
|
it 'is in voted_by scope for each voted user' do
|
94
|
-
@users.each
|
101
|
+
@users.each do |u|
|
102
|
+
expect(subject.class.voted_by(u)).to include(@post)
|
103
|
+
end
|
95
104
|
end
|
96
105
|
|
97
106
|
it '#vote_count returns count of votes' do
|
98
|
-
@post.vote_count.
|
107
|
+
expect(@post.vote_count).to eq(@post.votes.size)
|
99
108
|
end
|
100
109
|
|
101
110
|
it '#vote_value returns actual vote value' do
|
102
111
|
vote_value = @post.votes.map(&:value).sum.to_f/@post.votes.size
|
103
|
-
@post.vote_value.
|
112
|
+
expect(@post.vote_value).to eq(vote_value)
|
104
113
|
end
|
105
114
|
|
106
115
|
it '#votes returns array of vote marks' do
|
107
116
|
@post.votes.tap do |votes|
|
108
|
-
votes.class.
|
109
|
-
votes.
|
110
|
-
votes.size.
|
111
|
-
votes.map(&:voted_by_id).sort.
|
117
|
+
expect(votes.class).to eq(Array)
|
118
|
+
expect(votes).not_to be_empty
|
119
|
+
expect(votes.size).to eq(@users.size)
|
120
|
+
expect(votes.map(&:voted_by_id).sort).to eq(@users.map(&:id).sort)
|
112
121
|
end
|
113
122
|
end
|
114
123
|
|
115
124
|
it '#has_votes? returns true' do
|
116
|
-
@post.vote_count.
|
117
|
-
@post.has_votes
|
125
|
+
expect(@post.vote_count).to eq(@post.votes.size)
|
126
|
+
expect(@post.has_votes?).to be_truthy
|
118
127
|
end
|
119
128
|
|
120
129
|
it '#voted_by? returns true for all voted resource' do
|
121
|
-
@users.each
|
130
|
+
@users.each do |u|
|
131
|
+
expect(@post.voted_by?(u)).to be_truthy
|
132
|
+
end
|
122
133
|
end
|
123
134
|
|
124
135
|
it '#retract returns true on successful vote retract' do
|
125
136
|
cnt = @users.size
|
126
137
|
|
127
138
|
@users.each do |u|
|
128
|
-
@post.retract(u).
|
139
|
+
expect(@post.retract(u)).to be_truthy
|
129
140
|
cnt -= 1
|
130
141
|
|
131
142
|
vote_value = @post.votes.size == 0 ? nil : @post.votes.map(&:value).sum.to_f/@post.votes.size
|
132
143
|
|
133
|
-
@post.vote_value.
|
134
|
-
@post.vote_count.
|
135
|
-
@post.vote_count.
|
144
|
+
expect(@post.vote_value).to eq(vote_value)
|
145
|
+
expect(@post.vote_count).to eq(@post.votes.size)
|
146
|
+
expect(@post.vote_count).to eq(cnt)
|
136
147
|
|
137
|
-
Post.find(@post.id).vote_value.
|
138
|
-
Post.find(@post.id).vote_count.
|
148
|
+
expect(Post.find(@post.id).vote_value).to eq(vote_value)
|
149
|
+
expect(Post.find(@post.id).vote_count).to eq(cnt)
|
139
150
|
Post.find(@post.id).votes.size == cnt
|
140
151
|
end
|
141
152
|
end
|
@@ -148,13 +159,13 @@ describe Post do
|
|
148
159
|
end
|
149
160
|
|
150
161
|
it 'Vote has vote_range set up' do
|
151
|
-
Mongoid::AtomicVotes::Vote.vote_range.
|
162
|
+
expect(Mongoid::AtomicVotes::Vote.vote_range).to eq((1..5))
|
152
163
|
end
|
153
164
|
|
154
165
|
it 'Vote is not valid if its value is not in specified vote_range' do
|
155
166
|
@vote = Mongoid::AtomicVotes::Vote.new(value: rand(6..10), voted_by_id: @users.first.id, voter_type: @users.first.class.name)
|
156
|
-
@vote.
|
157
|
-
@vote.errors.messages[:value].first.
|
167
|
+
expect(@vote).not_to be_valid
|
168
|
+
expect(@vote.errors.messages[:value].first).to match(/included in the list/)
|
158
169
|
end
|
159
170
|
end
|
160
171
|
end
|
metadata
CHANGED
@@ -1,38 +1,38 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_atomic_votes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hck
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
27
|
description: Atomic votes for mongoid models
|
28
28
|
email:
|
29
29
|
executables: []
|
30
30
|
extensions: []
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
|
-
- .gitignore
|
34
|
-
- .rspec
|
35
|
-
- .travis.yml
|
33
|
+
- ".gitignore"
|
34
|
+
- ".rspec"
|
35
|
+
- ".travis.yml"
|
36
36
|
- Gemfile
|
37
37
|
- LICENSE
|
38
38
|
- README.md
|
@@ -56,17 +56,17 @@ require_paths:
|
|
56
56
|
- lib
|
57
57
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
63
|
requirements:
|
64
|
-
- -
|
64
|
+
- - ">="
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
69
|
+
rubygems_version: 2.2.2
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: Atomic votes implementation for mongoid
|