voteable_mongo 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +12 -0
- data/.rvmrc +2 -0
- data/.watchr +23 -0
- data/CHANGELOG.rdoc +71 -0
- data/Gemfile +4 -0
- data/README.rdoc +159 -0
- data/Rakefile +10 -0
- data/TODO +3 -0
- data/lib/voteable_mongo.rb +10 -0
- data/lib/voteable_mongo/railtie.rb +17 -0
- data/lib/voteable_mongo/railties/database.rake +18 -0
- data/lib/voteable_mongo/version.rb +3 -0
- data/lib/voteable_mongo/voteable.rb +199 -0
- data/lib/voteable_mongo/voteable/tasks.rb +162 -0
- data/lib/voteable_mongo/voteable/votes.rb +19 -0
- data/lib/voteable_mongo/voteable/voting.rb +229 -0
- data/lib/voteable_mongo/voter.rb +97 -0
- data/spec/.rspec +1 -0
- data/spec/models/category.rb +10 -0
- data/spec/models/comment.rb +13 -0
- data/spec/models/post.rb +13 -0
- data/spec/models/user.rb +4 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/voteable_mongo/tasks_spec.rb +33 -0
- data/spec/voteable_mongo/voteable_spec.rb +426 -0
- data/spec/voteable_mongo/voter_spec.rb +148 -0
- data/voteable_mongo.gemspec +26 -0
- metadata +131 -0
@@ -0,0 +1,148 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Mongo::Voter do
|
4
|
+
before :all do
|
5
|
+
@post1 = Post.create!
|
6
|
+
@post2 = Post.create!
|
7
|
+
|
8
|
+
@user1 = User.create!
|
9
|
+
@user2 = User.create!
|
10
|
+
end
|
11
|
+
|
12
|
+
context "just created" do
|
13
|
+
it 'validates' do
|
14
|
+
Post.voted_by(@user1).should be_empty
|
15
|
+
Post.up_voted_by(@user1).should be_empty
|
16
|
+
Post.down_voted_by(@user1).should be_empty
|
17
|
+
@user1.voted?(@post1).should be_false
|
18
|
+
@user1.voted?(@post2).should be_false
|
19
|
+
|
20
|
+
Post.voted_by(@user2).should be_empty
|
21
|
+
Post.up_voted_by(@user2).should be_empty
|
22
|
+
Post.down_voted_by(@user2).should be_empty
|
23
|
+
@user2.voted?(@post1).should be_false
|
24
|
+
@user2.voted?(@post2).should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'revote has no effect' do
|
28
|
+
@user2.vote(:revote => true, :votee => @post2, :value => :down)
|
29
|
+
@post2.reload
|
30
|
+
|
31
|
+
@post2.votes_count.should == 0
|
32
|
+
@post2.votes_point.should == 0
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'user1 vote up post1 the first time' do
|
37
|
+
before :all do
|
38
|
+
@user1.vote(:revote => '', :votee_id => @post1.id, :votee_type => 'Post', :value => :up)
|
39
|
+
@post1.reload
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'validates' do
|
43
|
+
@post1.votes_count.should == 1
|
44
|
+
@post1.votes_point.should == 1
|
45
|
+
|
46
|
+
@user1.vote_value(@post1).should == :up
|
47
|
+
@user2.vote_value(:votee_type => 'Post', :votee_id => @post1.id).should be_nil
|
48
|
+
|
49
|
+
@user1.should be_voted(@post1)
|
50
|
+
@user2.should_not be_voted(:votee_type => 'Post', :votee_id => @post1.id)
|
51
|
+
|
52
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
53
|
+
Post.up_voted_by(@user1).to_a.should == [ @post1 ]
|
54
|
+
Post.down_voted_by(@user1).to_a.should be_empty
|
55
|
+
Post.voted_by(@user2).to_a.should be_empty
|
56
|
+
|
57
|
+
User.up_voted_for(@post1).should == [ @user1 ]
|
58
|
+
User.down_voted_for(@post1).should be_empty
|
59
|
+
User.voted_for(@post1).should == [ @user1 ]
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'user1 vote post1 has no effect' do
|
63
|
+
@user1.vote(:votee => @post1, :value => :up)
|
64
|
+
@post1.reload
|
65
|
+
|
66
|
+
@post1.votes_count.should == 1
|
67
|
+
@post1.votes_point.should == 1
|
68
|
+
|
69
|
+
@post1.vote_value(@user1.id).should == :up
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'user2 vote down post1 the first time' do
|
74
|
+
before :all do
|
75
|
+
@user2.vote(:votee => @post1, :value => :down)
|
76
|
+
@post1.reload
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'validates' do
|
80
|
+
@post1.votes_count.should == 2
|
81
|
+
@post1.votes_point.should == 0
|
82
|
+
|
83
|
+
@user1.vote_value(@post1).should == :up
|
84
|
+
@user2.vote_value(@post1).should == :down
|
85
|
+
|
86
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
87
|
+
Post.voted_by(@user2).to_a.should == [ @post1 ]
|
88
|
+
Post.up_voted_by(@user2).to_a.should be_empty
|
89
|
+
Post.down_voted_by(@user2).to_a.should == [ @post1 ]
|
90
|
+
|
91
|
+
User.up_voted_for(@post1).should == [ @user1 ]
|
92
|
+
User.down_voted_for(@post1).should == [ @user2 ]
|
93
|
+
User.voted_for(@post1).should == [ @user1, @user2 ]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'user1 change vote on post1 from up to down' do
|
98
|
+
before :all do
|
99
|
+
@user1.vote(:votee => @post1, :value => :down)
|
100
|
+
@post1.reload
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'validates' do
|
104
|
+
@post1.votes_count.should == 2
|
105
|
+
@post1.votes_point.should == -2
|
106
|
+
|
107
|
+
@user1.vote_value(@post1).should == :down
|
108
|
+
@user2.vote_value(@post1).should == :down
|
109
|
+
|
110
|
+
Post.voted_by(@user1).to_a.should == [ @post1 ]
|
111
|
+
Post.voted_by(@user2).to_a.should == [ @post1 ]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'user1 vote down post2 the first time' do
|
116
|
+
before :all do
|
117
|
+
@user1.vote(:new => 'abc', :votee => @post2, :value => :down)
|
118
|
+
@post2.reload
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'validates' do
|
122
|
+
@post2.votes_count.should == 1
|
123
|
+
@post2.votes_point.should == -1
|
124
|
+
|
125
|
+
@user1.vote_value(@post2).should == :down
|
126
|
+
@user2.vote_value(@post2).should be_nil
|
127
|
+
|
128
|
+
Post.voted_by(@user1).to_a.should == [ @post1, @post2 ]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'user1 change vote on post2 from down to up' do
|
133
|
+
before :all do
|
134
|
+
@user1.vote(:revote => 'abc', :votee => @post2, :value => :up)
|
135
|
+
@post2.reload
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'validates' do
|
139
|
+
@post2.votes_count.should == 1
|
140
|
+
@post2.votes_point.should == 1
|
141
|
+
|
142
|
+
@user1.vote_value(@post2).should == :up
|
143
|
+
@user2.vote_value(@post2).should be_nil
|
144
|
+
|
145
|
+
Post.voted_by(@user1).sort.should == [ @post1, @post2 ].sort
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'voteable_mongo/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'voteable_mongo'
|
7
|
+
s.version = VoteableMongo::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Alex Nguyen']
|
10
|
+
s.email = ['alex@vinova.sg']
|
11
|
+
s.homepage = 'https://github.com/vinova/voteable_mongo'
|
12
|
+
s.summary = %q{Add Up / Down Voting for Mongoid and MongoMapper}
|
13
|
+
s.description = %q{Up / Down Voting for Mongoid (MongoMapper support coming soon). Built for speed by using only one database request per collection to validate data, update data, and get updated data.}
|
14
|
+
|
15
|
+
s.add_development_dependency 'rspec'
|
16
|
+
s.add_development_dependency 'mongoid', '~> 2.0.0'
|
17
|
+
s.add_development_dependency 'mongo_mapper'
|
18
|
+
s.add_development_dependency 'bson_ext'
|
19
|
+
|
20
|
+
s.rubyforge_project = 'voteable_mongo'
|
21
|
+
|
22
|
+
s.files = `git ls-files`.split("\n")
|
23
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
24
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
25
|
+
s.require_paths = ['lib']
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: voteable_mongo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.8.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Nguyen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-03 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mongoid
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.0.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: mongo_mapper
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: bson_ext
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
59
|
+
description: Up / Down Voting for Mongoid (MongoMapper support coming soon). Built for speed by using only one database request per collection to validate data, update data, and get updated data.
|
60
|
+
email:
|
61
|
+
- alex@vinova.sg
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files: []
|
67
|
+
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rvmrc
|
71
|
+
- .watchr
|
72
|
+
- CHANGELOG.rdoc
|
73
|
+
- Gemfile
|
74
|
+
- README.rdoc
|
75
|
+
- Rakefile
|
76
|
+
- TODO
|
77
|
+
- lib/voteable_mongo.rb
|
78
|
+
- lib/voteable_mongo/railtie.rb
|
79
|
+
- lib/voteable_mongo/railties/database.rake
|
80
|
+
- lib/voteable_mongo/version.rb
|
81
|
+
- lib/voteable_mongo/voteable.rb
|
82
|
+
- lib/voteable_mongo/voteable/tasks.rb
|
83
|
+
- lib/voteable_mongo/voteable/votes.rb
|
84
|
+
- lib/voteable_mongo/voteable/voting.rb
|
85
|
+
- lib/voteable_mongo/voter.rb
|
86
|
+
- spec/.rspec
|
87
|
+
- spec/models/category.rb
|
88
|
+
- spec/models/comment.rb
|
89
|
+
- spec/models/post.rb
|
90
|
+
- spec/models/user.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
- spec/voteable_mongo/tasks_spec.rb
|
93
|
+
- spec/voteable_mongo/voteable_spec.rb
|
94
|
+
- spec/voteable_mongo/voter_spec.rb
|
95
|
+
- voteable_mongo.gemspec
|
96
|
+
homepage: https://github.com/vinova/voteable_mongo
|
97
|
+
licenses: []
|
98
|
+
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: "0"
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: "0"
|
116
|
+
requirements: []
|
117
|
+
|
118
|
+
rubyforge_project: voteable_mongo
|
119
|
+
rubygems_version: 1.7.2
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: Add Up / Down Voting for Mongoid and MongoMapper
|
123
|
+
test_files:
|
124
|
+
- spec/models/category.rb
|
125
|
+
- spec/models/comment.rb
|
126
|
+
- spec/models/post.rb
|
127
|
+
- spec/models/user.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/voteable_mongo/tasks_spec.rb
|
130
|
+
- spec/voteable_mongo/voteable_spec.rb
|
131
|
+
- spec/voteable_mongo/voter_spec.rb
|