mongoid_atomic_votes 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/lib/mongoid_atomic_votes/atomic_votes.rb +8 -8
- data/lib/mongoid_atomic_votes/version.rb +1 -1
- data/lib/mongoid_atomic_votes/vote.rb +5 -5
- data/lib/mongoid_atomic_votes.rb +3 -3
- data/mongoid_atomic_votes.gemspec +1 -1
- data/spec/atomic_votes_spec.rb +3 -3
- data/spec/spec_helper.rb +10 -13
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 474842edee2815d2d65750077a75894ac7f67dc0
|
4
|
+
data.tar.gz: 414e249a6a8bd8c9bfb4852381f7d47f6131bdf7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7e410e7eb861576dde400586ee450bf7514ce6202a4c14b5aa01b4c2b9b97339cfd6d06cde5006ca21ab6d62f2d9a616bcb76b7a916904cdaf509fa6582b6ad
|
7
|
+
data.tar.gz: 2bbbc2ff81776a89cef0f4878d6bd3ff6f72c7cc6338d97c96c2b8c71cabda3cfb653fe01cf0abc33a049814e0126f2427da34515ebad156ab7aa82b2ad8e875
|
data/Gemfile
CHANGED
@@ -10,7 +10,7 @@ module Mongoid
|
|
10
10
|
private
|
11
11
|
def define_relations(base)
|
12
12
|
base.field :vote_count, type: Integer, default: 0
|
13
|
-
base.field :vote_value, type: Float,
|
13
|
+
base.field :vote_value, type: Float, default: nil
|
14
14
|
base.embeds_many :votes, class_name: 'Mongoid::AtomicVotes::Vote', as: :atomic_voteable
|
15
15
|
end
|
16
16
|
|
@@ -59,21 +59,21 @@ module Mongoid
|
|
59
59
|
private
|
60
60
|
def update_votes(mark, retract=false)
|
61
61
|
opts = {
|
62
|
-
'$inc' => {vote_count: retract ? -1 : 1},
|
63
|
-
'$set' => {vote_value: self.vote_value}
|
62
|
+
'$inc' => { vote_count: retract ? -1 : 1 },
|
63
|
+
'$set' => { vote_value: self.vote_value }
|
64
64
|
}
|
65
65
|
|
66
66
|
if retract
|
67
|
-
opts['$pull'] = {votes: {_id: mark.id}}
|
67
|
+
opts['$pull'] = { votes: { _id: mark.id } }
|
68
68
|
else
|
69
|
-
opts['$push'] = {votes: mark.as_json}
|
69
|
+
opts['$push'] = { votes: mark.as_json }
|
70
70
|
end
|
71
71
|
|
72
|
-
self.collection.find(_id: self.id).
|
72
|
+
self.collection.find(_id: self.id).update_one(opts).modified_count > 0
|
73
73
|
end
|
74
74
|
|
75
75
|
def update_vote_value(mark, retract=false)
|
76
|
-
value, vote_count_diff = [mark.value, 1].map{|v| v * (retract ? -1 : 1)}
|
76
|
+
value, vote_count_diff = [mark.value, 1].map { |v| v * (retract ? -1 : 1) }
|
77
77
|
self.vote_value = if self.vote_count == 1 && retract
|
78
78
|
nil
|
79
79
|
else
|
@@ -92,7 +92,7 @@ module Mongoid
|
|
92
92
|
|
93
93
|
def remove_vote_mark(mark)
|
94
94
|
_assigning do
|
95
|
-
self.votes.reject!{|v| v.id == mark.id}
|
95
|
+
self.votes.reject! { |v| v.id == mark.id }
|
96
96
|
update_vote_value(mark, true)
|
97
97
|
end
|
98
98
|
update_votes(mark, true)
|
@@ -4,14 +4,14 @@ class Mongoid::AtomicVotes::Vote
|
|
4
4
|
|
5
5
|
embedded_in :atomic_voteable, polymorphic: true
|
6
6
|
|
7
|
-
field :value,
|
7
|
+
field :value, type: Integer
|
8
8
|
field :voted_by_id, type: BSON::ObjectId
|
9
|
-
field :voter_type,
|
9
|
+
field :voter_type, type: String
|
10
10
|
|
11
|
-
validates_presence_of
|
12
|
-
validates_inclusion_of :value, in: ->(vote){ vote.class.vote_range }, if: ->(vote){ vote.class.vote_range }
|
11
|
+
validates_presence_of :value, :voted_by_id, :voter_type
|
12
|
+
validates_inclusion_of :value, in: ->(vote) { vote.class.vote_range }, if: ->(vote) { vote.class.vote_range }
|
13
13
|
|
14
|
-
index({voted_by_id: 1, mark: 1},
|
14
|
+
index({ voted_by_id: 1, mark: 1 }, unique: true, background: true)
|
15
15
|
|
16
16
|
class << self
|
17
17
|
@@vote_range = nil
|
data/lib/mongoid_atomic_votes.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'mongoid_atomic_votes/version'
|
2
|
+
require 'mongoid_atomic_votes/atomic_votes'
|
3
|
+
require 'mongoid_atomic_votes/vote'
|
data/spec/atomic_votes_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe Post do
|
|
7
7
|
@post = FactoryGirl.create(:post)
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'has Mongoid::AtomicVotes module' do
|
11
11
|
expect(subject.class.ancestors).to include(Mongoid::AtomicVotes)
|
12
12
|
end
|
13
13
|
|
@@ -19,7 +19,7 @@ describe Post do
|
|
19
19
|
it { expect(subject).to respond_to(:votes) }
|
20
20
|
|
21
21
|
it { expect(subject.class).to respond_to(:set_vote_range) }
|
22
|
-
it { expect { subject.class.set_vote_range(1) }.to
|
22
|
+
it { expect { subject.class.set_vote_range(1) }.to raise_error('argument should be a Range') }
|
23
23
|
|
24
24
|
describe '#votes' do
|
25
25
|
it 'is array of votes' do
|
@@ -71,7 +71,7 @@ describe Post do
|
|
71
71
|
end
|
72
72
|
|
73
73
|
vote_value = @post.votes.map(&:value).sum.to_f/@post.votes.size
|
74
|
-
|
74
|
+
|
75
75
|
expect(@post.vote_value).to eq(vote_value)
|
76
76
|
expect(@post.vote_count).to eq(@users.size)
|
77
77
|
expect(@post.votes.size).to eq(@users.size)
|
data/spec/spec_helper.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
-
|
4
1
|
MODELS = File.join(File.dirname(__FILE__), 'models')
|
5
2
|
|
3
|
+
require 'rubygems'
|
4
|
+
require 'database_cleaner'
|
5
|
+
require 'factory_girl'
|
6
6
|
require 'simplecov'
|
7
7
|
|
8
|
-
SimpleCov.start
|
8
|
+
SimpleCov.start do
|
9
|
+
add_filter '/.gems/'
|
10
|
+
add_filter '/.bundle/'
|
11
|
+
end
|
9
12
|
|
10
|
-
require 'rubygems'
|
11
13
|
require 'mongoid'
|
12
14
|
require 'mongoid_atomic_votes'
|
13
|
-
require 'database_cleaner'
|
14
|
-
require 'factory_girl'
|
15
15
|
|
16
16
|
Dir["#{MODELS}/*.rb"].each { |f| require f }
|
17
17
|
|
@@ -19,11 +19,8 @@ Mongoid.configure do |config|
|
|
19
19
|
config.connect_to 'mongoid_atomic_votes_test'
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
#Mongoid.logger.level = Logger::DEBUG
|
26
|
-
#Moped.logger.level = Logger::DEBUG
|
22
|
+
Mongoid.logger.level = Logger::ERROR
|
23
|
+
Mongo::Logger.logger.level = Logger::ERROR
|
27
24
|
|
28
25
|
FactoryGirl.definition_file_paths = [File.join(File.dirname(__FILE__), 'factories')]
|
29
26
|
FactoryGirl.find_definitions
|
@@ -42,4 +39,4 @@ RSpec.configure do |config|
|
|
42
39
|
config.after(:each) do
|
43
40
|
DatabaseCleaner.clean
|
44
41
|
end
|
45
|
-
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_atomic_votes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
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: 2015-11-24 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: '5.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: '5.0'
|
27
27
|
description: Atomic votes for mongoid models
|
28
28
|
email:
|
29
29
|
executables: []
|
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
69
|
+
rubygems_version: 2.4.5
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: Atomic votes implementation for mongoid
|