voteable_mongoid 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mongoid/voter.rb CHANGED
@@ -7,7 +7,7 @@ module Mongoid
7
7
  # @param [Class] klass the votee class, e.g. `Post` or `Comment`
8
8
  # @return [Array, nil] an array of voteable objects voted by this voter
9
9
  def votees(klass)
10
- klass.any_of({ :up_voter_ids => _id }, { :down_voter_ids => _id })
10
+ klass.any_of({ "voteable.up_voter_ids" => _id }, { "voteable.down_voter_ids" => _id })
11
11
  end
12
12
 
13
13
  # Check to see if this voter voted on the votee or not
@@ -0,0 +1,3 @@
1
+ module VoteableMongoid
2
+ VERSION = "0.4.3"
3
+ end
data/spec/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --debugger
@@ -0,0 +1,11 @@
1
+ require 'post'
2
+
3
+ class Comment
4
+ include Mongoid::Document
5
+ include Mongoid::Voteable
6
+
7
+ referenced_in :post
8
+
9
+ vote_point self, :up => +1, :down => -3
10
+ vote_point Post, :up => +2, :down => -1
11
+ end
@@ -0,0 +1,8 @@
1
+ class Post
2
+ include Mongoid::Document
3
+ include Mongoid::Voteable
4
+
5
+ vote_point self, :up => +1, :down => -1
6
+
7
+ references_many :comments
8
+ end
@@ -0,0 +1,4 @@
1
+ class User
2
+ include Mongoid::Document
3
+ include Mongoid::Voter
4
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+
9
+ MODELS = File.join(File.dirname(__FILE__), "models")
10
+ $LOAD_PATH.unshift(MODELS)
11
+
12
+
13
+ require 'mongoid'
14
+ require 'voteable_mongoid'
15
+ require 'rspec'
16
+ require 'rspec/autorun'
17
+
18
+
19
+ Mongoid.configure do |config|
20
+ name = "voteable_mongoid_test"
21
+ host = "localhost"
22
+ config.master = Mongo::Connection.new.db(name)
23
+ end
24
+
25
+
26
+ Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
27
+
28
+
29
+ RSpec.configure do |config|
30
+ end
@@ -0,0 +1,234 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Voteable do
4
+ before :all do
5
+ Mongoid::database.connection.drop_database(Mongoid::database.name)
6
+
7
+ @post1 = Post.create!
8
+ @post2 = Post.create!
9
+
10
+ @comment = @post2.comments.create!
11
+
12
+ @user1 = User.create!
13
+ @user2 = User.create!
14
+ end
15
+
16
+ context "just created" do
17
+ it 'voteable votes_count, votes_point should be zero' do
18
+ @post1.votes_count.should == 0
19
+ @post1.votes_point.should == 0
20
+
21
+ @post2.votes_count.should == 0
22
+ @post2.votes_point.should == 0
23
+ end
24
+
25
+ it 'voteable up_voter_ids, down_voter_ids should be empty' do
26
+ @post1.up_voter_ids.should be_empty
27
+ @post1.down_voter_ids.should be_empty
28
+
29
+ @post2.up_voter_ids.should be_empty
30
+ @post2.down_voter_ids.should be_empty
31
+ end
32
+
33
+ it 'voter votees should be empty' do
34
+ @user1.votees(Post).should be_empty
35
+ @user2.votees(Post).should be_empty
36
+ end
37
+
38
+ it 'revote has no effect' do
39
+ Post.vote(:revote => true, :votee_id => @post1.id, :voter_id => @user1.id, :value => 'up')
40
+ @post1.reload
41
+
42
+ @post1.votes_count.should == 0
43
+ @post1.votes_point.should == 0
44
+
45
+ Post.vote(:revote => true, :votee_id => @post2.id, :voter_id => @user2.id, :value => :down)
46
+ @post2.reload
47
+
48
+ @post2.votes_count.should == 0
49
+ @post2.votes_point.should == 0
50
+ end
51
+ end
52
+
53
+ context 'user1 vote up post1 the first time' do
54
+ before :all do
55
+ Post.vote(:votee_id => @post1.id, :voter_id => @user1.id, :value => :up)
56
+ @post1.reload
57
+ end
58
+
59
+ it '' do
60
+ @post1.votes_count.should == 1
61
+ @post1.votes_point.should == 1
62
+
63
+ @post1.vote_value(@user1).should == :up
64
+ @post1.vote_value(@user2.id).should be_nil
65
+
66
+ @user1.votees(Post).to_a.should == [ @post1 ]
67
+ @user2.votees(Post).to_a.should be_empty
68
+ end
69
+
70
+ it 'user1 vote post1 has no effect' do
71
+ Post.vote(:revote => false, :votee_id => @post1.id, :voter_id => @user1.id, :value => :up)
72
+ @post1.reload
73
+
74
+ @post1.votes_count.should == 1
75
+ @post1.votes_point.should == 1
76
+
77
+ @post1.vote_value(@user1.id).should == :up
78
+ end
79
+ end
80
+
81
+ context 'user2 vote down post1 the first time' do
82
+ before :all do
83
+ Post.vote(:votee_id => @post1.id, :voter_id => @user2.id, :value => :down)
84
+ @post1.reload
85
+ end
86
+
87
+ it '' do
88
+ @post1.votes_count.should == 2
89
+ @post1.votes_point.should == 0
90
+
91
+ @post1.vote_value(@user1.id).should == :up
92
+ @post1.vote_value(@user2.id).should == :down
93
+
94
+ @user1.votees(Post).to_a.should == [ @post1 ]
95
+ @user2.votees(Post).to_a.should == [ @post1 ]
96
+ end
97
+ end
98
+
99
+ context 'user1 change vote on post1 from up to down' do
100
+ before :all do
101
+ Post.vote(:revote => true, :votee_id => @post1.id, :voter_id => @user1.id, :value => :down)
102
+ @post1.reload
103
+ end
104
+
105
+ it '' do
106
+ @post1.votes_count.should == 2
107
+ @post1.votes_point.should == -2
108
+
109
+ @post1.vote_value(@user1.id).should == :down
110
+ @post1.vote_value(@user2.id).should == :down
111
+
112
+ @user1.votees(Post).to_a.should == [ @post1 ]
113
+ @user2.votees(Post).to_a.should == [ @post1 ]
114
+ end
115
+ end
116
+
117
+ context 'user1 vote down post2 the first time' do
118
+ before :all do
119
+ Post.vote(:votee_id => @post2.id, :voter_id => @user1.id, :value => :down)
120
+ @post2.reload
121
+ end
122
+
123
+ it '' do
124
+ @post2.votes_count.should == 1
125
+ @post2.votes_point.should == -1
126
+
127
+ @post2.vote_value(@user1.id).should == :down
128
+ @post2.vote_value(@user2.id).should be_nil
129
+
130
+ @user1.votees(Post).to_a.should == [ @post1, @post2 ]
131
+ end
132
+ end
133
+
134
+ context 'user1 change vote on post2 from down to up' do
135
+ before :all do
136
+ Post.vote(:revote => true, :votee_id => @post2.id.to_s, :voter_id => @user1.id.to_s, :value => :up)
137
+ @post2.reload
138
+ end
139
+
140
+ it '' do
141
+ @post2.votes_count.should == 1
142
+ @post2.votes_point.should == 1
143
+
144
+ @post2.vote_value(@user1.id).should == :up
145
+ @post2.vote_value(@user2.id).should be_nil
146
+
147
+ @user1.votees(Post).to_a.should == [ @post1, @post2 ]
148
+ end
149
+ end
150
+
151
+
152
+ context 'user1 vote up post2 comment the first time' do
153
+ before :all do
154
+ @comment.vote(:voter_id => @user1.id, :value => :up)
155
+ @comment.reload
156
+ @post2.reload
157
+ end
158
+
159
+ it '' do
160
+ @post2.votes_count.should == 2
161
+ @post2.votes_point.should == 3
162
+
163
+ @comment.votes_count.should == 1
164
+ @comment.votes_point.should == 1
165
+ end
166
+ end
167
+
168
+
169
+ context 'user1 revote post2 comment from up to down' do
170
+ before :all do
171
+ @user1.vote(:votee => @comment, :value => :down)
172
+ @comment.reload
173
+ @post2.reload
174
+ end
175
+
176
+ it '' do
177
+ @post2.votes_count.should == 2
178
+ @post2.votes_point.should == 0
179
+
180
+ @comment.votes_count.should == 1
181
+ @comment.votes_point.should == -3
182
+ end
183
+
184
+ it 'revote with wrong value has no effect' do
185
+ @user1.vote(:votee => @comment, :value => :down)
186
+
187
+ @post2.votes_count.should == 2
188
+ @post2.votes_point.should == 0
189
+
190
+ @comment.votes_count.should == 1
191
+ @comment.votes_point.should == -3
192
+ end
193
+ end
194
+
195
+ context "user1 unvote on post1" do
196
+ before(:all) do
197
+ @post1.vote(:voter_id => @user1.id, :votee_id => @post1.id, :unvote => true)
198
+ @post1.reload
199
+ end
200
+
201
+ it "" do
202
+ @post1.votes_count.should == 1
203
+ @post1.votes_point.should == -1
204
+
205
+ @post1.vote_value(@user1.id).should be_nil
206
+ @post1.vote_value(@user2.id).should == :down
207
+
208
+ @user1.votees(Post).to_a.should_not include(@post1)
209
+ end
210
+ end
211
+
212
+ context "@post1 has 1 vote and -1 point, @post2 has 2 votes and 0 point" do
213
+ it "" do
214
+ Post.most_voted.first.should == @post2
215
+ Post.best_voted.first.should == @post2
216
+ end
217
+ end
218
+
219
+ context "user1 unvote on comment" do
220
+ before(:all) do
221
+ @user1.unvote(@comment)
222
+ @comment.reload
223
+ @post2.reload
224
+ end
225
+
226
+ it "" do
227
+ @post2.votes_count.should == 1
228
+ @post2.votes_point.should == 1
229
+
230
+ @comment.votes_count.should == 0
231
+ @comment.votes_point.should == 0
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,134 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Voter do
4
+ before :all do
5
+ Mongoid::database.connection.drop_database(Mongoid::database.name)
6
+
7
+ @post1 = Post.create!
8
+ @post2 = Post.create!
9
+
10
+ @user1 = User.create!
11
+ @user2 = User.create!
12
+ end
13
+
14
+ context "just created" do
15
+ it '' do
16
+ @user1.votees(Post).should be_empty
17
+ @user1.voted?(@post1).should be_false
18
+ @user1.voted?(@post2).should be_false
19
+
20
+ @user2.votees(Post).should be_empty
21
+ @user2.voted?(@post1).should be_false
22
+ @user2.voted?(@post2).should be_false
23
+ end
24
+
25
+ it 'revote has no effect' do
26
+ @user2.vote(:revote => true, :votee => @post2, :value => :down)
27
+ @post2.reload
28
+
29
+ @post2.votes_count.should == 0
30
+ @post2.votes_point.should == 0
31
+ end
32
+ end
33
+
34
+ context 'user1 vote up post1 the first time' do
35
+ before :all do
36
+ @user1.vote(:revote => '', :votee_id => @post1.id, :votee_type => 'Post', :value => :up)
37
+ @post1.reload
38
+ end
39
+
40
+ it '' do
41
+ @post1.votes_count.should == 1
42
+ @post1.votes_point.should == 1
43
+
44
+ @user1.vote_value(@post1).should == :up
45
+ @user2.vote_value(:votee_type => 'Post', :votee_id => @post1.id).should be_nil
46
+
47
+ @user1.should be_voted(@post1)
48
+ @user2.should_not be_voted(:votee_type => 'Post', :votee_id => @post1.id)
49
+
50
+ @user1.votees(Post).to_a.should == [ @post1 ]
51
+ @user2.votees(Post).to_a.should be_empty
52
+ end
53
+
54
+ it 'user1 vote post1 has no effect' do
55
+ @user1.vote(:votee => @post1, :value => :up)
56
+ @post1.reload
57
+
58
+ @post1.votes_count.should == 1
59
+ @post1.votes_point.should == 1
60
+
61
+ @post1.vote_value(@user1.id).should == :up
62
+ end
63
+ end
64
+
65
+ context 'user2 vote down post1 the first time' do
66
+ before :all do
67
+ @user2.vote(:votee => @post1, :value => :down)
68
+ @post1.reload
69
+ end
70
+
71
+ it '' do
72
+ @post1.votes_count.should == 2
73
+ @post1.votes_point.should == 0
74
+
75
+ @user1.vote_value(@post1).should == :up
76
+ @user2.vote_value(@post1).should == :down
77
+
78
+ @user1.votees(Post).to_a.should == [ @post1 ]
79
+ @user2.votees(Post).to_a.should == [ @post1 ]
80
+ end
81
+ end
82
+
83
+ context 'user1 change vote on post1 from up to down' do
84
+ before :all do
85
+ @user1.vote(:votee => @post1, :value => :down)
86
+ @post1.reload
87
+ end
88
+
89
+ it '' do
90
+ @post1.votes_count.should == 2
91
+ @post1.votes_point.should == -2
92
+
93
+ @user1.vote_value(@post1).should == :down
94
+ @user2.vote_value(@post1).should == :down
95
+
96
+ @user1.votees(Post).to_a.should == [ @post1 ]
97
+ @user2.votees(Post).to_a.should == [ @post1 ]
98
+ end
99
+ end
100
+
101
+ context 'user1 vote down post2 the first time' do
102
+ before :all do
103
+ @user1.vote(:new => 'abc', :votee => @post2, :value => :down)
104
+ @post2.reload
105
+ end
106
+
107
+ it '' do
108
+ @post2.votes_count.should == 1
109
+ @post2.votes_point.should == -1
110
+
111
+ @user1.vote_value(@post2).should == :down
112
+ @user2.vote_value(@post2).should be_nil
113
+
114
+ @user1.votees(Post).to_a.should == [ @post1, @post2 ]
115
+ end
116
+ end
117
+
118
+ context 'user1 change vote on post2 from down to up' do
119
+ before :all do
120
+ @user1.vote(:revote => 'abc', :votee => @post2, :value => :up)
121
+ @post2.reload
122
+ end
123
+
124
+ it '' do
125
+ @post2.votes_count.should == 1
126
+ @post2.votes_point.should == 1
127
+
128
+ @user1.vote_value(@post2).should == :up
129
+ @user2.vote_value(@post2).should be_nil
130
+
131
+ @user1.votees(Post).to_a.should == [ @post1, @post2 ]
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "voteable_mongoid/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "voteable_mongoid"
7
+ s.version = VoteableMongoid::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Alex Nguyen", "Stefan Nguyen"]
10
+ s.email = ["alex@vinova.sg"]
11
+ s.homepage = "http://vinova.sg"
12
+ s.summary = %q{Add Up / Down Voting for Mongoid}
13
+ s.description = %q{Add Up / Down Voting for Mongoid (built for speed by using one Mongodb update-in-place for each collection when provided enough information)}
14
+
15
+ s.add_dependency 'mongoid', '~> 2.0.0.rc'
16
+ s.add_development_dependency 'rspec'
17
+ s.add_development_dependency 'bson_ext'
18
+
19
+ s.rubyforge_project = "voteable_mongoid"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
metadata CHANGED
@@ -1,90 +1,117 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voteable_mongoid
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 9
4
5
  prerelease:
