mongoid-votable 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +16 -1
- data/lib/mongoid/votable.rb +34 -33
- data/lib/mongoid/votable/version.rb +1 -1
- data/mongoid-votable.gemspec +1 -0
- data/spec/config/mongoid.yml +6 -0
- data/spec/factories/topics.rb +5 -0
- data/spec/factories/users.rb +5 -0
- data/spec/models/topic.rb +6 -0
- data/spec/models/user.rb +4 -0
- data/spec/mongoid/votable_spec.rb +149 -0
- data/spec/spec_helper.rb +35 -0
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a34ae76962b7b600d4201a76731e1bbdbea222cd
|
4
|
+
data.tar.gz: 4c64cf417ceab57073205364e07f71d6960924ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1af78cf0eb8cb66304e50fe9f06a4a21a4f4e19179e1c7597c4b6c2c3ef857a1e92a6adea7c4de72e895f79a237a58c6b3b0b2214320c24f14e7a2df9bc91659
|
7
|
+
data.tar.gz: 9a9c15021bf6a66a3982bb6d8ec4f7f49d6221677376e61a4f3cef6748353d5bed1d8255b7d38b1b72599fd58d663d933512af59b68af72193a2488a4a016566
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -18,7 +18,22 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
~~~ruby
|
22
|
+
class Topic
|
23
|
+
include Mongoid::Document
|
24
|
+
include Mongoid::Votable
|
25
|
+
|
26
|
+
end
|
27
|
+
~~~
|
28
|
+
|
29
|
+
~~~ruby
|
30
|
+
topic.vote!(1, user)
|
31
|
+
topic.vote!(-1, user)
|
32
|
+
|
33
|
+
topic.vote!(10, user)
|
34
|
+
|
35
|
+
topics = Topic.voted_by(user)
|
36
|
+
~~~
|
22
37
|
|
23
38
|
## Contributing
|
24
39
|
|
data/lib/mongoid/votable.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
module Mongoid
|
3
3
|
module Votable
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
included do
|
7
7
|
field :votes_count, type: Integer, default: 0
|
8
8
|
field :votes_average, type: Integer, default: 0
|
@@ -13,7 +13,7 @@ module Mongoid
|
|
13
13
|
embeds_many :votes, as: :votable, class_name: "Mongoid::Vote"
|
14
14
|
|
15
15
|
scope :voted_by, ->(voter) do
|
16
|
-
where("votes.
|
16
|
+
where("votes.voter_id" => voter.id)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -53,38 +53,39 @@ module Mongoid
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
56
|
+
private
|
57
|
+
def do_vote!(vote, value, status)
|
58
|
+
case status
|
59
|
+
when :created
|
60
|
+
if value > 0
|
61
|
+
inc(:votes_up, 1)
|
62
|
+
else
|
63
|
+
inc(:votes_down, 1)
|
64
|
+
end
|
65
|
+
inc(:votes_count, 1)
|
66
|
+
when :updated
|
67
|
+
#Origin is vote down, but now is vote up
|
68
|
+
if vote.value - value < 0 && vote.value > 0
|
69
|
+
inc(:votes_up, 1)
|
70
|
+
inc(:votes_down, -1)
|
71
|
+
#Origin is vote up, but now is vote down
|
72
|
+
elsif vote.value - value > 0 && vote.value < 0
|
73
|
+
inc(:votes_down, 1)
|
74
|
+
inc(:votes_up, -1)
|
75
|
+
end
|
76
|
+
when :destroyed
|
77
|
+
if value > 0
|
78
|
+
inc(:votes_down, -1)
|
79
|
+
else
|
80
|
+
inc(:votes_up, -1)
|
81
|
+
end
|
82
|
+
inc(:votes_count, -1)
|
78
83
|
else
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
end
|
85
|
-
inc(:votes_average, value)
|
86
|
-
#set(:votes_average, votes.map(&:value).reduce(0, &:+))
|
87
|
-
end
|
84
|
+
raise "Not accept status"
|
85
|
+
end
|
86
|
+
inc(:votes_average, value)
|
87
|
+
#set(:votes_average, votes.map(&:value).reduce(0, &:+))
|
88
|
+
end
|
88
89
|
|
89
90
|
|
90
91
|
module ClassMethods
|
data/mongoid-votable.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_dependency("mongoid", [">= 3.0.0"])
|
22
22
|
spec.add_dependency("activesupport", [">= 3.0.0"])
|
23
23
|
|
24
|
+
spec.add_development_dependency "factory_girl"
|
24
25
|
spec.add_development_dependency "rspec"
|
25
26
|
spec.add_development_dependency "bundler"
|
26
27
|
spec.add_development_dependency "rake"
|
data/spec/models/user.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
describe Votable do
|
5
|
+
it { Topic.should respond_to(:voted_by) }
|
6
|
+
|
7
|
+
let(:user) { create(:user) }
|
8
|
+
let(:user2) { create(:user) }
|
9
|
+
let(:topic) { create(:topic) }
|
10
|
+
|
11
|
+
subject { topic }
|
12
|
+
|
13
|
+
it { should respond_to(:votes, :votes_count, :votes_up, :votes_down, :votes_average) }
|
14
|
+
|
15
|
+
its(:votes_count) { should == 0 }
|
16
|
+
its(:votes_up) { should == 0 }
|
17
|
+
its(:votes_down) { should == 0 }
|
18
|
+
its(:votes_average) { should == 0 }
|
19
|
+
its(:votes) { should be_empty }
|
20
|
+
|
21
|
+
context "single user" do
|
22
|
+
|
23
|
+
context "user vote up" do
|
24
|
+
before(:each) { topic.vote!(1, user) }
|
25
|
+
its(:votes_count) { should == 1 }
|
26
|
+
its(:votes_up) { should == 1 }
|
27
|
+
its(:votes_down) { should == 0 }
|
28
|
+
its(:votes_average) { should == 1 }
|
29
|
+
its(:votes) { should_not be_empty }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "user vote down" do
|
33
|
+
before(:each) { topic.vote!(-1, user) }
|
34
|
+
its(:votes_count) { should == 1 }
|
35
|
+
its(:votes_up) { should == 0 }
|
36
|
+
its(:votes_down) { should == 1 }
|
37
|
+
its(:votes_average) { should == -1 }
|
38
|
+
its(:votes) { should_not be_empty }
|
39
|
+
end
|
40
|
+
|
41
|
+
context "user vote up, then vote down with same value" do
|
42
|
+
before(:each) do
|
43
|
+
topic.vote!(3, user)
|
44
|
+
topic.vote!(-3, user)
|
45
|
+
end
|
46
|
+
its(:votes_count) { should == 0 }
|
47
|
+
its(:votes_up) { should == 0 }
|
48
|
+
its(:votes_down) { should == 0 }
|
49
|
+
its(:votes_average) { should == 0 }
|
50
|
+
its(:votes) { should be_empty }
|
51
|
+
end
|
52
|
+
|
53
|
+
context "user vote up, then vote down, (down > up)" do
|
54
|
+
before(:each) do
|
55
|
+
topic.vote!(3, user)
|
56
|
+
topic.vote!(-6, user)
|
57
|
+
end
|
58
|
+
its(:votes_count) { should == 1 }
|
59
|
+
its(:votes_up) { should == 0 }
|
60
|
+
its(:votes_down) { should == 1 }
|
61
|
+
its(:votes_average) { should == -3 }
|
62
|
+
its(:votes) { should_not be_empty }
|
63
|
+
end
|
64
|
+
|
65
|
+
context "user vote up, then vote down, (down < up)" do
|
66
|
+
before(:each) do
|
67
|
+
topic.vote!(3, user)
|
68
|
+
topic.vote!(-1, user)
|
69
|
+
end
|
70
|
+
its(:votes_count) { should == 1 }
|
71
|
+
its(:votes_up) { should == 1 }
|
72
|
+
its(:votes_down) { should == 0 }
|
73
|
+
its(:votes_average) { should == 2 }
|
74
|
+
its(:votes) { should_not be_empty }
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
context "multiple users" do
|
80
|
+
|
81
|
+
context "users all vote up" do
|
82
|
+
before(:each) do
|
83
|
+
topic.vote!(1, user)
|
84
|
+
topic.vote!(1, user2)
|
85
|
+
end
|
86
|
+
its(:votes_count) { should == 2 }
|
87
|
+
its(:votes_up) { should == 2 }
|
88
|
+
its(:votes_down) { should == 0 }
|
89
|
+
its(:votes_average) { should == 2 }
|
90
|
+
its(:votes) { should have(2).items }
|
91
|
+
end
|
92
|
+
|
93
|
+
context "users all vote down" do
|
94
|
+
before(:each) do
|
95
|
+
topic.vote!(-1, user)
|
96
|
+
topic.vote!(-1, user2)
|
97
|
+
end
|
98
|
+
its(:votes_count) { should == 2 }
|
99
|
+
its(:votes_up) { should == 0 }
|
100
|
+
its(:votes_down) { should == 2 }
|
101
|
+
its(:votes_average) { should == -2 }
|
102
|
+
its(:votes) { should have(2).items }
|
103
|
+
end
|
104
|
+
|
105
|
+
context "users with different votes" do
|
106
|
+
before(:each) do
|
107
|
+
topic.vote!(1, user)
|
108
|
+
topic.vote!(-1, user2)
|
109
|
+
end
|
110
|
+
its(:votes_count) { should == 2 }
|
111
|
+
its(:votes_up) { should == 1 }
|
112
|
+
its(:votes_down) { should == 1 }
|
113
|
+
its(:votes_average) { should == 0 }
|
114
|
+
its(:votes) { should have(2).items }
|
115
|
+
end
|
116
|
+
|
117
|
+
context "users with different votes" do
|
118
|
+
before(:each) do
|
119
|
+
topic.vote!(1, user)
|
120
|
+
topic.vote!(-2, user2)
|
121
|
+
end
|
122
|
+
its(:votes_count) { should == 2 }
|
123
|
+
its(:votes_up) { should == 1 }
|
124
|
+
its(:votes_down) { should == 1 }
|
125
|
+
its(:votes_average) { should == -1 }
|
126
|
+
its(:votes) { should have(2).items }
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
context "voted_by" do
|
132
|
+
before(:each) do
|
133
|
+
topic.vote!(1, user)
|
134
|
+
topic.vote!(1, user2)
|
135
|
+
end
|
136
|
+
subject { Topic.voted_by(user) }
|
137
|
+
|
138
|
+
it { should have(1).items }
|
139
|
+
its(:first) { should be_instance_of Topic }
|
140
|
+
end
|
141
|
+
|
142
|
+
context "voted?" do
|
143
|
+
before(:each) { topic.vote!(1, user) }
|
144
|
+
it { topic.voted?(user).should be_true }
|
145
|
+
it { topic.voted?(user2).should be_false }
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require 'mongoid'
|
8
|
+
require 'mongoid-votable'
|
9
|
+
require 'active_support'
|
10
|
+
require 'factory_girl'
|
11
|
+
|
12
|
+
FactoryGirl.find_definitions
|
13
|
+
|
14
|
+
ENV['MONGOID_ENV'] = 'test'
|
15
|
+
|
16
|
+
Mongoid.load!("#{File.dirname(__FILE__)}/config/mongoid.yml")
|
17
|
+
Mongoid.logger = nil
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
21
|
+
config.run_all_when_everything_filtered = true
|
22
|
+
config.filter_run :focus
|
23
|
+
|
24
|
+
config.mock_with :rspec
|
25
|
+
|
26
|
+
# Run specs in random order to surface order dependencies. If you find an
|
27
|
+
# order dependency and want to debug it, you can fix the order by providing
|
28
|
+
# the seed, which is printed after each run.
|
29
|
+
# --seed 1234
|
30
|
+
config.order = 'random'
|
31
|
+
|
32
|
+
config.include FactoryGirl::Syntax::Methods
|
33
|
+
|
34
|
+
Dir['./spec/models/*.rb'].each { |f| require f }
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-votable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Zhan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: factory_girl
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,6 +102,7 @@ extensions: []
|
|
88
102
|
extra_rdoc_files: []
|
89
103
|
files:
|
90
104
|
- .gitignore
|
105
|
+
- .rspec
|
91
106
|
- Gemfile
|
92
107
|
- LICENSE.txt
|
93
108
|
- README.md
|
@@ -97,6 +112,13 @@ files:
|
|
97
112
|
- lib/mongoid/votable/version.rb
|
98
113
|
- lib/mongoid/vote.rb
|
99
114
|
- mongoid-votable.gemspec
|
115
|
+
- spec/config/mongoid.yml
|
116
|
+
- spec/factories/topics.rb
|
117
|
+
- spec/factories/users.rb
|
118
|
+
- spec/models/topic.rb
|
119
|
+
- spec/models/user.rb
|
120
|
+
- spec/mongoid/votable_spec.rb
|
121
|
+
- spec/spec_helper.rb
|
100
122
|
homepage: ''
|
101
123
|
licenses:
|
102
124
|
- MIT
|
@@ -121,5 +143,12 @@ rubygems_version: 2.0.3
|
|
121
143
|
signing_key:
|
122
144
|
specification_version: 4
|
123
145
|
summary: Votable for mongoid
|
124
|
-
test_files:
|
146
|
+
test_files:
|
147
|
+
- spec/config/mongoid.yml
|
148
|
+
- spec/factories/topics.rb
|
149
|
+
- spec/factories/users.rb
|
150
|
+
- spec/models/topic.rb
|
151
|
+
- spec/models/user.rb
|
152
|
+
- spec/mongoid/votable_spec.rb
|
153
|
+
- spec/spec_helper.rb
|
125
154
|
has_rdoc:
|