voting 0.2.0 → 0.3.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: f0f7788833c3a076eb925aac474527ff11af91bf
4
- data.tar.gz: b49e89a0bfae1c14bfbaafc785b49901d3d987ea
3
+ metadata.gz: cb95d0887656cb736919fb5ab9fda2b3689dc3d3
4
+ data.tar.gz: 5078155552f020da3f0a75434caceeb81b8c1355
5
5
  SHA512:
6
- metadata.gz: 285ef836d6e3468fdd140d20342b6a6dd2eb728f323e1265632c0591e49d3e74abed06faac44bb0e0c2f15fa0b570e7ce94ec6dd5503d3cb0ce891fe63de8d3e
7
- data.tar.gz: eabf0323ba058330e694692f5fe9611234410f2335b6a0bae6500acc096a3d214f18b24298214cd118ce23cc5701728c2f98492dec90d22f8701b7c7332818b0
6
+ metadata.gz: c9b1fc20e7a7d4b4524121a54ec98bf3f98f6e835ff431466fba4aa2b55eeedba562b9e6b07ad86b4209da3a9bb65db3a1b56c8940c53a2576de686daec07df6
7
+ data.tar.gz: 02d4e5c839d2a71fb8209bbf35cee97d15532491c4a9ff968de4f3054e2fdd3becdd694e7c30f4e68cdb2c14a09984d16b3f4588b46269a91b53148350037268
@@ -1,3 +1,13 @@
1
+ ## v0.3.0
2
+
3
+ ### News
4
+
5
+ - Added method `status` to return the status of some vote record.
6
+
7
+ ### Updates
8
+
9
+ - The voting warm up registers is only made on creation to avoid overhead.
10
+
1
11
  ## v0.2.0
2
12
 
3
13
  ### News
data/README.md CHANGED
@@ -58,6 +58,19 @@ author_1.vote(resource, 1) # +1 vote
58
58
  author_2.vote(resource, -1) # -1 vote
59
59
  ```
60
60
 
61
+ ### status
62
+
63
+ This mehod will return the status of a `Voting::Vote` object as `positive`, `negative` or `none`.
64
+
65
+ ```ruby
66
+ author = Author.first
67
+ resource = Comment.last
68
+
69
+ author.vote(resource, 1).status # 'positive'
70
+ author.vote(resource, -1) # 'negative'
71
+ author.vote(resource, -1) # 'none'
72
+ ```
73
+
61
74
  ### voting
62
75
 
63
76
  A voted resource exposes a cached data about it state:
@@ -56,7 +56,7 @@ module Voting
56
56
 
57
57
  module ClassMethods
58
58
  def voting(as: nil, scoping: nil)
59
- after_save -> { voting_warm_up scoping: scoping }, unless: -> { as == :author }
59
+ after_create -> { voting_warm_up scoping: scoping }, unless: -> { as == :author }
60
60
 
61
61
  has_many :voting_records,
62
62
  as: :resource,
@@ -18,6 +18,12 @@ module Voting
18
18
  scope: %i[author_type resource_id resource_type scopeable_id scopeable_type]
19
19
  }
20
20
 
21
+ def status
22
+ return 'positive' if positive == 1
23
+
24
+ negative == 1 ? 'negative' : 'none'
25
+ end
26
+
21
27
  def self.create(author:, resource:, scopeable: nil, value:)
22
28
  record = find_or_initialize_by(author: author, resource: resource, scopeable: scopeable)
23
29
  attribute = value.positive? ? :positive : :negative
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Voting
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe Voting::Extension, 'after_save' do
5
+ RSpec.describe Voting::Extension, 'after_create' do
6
6
  context 'when record is author' do
7
7
  let!(:record) { build :author }
8
8
 
@@ -33,5 +33,15 @@ RSpec.describe Voting::Extension, 'after_save' do
33
33
  record.save
34
34
  end
35
35
  end
36
+
37
+ context 'when update is made' do
38
+ let!(:record) { create :comment }
39
+
40
+ it 'does not warm up the cache' do
41
+ expect(record).not_to receive(:voting_warm_up)
42
+
43
+ record.save
44
+ end
45
+ end
36
46
  end
37
47
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe Voting::Vote, '.status' do
6
+ let!(:author) { create :author }
7
+ let!(:resource) { create :comment }
8
+
9
+ context 'with no scope' do
10
+
11
+ it 'returns the status' do
12
+ expect(author.up(resource).status).to eq 'positive'
13
+ expect(author.down(resource).status).to eq 'negative'
14
+ expect(author.down(resource).status).to eq 'none'
15
+ end
16
+ end
17
+
18
+ context 'with scope' do
19
+ let!(:scope) { create :category }
20
+
21
+ it 'returns the status' do
22
+ expect(author.up(resource, scope: scope).status).to eq 'positive'
23
+ expect(author.down(resource, scope: scope).status).to eq 'negative'
24
+ expect(author.down(resource, scope: scope).status).to eq 'none'
25
+ end
26
+ end
27
+
28
+ it 'respect the scope' do
29
+ no_scope = author.up(resource)
30
+ with_scope = author.down(resource, scope: create(:category))
31
+
32
+ expect(no_scope.status).to eq 'positive'
33
+ expect(with_scope.status).to eq 'negative'
34
+ end
35
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-29 00:00:00.000000000 Z
11
+ date: 2018-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -151,7 +151,7 @@ files:
151
151
  - spec/factories/comment.rb
152
152
  - spec/factories/voting/vote.rb
153
153
  - spec/factories/voting/voting.rb
154
- - spec/models/extension/after_save_spec.rb
154
+ - spec/models/extension/after_create_spec.rb
155
155
  - spec/models/extension/as_spec.rb
156
156
  - spec/models/extension/down_spec.rb
157
157
  - spec/models/extension/order_by_voting_spec.rb
@@ -167,6 +167,7 @@ files:
167
167
  - spec/models/extension/voting_spec.rb
168
168
  - spec/models/extension/voting_warm_up_spec.rb
169
169
  - spec/models/vote/create_spec.rb
170
+ - spec/models/vote/status_spec.rb
170
171
  - spec/models/vote/vote_for_spec.rb
171
172
  - spec/models/vote_spec.rb
172
173
  - spec/models/voting/update_voting_spec.rb
@@ -214,6 +215,7 @@ summary: A Binomial proportion confidence interval voting system with scope and
214
215
  enabled.
215
216
  test_files:
216
217
  - spec/models/vote/vote_for_spec.rb
218
+ - spec/models/vote/status_spec.rb
217
219
  - spec/models/vote/create_spec.rb
218
220
  - spec/models/voting_spec.rb
219
221
  - spec/models/vote_spec.rb
@@ -228,8 +230,8 @@ test_files:
228
230
  - spec/models/extension/voting_records_spec.rb
229
231
  - spec/models/extension/as_spec.rb
230
232
  - spec/models/extension/voted_spec.rb
233
+ - spec/models/extension/after_create_spec.rb
231
234
  - spec/models/extension/votes_spec.rb
232
- - spec/models/extension/after_save_spec.rb
233
235
  - spec/models/extension/votes_records_spec.rb
234
236
  - spec/models/extension/up_spec.rb
235
237
  - spec/models/voting/update_voting_spec.rb