5
- version: 0.4.2
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 3
10
+ version: 0.4.3
6
11
  platform: ruby
7
12
  authors:
8
13
  - Alex Nguyen
14
+ - Stefan Nguyen
9
15
  autorequire:
10
16
  bindir: bin
11
17
  cert_chain: []
12
18
 
13
- date: 2011-03-01 00:00:00 +08:00
19
+ date: 2011-03-30 00:00:00 +08:00
14
20
  default_executable:
15
21
  dependencies:
16
22
  - !ruby/object:Gem::Dependency
17
- name: voteable_mongoid
23
+ name: mongoid
24
+ prerelease: false
18
25
  requirement: &id001 !ruby/object:Gem::Requirement
19
26
  none: false
20
27
  requirements:
21
- - - ">="
28
+ - - ~>
22
29
  - !ruby/object:Gem::Version
23
- version: "0"
30
+ hash: 7712058
31
+ segments:
32
+ - 2
33
+ - 0
34
+ - 0
35
+ - rc
36
+ version: 2.0.0.rc
24
37
  type: :runtime
25
- prerelease: false
26
38
  version_requirements: *id001
27
39
  - !ruby/object:Gem::Dependency
28
40
  name: rspec
41
+ prerelease: false
29
42
  requirement: &id002 !ruby/object:Gem::Requirement
