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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4aa7764b2a518434df6182d2f0a0101b9d3079c
4
- data.tar.gz: e6a96257d3a5f67a6b8224f21d538285db21c79c
3
+ metadata.gz: f0f7788833c3a076eb925aac474527ff11af91bf
4
+ data.tar.gz: b49e89a0bfae1c14bfbaafc785b49901d3d987ea
5
5
  SHA512:
6
- metadata.gz: '01686bdfc40e30e8ce89208e3f3b2e21425382abc2fb7f9dee4f9f2205e5b63886e8564703198d2e6cae610866a839aa8bfa0352cb00ac5d46fe80e1104096ae'
7
- data.tar.gz: d3aa1a7f97da1fa5b2c145e7887beb74ab8277007e89b71a70d9baee8a4bc94138c1236c2e4c6e4c58f1e17ea6b5d744794ecf51d6bd8e3f40623a6b106f80e1
6
+ metadata.gz: 285ef836d6e3468fdd140d20342b6a6dd2eb728f323e1265632c0591e49d3e74abed06faac44bb0e0c2f15fa0b570e7ce94ec6dd5503d3cb0ce891fe63de8d3e
7
+ data.tar.gz: eabf0323ba058330e694692f5fe9611234410f2335b6a0bae6500acc096a3d214f18b24298214cd118ce23cc5701728c2f98492dec90d22f8701b7c7332818b0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## v0.2.0
2
+
3
+ ### News
4
+
5
+ - The Rails dependency was reduced to only activerecord;
6
+ - The `voted?` method now support ask if the vote was `positive` or `negative`.
7
+
1
8
  ## v0.1.0
2
9
 
3
10
  - First release.
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
- [![LiberPay](https://img.shields.io/badge/donate-%3C3-brightgreen.svg)](https://liberapay.com/wbotelhos)
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
- ### Scoping
193
+ ### Alias
186
194
 
187
- If you need to warm up a record with scope, you need to setup the `scoping` relation.
195
+ You can to use alias to directly call `vote` with positive or negative data.
188
196
 
189
197
  ```ruby
190
- class Resource < ApplicationRecord
191
- voting scoping: :categories
192
- end
198
+ author = Author.last
199
+ resource = Comment.last
200
+
201
+ author.up resource # +1
202
+ author.down resource # -1
193
203
  ```
194
204
 
195
- Now, when a resource is created, the cache will be generated for each related `category` as `scopeable`.
205
+ #### Options
206
+
207
+ `down`: makes a negative vote;
196
208
 
197
- # Toggle
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
- # Alias
216
+ ### Scope
203
217
 
204
- You can to use alias to directly call `vote` with positive or negative data.
218
+ All methods accepts a `scope` param to be persisted with the vote or to be searched:
205
219
 
206
220
  ```ruby
207
- author = Author.last
208
- resource = Comment.last
209
-
210
- author.up resource # +1
211
- author.down resource # -1
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
- ### Options
236
+ ### Scoping
215
237
 
216
- `down`: makes a negative vote;
238
+ If you need to warm up a record with scope, you need to setup the `scoping` relation.
217
239
 
218
- `up`: makes a positive vote;
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 [LiberPay](https://liberapay.com/wbotelhos). Thanks! (:
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
- !vote_for(resource, scope: scope).nil?
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Voting
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -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 'when has no vote for the given resource' do
11
- before { allow(author).to receive(:vote_for).with(comment, scope: nil) { nil } }
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
- specify { expect(author.voted?(comment)).to eq false }
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 'when has vote for the given resource' do
17
- before { allow(author).to receive(:vote_for).with(comment, scope: nil) { double } }
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
- specify { expect(author.voted?(comment)).to eq true }
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 'when has no vote for the given resource' do
27
- before { allow(author).to receive(:vote_for).with(comment, scope: category) { nil } }
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
- specify { expect(author.voted?(comment, scope: category)).to eq false }
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 'when has vote for the given resource' do
33
- before { allow(author).to receive(:vote_for).with(comment, scope: category) { double } }
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
- specify { expect(author.voted?(comment, scope: category)).to eq true }
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.1.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-22 00:00:00.000000000 Z
11
+ date: 2018-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="