voting 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +47 -19
- data/lib/voting/models/voting/extension.rb +6 -2
- data/lib/voting/version.rb +1 -1
- data/spec/models/extension/voted_question_spec.rb +88 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0f7788833c3a076eb925aac474527ff11af91bf
|
4
|
+
data.tar.gz: b49e89a0bfae1c14bfbaafc785b49901d3d987ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 285ef836d6e3468fdd140d20342b6a6dd2eb728f323e1265632c0591e49d3e74abed06faac44bb0e0c2f15fa0b570e7ce94ec6dd5503d3cb0ce891fe63de8d3e
|
7
|
+
data.tar.gz: eabf0323ba058330e694692f5fe9611234410f2335b6a0bae6500acc096a3d214f18b24298214cd118ce23cc5701728c2f98492dec90d22f8701b7c7332818b0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/wbotelhos/voting.svg)](https://travis-ci.org/wbotelhos/voting)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/voting.svg)](https://badge.fury.io/rb/voting)
|
5
|
-
[![
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/dcdf51e3093148c5ac6a/maintainability)](https://codeclimate.com/github/wbotelhos/voting/maintainability)
|
6
|
+
[![Support](https://img.shields.io/badge/donate-%3C3-brightgreen.svg)](https://liberapay.com/wbotelhos)
|
6
7
|
|
7
8
|
A Binomial proportion confidence interval voting system with scope and cache enabled.
|
8
9
|
|
@@ -115,6 +116,13 @@ resource = Comment.last
|
|
115
116
|
author.voted? resource
|
116
117
|
```
|
117
118
|
|
119
|
+
If you want to know if the vote was `positive` or `negative`, just pass a symbol about it:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
author.voted? resource, :negative
|
123
|
+
author.voted? resource, :positive
|
124
|
+
```
|
125
|
+
|
118
126
|
### votes
|
119
127
|
|
120
128
|
You can retrieve all votes received by some resource:
|
@@ -182,40 +190,60 @@ An author model still can be voted, but won't generate a Voting record with all
|
|
182
190
|
voting as: :author
|
183
191
|
```
|
184
192
|
|
185
|
-
###
|
193
|
+
### Alias
|
186
194
|
|
187
|
-
|
195
|
+
You can to use alias to directly call `vote` with positive or negative data.
|
188
196
|
|
189
197
|
```ruby
|
190
|
-
|
191
|
-
|
192
|
-
|
198
|
+
author = Author.last
|
199
|
+
resource = Comment.last
|
200
|
+
|
201
|
+
author.up resource # +1
|
202
|
+
author.down resource # -1
|
193
203
|
```
|
194
204
|
|
195
|
-
|
205
|
+
#### Options
|
206
|
+
|
207
|
+
`down`: makes a negative vote;
|
196
208
|
|
197
|
-
|
209
|
+
`up`: makes a positive vote;
|
210
|
+
|
211
|
+
### Toggle
|
198
212
|
|
199
213
|
The toggle functions works out of box, so if you vote up twice or vote twice down, the vote will be canceled.
|
200
214
|
When you do that, the vote record is **not destroyed** instead, it receives zero on `negative` and `positive` column.
|
201
215
|
|
202
|
-
|
216
|
+
### Scope
|
203
217
|
|
204
|
-
|
218
|
+
All methods accepts a `scope` param to be persisted with the vote or to be searched:
|
205
219
|
|
206
220
|
```ruby
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
author.up
|
211
|
-
author.
|
221
|
+
category = Category.last
|
222
|
+
|
223
|
+
author.down resource, scope: category
|
224
|
+
author.up resource, scope: category
|
225
|
+
author.vote resource, 1, scope: category
|
226
|
+
author.vote_for resource, scope: category
|
227
|
+
author.voted resource, scope: category
|
228
|
+
author.voted? resource, :negative, scope: category
|
229
|
+
author.voted? resource, :positive, scope: category
|
230
|
+
|
231
|
+
resource.votes scope: category
|
232
|
+
author .voted scope: category
|
233
|
+
resource.votign scope: category
|
212
234
|
```
|
213
235
|
|
214
|
-
###
|
236
|
+
### Scoping
|
215
237
|
|
216
|
-
|
238
|
+
If you need to warm up a record with scope, you need to setup the `scoping` relation.
|
217
239
|
|
218
|
-
|
240
|
+
```ruby
|
241
|
+
class Resource < ApplicationRecord
|
242
|
+
voting scoping: :categories
|
243
|
+
end
|
244
|
+
```
|
245
|
+
|
246
|
+
Now, when a resource is created, the cache will be generated for each related `category` as `scopeable`.
|
219
247
|
|
220
248
|
### References
|
221
249
|
|
@@ -226,4 +254,4 @@ author.down resource # -1
|
|
226
254
|
|
227
255
|
## Love it!
|
228
256
|
|
229
|
-
Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=voting) or [
|
257
|
+
Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=voting) or [Support](https://liberapay.com/wbotelhos). Thanks! (:
|
@@ -21,8 +21,12 @@ module Voting
|
|
21
21
|
Vote.vote_for author: self, resource: resource, scopeable: scope
|
22
22
|
end
|
23
23
|
|
24
|
-
def voted?(resource, scope: nil)
|
25
|
-
|
24
|
+
def voted?(resource, value = nil, scope: nil)
|
25
|
+
query = { author: self, resource: resource, scopeable: scope }
|
26
|
+
|
27
|
+
query[value] = 1 unless value.nil?
|
28
|
+
|
29
|
+
Vote.exists? query
|
26
30
|
end
|
27
31
|
|
28
32
|
def votes(scope: nil)
|
data/lib/voting/version.rb
CHANGED
@@ -7,32 +7,108 @@ RSpec.describe Voting::Extension, ':voted?' do
|
|
7
7
|
let!(:comment) { create :comment }
|
8
8
|
|
9
9
|
context 'with no scopeable' do
|
10
|
-
context '
|
11
|
-
|
10
|
+
context 'with no specific value' do
|
11
|
+
context 'when has no vote for the given resource' do
|
12
|
+
specify { expect(author.voted?(comment)).to eq false }
|
13
|
+
end
|
12
14
|
|
13
|
-
|
15
|
+
context 'when has vote for the given resource' do
|
16
|
+
before { author.up comment }
|
17
|
+
|
18
|
+
specify { expect(author.voted?(comment)).to eq true }
|
19
|
+
end
|
14
20
|
end
|
15
21
|
|
16
|
-
context '
|
17
|
-
|
22
|
+
context 'with specific value' do
|
23
|
+
context 'with negative' do
|
24
|
+
context 'when has no vote for the given resource' do
|
25
|
+
specify { expect(author.voted?(comment, :negative)).to eq false }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when has vote for the given resource' do
|
29
|
+
before { author.down comment }
|
30
|
+
|
31
|
+
specify { expect(author.voted?(comment, :negative)).to eq true }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when has vote for the given resource but positive' do
|
35
|
+
before { author.up comment }
|
36
|
+
|
37
|
+
specify { expect(author.voted?(comment, :negative)).to eq false }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with positive' do
|
42
|
+
context 'when has no vote for the given resource' do
|
43
|
+
specify { expect(author.voted?(comment, :positive)).to eq false }
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when has vote for the given resource' do
|
47
|
+
before { author.up comment }
|
48
|
+
|
49
|
+
specify { expect(author.voted?(comment, :positive)).to eq true }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when has vote for the given resource but negative' do
|
53
|
+
before { author.down comment }
|
18
54
|
|
19
|
-
|
55
|
+
specify { expect(author.voted?(comment, :positive)).to eq false }
|
56
|
+
end
|
57
|
+
end
|
20
58
|
end
|
21
59
|
end
|
22
60
|
|
23
61
|
context 'with scopeable' do
|
24
62
|
let!(:category) { build :category }
|
25
63
|
|
26
|
-
context '
|
27
|
-
|
64
|
+
context 'with no specific value' do
|
65
|
+
context 'when has no vote for the given resource' do
|
66
|
+
specify { expect(author.voted?(comment, scope: category)).to eq false }
|
67
|
+
end
|
28
68
|
|
29
|
-
|
69
|
+
context 'when has vote for the given resource' do
|
70
|
+
before { author.up comment, scope: category }
|
71
|
+
|
72
|
+
specify { expect(author.voted?(comment, scope: category)).to eq true }
|
73
|
+
end
|
30
74
|
end
|
31
75
|
|
32
|
-
context '
|
33
|
-
|
76
|
+
context 'with specific value' do
|
77
|
+
context 'with negative' do
|
78
|
+
context 'when has no vote for the given resource' do
|
79
|
+
specify { expect(author.voted?(comment, :negative, scope: category)).to eq false }
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'when has vote for the given resource' do
|
83
|
+
before { author.down comment, scope: category }
|
84
|
+
|
85
|
+
specify { expect(author.voted?(comment, :negative, scope: category)).to eq true }
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when has vote for the given resource but positive' do
|
89
|
+
before { author.up comment, scope: category }
|
90
|
+
|
91
|
+
specify { expect(author.voted?(comment, :negative, scope: category)).to eq false }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'with positive' do
|
96
|
+
context 'when has no vote for the given resource' do
|
97
|
+
specify { expect(author.voted?(comment, :positive, scope: category)).to eq false }
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'when has vote for the given resource' do
|
101
|
+
before { author.up comment, scope: category }
|
102
|
+
|
103
|
+
specify { expect(author.voted?(comment, :positive, scope: category)).to eq true }
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when has vote for the given resource negative' do
|
107
|
+
before { author.down comment, scope: category }
|
34
108
|
|
35
|
-
|
109
|
+
specify { expect(author.voted?(comment, :positive, scope: category)).to eq false }
|
110
|
+
end
|
111
|
+
end
|
36
112
|
end
|
37
113
|
end
|
38
114
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Botelho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|