30
43
  none: false
31
44
  requirements:
32
45
  - - ">="
33
46
  - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
34
50
  version: "0"
35
51
  type: :development
36
- prerelease: false
37
52
  version_requirements: *id002
38
53
  - !ruby/object:Gem::Dependency
39
- name: rspec
54
+ name: bson_ext
55
+ prerelease: false
40
56
  requirement: &id003 !ruby/object:Gem::Requirement
41
57
  none: false
42
58
  requirements:
43
59
  - - ">="
44
60
  - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
45
64
  version: "0"
46
65
  type: :development
47
- prerelease: false
48
66
  version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
50
- name: mongoid
51
- requirement: &id004 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ~>
55
- - !ruby/object:Gem::Version
56
- version: 2.0.0.rc
57
- type: :runtime
58
- prerelease: false
59
- version_requirements: *id004
60
- - !ruby/object:Gem::Dependency
61
- name: rspec
62
- requirement: &id005 !ruby/object:Gem::Requirement
63
- none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: "0"
68
- type: :development
69
- prerelease: false
70
- version_requirements: *id005
71
- description: Add Up / Down Voting for Mongoid
72
- email: alex@vinova.sg
67
+ description: Add Up / Down Voting for Mongoid (built for speed by using one Mongodb update-in-place for each collection when provided enough information)
68
+ email:
69
+ - alex@vinova.sg
73
70
  executables: []
74
71
 
75
72
  extensions: []
76
73
 
77
- extra_rdoc_files:
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
78
79
  - README.rdoc
80
+ - Rakefile
79
81
  - TODO
80
- files:
82
+ - VERSION
83
+ - doc/Mongoid.html
84
+ - doc/Mongoid/Voteable.html
85
+ - doc/Mongoid/Voter.html
86
+ - doc/VoteableMongoid.html
87
+ - doc/_index.html
88
+ - doc/class_list.html
89
+ - doc/css/common.css
90
+ - doc/css/full_list.css
91
+ - doc/css/style.css
92
+ - doc/file.README.html
93
+ - doc/file_list.html
94
+ - doc/frames.html
95
+ - doc/index.html
96
+ - doc/js/app.js
97
+ - doc/js/full_list.js
98
+ - doc/js/jquery.js
99
+ - doc/method_list.html
100
+ - doc/top-level-namespace.html
81
101
  - lib/mongoid/voteable.rb
82
102
  - lib/mongoid/voter.rb
83
103
  - lib/voteable_mongoid.rb
84
- - README.rdoc
85
- - TODO
104
+ - lib/voteable_mongoid/version.rb
105
+ - spec/.rspec
106
+ - spec/models/comment.rb
107
+ - spec/models/post.rb
108
+ - spec/models/user.rb
109
+ - spec/spec_helper.rb
110
+ - spec/voteable_spec.rb
111
+ - spec/voter_spec.rb
112
+ - voteable_mongoid.gemspec
86
113
  has_rdoc: true
87
- homepage: http://github.com/vinova/voteable_mongoid
114
+ homepage: http://vinova.sg
88
115
  licenses: []
89
116
 
90
117
  post_install_message:
@@ -97,19 +124,30 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
124
  requirements:
98
125
  - - ">="
99
126
  - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
100
130
  version: "0"
101
131
  required_rubygems_version: !ruby/object:Gem::Requirement
102
132
  none: false
103
133
  requirements:
104
134
  - - ">="
105
135
  - !ruby/object:Gem::Version
136
+ hash: 3
137
+ segments:
138
+ - 0
106
139
  version: "0"
107
140
  requirements: []
108
141
 
109
- rubyforge_project:
110
- rubygems_version: 1.5.3
142
+ rubyforge_project: voteable_mongoid
143
+ rubygems_version: 1.6.2
111
144
  signing_key:
112
145
  specification_version: 3
113
146
  summary: Add Up / Down Voting for Mongoid
114
- test_files: []
115
-
147
+ test_files:
148
+ - spec/models/comment.rb
149
+ - spec/models/post.rb
150
+ - spec/models/user.rb
151
+ - spec/spec_helper.rb
152
+ - spec/voteable_spec.rb
153
+ - spec/voter_spec.rb