mm-voteable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +60 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +59 -0
- data/Rakefile +43 -0
- data/VERSION +1 -0
- data/lib/mm-voteable.rb +3 -0
- data/lib/mongo_mapper/plugins/voteable/vote.rb +40 -0
- data/lib/mongo_mapper/plugins/voteable.rb +48 -0
- data/mm-voteable.gemspec +93 -0
- data/spec/mm-voteable_spec.rb +120 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/support/models.rb +23 -0
- metadata +263 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
group :development do
|
4
|
+
gem 'mongo', '1.1.2'
|
5
|
+
gem 'bson_ext', '1.1.2'
|
6
|
+
gem 'mongo_ext'
|
7
|
+
gem 'mongo_mapper'
|
8
|
+
gem 'i18n'
|
9
|
+
|
10
|
+
gem 'log_buddy'
|
11
|
+
|
12
|
+
gem 'ruby-debug'
|
13
|
+
gem "rspec", "~> 2.1.0"
|
14
|
+
gem "yard", "~> 0.6.0"
|
15
|
+
gem "bundler", "~> 1.0.0"
|
16
|
+
gem "jeweler", "~> 1.5.1"
|
17
|
+
gem "rcov", ">= 0"
|
18
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.0.1)
|
5
|
+
bson (1.1.2)
|
6
|
+
bson_ext (1.1.2)
|
7
|
+
columnize (0.3.2)
|
8
|
+
diff-lcs (1.1.2)
|
9
|
+
git (1.2.5)
|
10
|
+
i18n (0.4.2)
|
11
|
+
jeweler (1.5.1)
|
12
|
+
bundler (~> 1.0.0)
|
13
|
+
git (>= 1.2.5)
|
14
|
+
rake
|
15
|
+
jnunemaker-validatable (1.8.4)
|
16
|
+
activesupport (>= 2.3.4)
|
17
|
+
linecache (0.43)
|
18
|
+
log_buddy (0.5.0)
|
19
|
+
mongo (1.1.2)
|
20
|
+
bson (>= 1.1.1)
|
21
|
+
mongo_ext (0.19.3)
|
22
|
+
mongo_mapper (0.8.6)
|
23
|
+
activesupport (>= 2.3.4)
|
24
|
+
jnunemaker-validatable (~> 1.8.4)
|
25
|
+
plucky (~> 0.3.6)
|
26
|
+
plucky (0.3.6)
|
27
|
+
mongo (~> 1.1)
|
28
|
+
rake (0.8.7)
|
29
|
+
rcov (0.9.9)
|
30
|
+
rspec (2.1.0)
|
31
|
+
rspec-core (~> 2.1.0)
|
32
|
+
rspec-expectations (~> 2.1.0)
|
33
|
+
rspec-mocks (~> 2.1.0)
|
34
|
+
rspec-core (2.1.0)
|
35
|
+
rspec-expectations (2.1.0)
|
36
|
+
diff-lcs (~> 1.1.2)
|
37
|
+
rspec-mocks (2.1.0)
|
38
|
+
ruby-debug (0.10.4)
|
39
|
+
columnize (>= 0.1)
|
40
|
+
ruby-debug-base (~> 0.10.4.0)
|
41
|
+
ruby-debug-base (0.10.4)
|
42
|
+
linecache (>= 0.3)
|
43
|
+
yard (0.6.2)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
bson_ext (= 1.1.2)
|
50
|
+
bundler (~> 1.0.0)
|
51
|
+
i18n
|
52
|
+
jeweler (~> 1.5.1)
|
53
|
+
log_buddy
|
54
|
+
mongo (= 1.1.2)
|
55
|
+
mongo_ext
|
56
|
+
mongo_mapper
|
57
|
+
rcov
|
58
|
+
rspec (~> 2.1.0)
|
59
|
+
ruby-debug
|
60
|
+
yard (~> 0.6.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Luke Cunningham
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
= mm-voteable
|
2
|
+
|
3
|
+
A very simple mongomapper plugin to add vote up/down functionality to your application.
|
4
|
+
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
Install the gem
|
9
|
+
|
10
|
+
gem install mm-voteable
|
11
|
+
|
12
|
+
|
13
|
+
Or add it to your Gemfile
|
14
|
+
|
15
|
+
gem 'mm-voteable'
|
16
|
+
|
17
|
+
Then add the voteable plugin to you MongoMapper::Document
|
18
|
+
|
19
|
+
class Post
|
20
|
+
include MongoMapper::Document
|
21
|
+
plugin MongoMapper::Plugins::Voteable
|
22
|
+
|
23
|
+
def on_add_vote(comment)
|
24
|
+
do something when a comment is added...
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
You must implement the on_add_comment callback! - however it can be an empty implementation
|
29
|
+
|
30
|
+
@post = Post.create
|
31
|
+
@voter = User.create
|
32
|
+
|
33
|
+
@post.add_vote!(1, @user)
|
34
|
+
@post.add_vote!(1, @user)
|
35
|
+
|
36
|
+
@post.votes_count #=> 2
|
37
|
+
@posts.votes #=> [<Vote>]
|
38
|
+
@posts.voters #=> [<User>]
|
39
|
+
@posts.voters.include?(@voter) #=> true
|
40
|
+
|
41
|
+
== Assumptions
|
42
|
+
* your voteable model has a 'user' association
|
43
|
+
|
44
|
+
|
45
|
+
== Contributing to mm-voteable
|
46
|
+
|
47
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
48
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
49
|
+
* Fork the project
|
50
|
+
* Start a feature/bugfix branch
|
51
|
+
* Commit and push until you are happy with your contribution
|
52
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
53
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
54
|
+
|
55
|
+
== Copyright
|
56
|
+
|
57
|
+
Copyright (c) 2010 Luke Cunningham. See LICENSE.txt for
|
58
|
+
further details.
|
59
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "mm-voteable"
|
16
|
+
gem.homepage = "http://github.com/icaruswings/mm-voteable"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{A very simple mongomapper plugin to add vote up/down functionality to your application}
|
19
|
+
gem.description = %Q{A very simple mongomapper plugin to add vote up/down functionality to your application}
|
20
|
+
gem.email = "luke@icaruswings.com"
|
21
|
+
gem.authors = ["Luke Cunningham"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'yard'
|
43
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/mm-voteable.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
class Vote
|
2
|
+
include MongoMapper::Document
|
3
|
+
|
4
|
+
key :voter_id, ObjectId, :required => true
|
5
|
+
belongs_to :voter, :class_name => 'User'
|
6
|
+
|
7
|
+
key :value, Integer, :required => true
|
8
|
+
|
9
|
+
key :voteable_id, ObjectId, :required => true
|
10
|
+
key :voteable_type, String, :required => true
|
11
|
+
belongs_to :voteable, :polymorphic => true
|
12
|
+
|
13
|
+
validates_inclusion_of :value, :within => [1,-1]
|
14
|
+
|
15
|
+
validate :should_be_unique
|
16
|
+
validate :should_not_be_owner
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def should_be_unique
|
21
|
+
vote = Vote.first({
|
22
|
+
:voteable_type => self.voteable_type,
|
23
|
+
:voteable_id => self.voteable_id,
|
24
|
+
:voter_id => self.voter_id
|
25
|
+
})
|
26
|
+
|
27
|
+
valid = vote.nil?
|
28
|
+
|
29
|
+
return true if valid
|
30
|
+
self.errors.add(:voter, "You already voted on this #{self.voteable_type}.")
|
31
|
+
return false
|
32
|
+
end
|
33
|
+
|
34
|
+
def should_not_be_owner
|
35
|
+
return true unless self.voteable.user_id == self.voter_id
|
36
|
+
self.errors.add(:vote, "You cant vote on this #{self.voteable_type}.")
|
37
|
+
return false
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'voteable', 'vote')
|
2
|
+
|
3
|
+
module MongoMapper
|
4
|
+
module Plugins
|
5
|
+
module Voteable
|
6
|
+
|
7
|
+
def self.configure(model)
|
8
|
+
model.class_eval do
|
9
|
+
key :votes_count, Integer, :default => 0
|
10
|
+
key :votes_average, Integer, :default => 0
|
11
|
+
key :voter_ids, Array, :typecast => 'ObjectId'
|
12
|
+
|
13
|
+
many :votes, :as => 'voteable', :dependent => :destroy
|
14
|
+
|
15
|
+
def voteable?; true; end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
|
21
|
+
def add_vote!(vote_value, voter)
|
22
|
+
vote = self.votes.build(:value => vote_value, :voter => voter)
|
23
|
+
|
24
|
+
return false unless vote.valid?
|
25
|
+
vote.save
|
26
|
+
|
27
|
+
self.class.push_uniq(self.id, 'voter_ids' => voter.id)
|
28
|
+
self.increment('votes_count' => 1, 'votes_average' => vote_value.to_i)
|
29
|
+
|
30
|
+
on_add_vote(vote)
|
31
|
+
end
|
32
|
+
|
33
|
+
def on_add_vote(vote)
|
34
|
+
raise NotImplementedError
|
35
|
+
end
|
36
|
+
|
37
|
+
def voters
|
38
|
+
User.find(self.voter_ids)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
module ClassMethods
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/mm-voteable.gemspec
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{mm-voteable}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Luke Cunningham"]
|
12
|
+
s.date = %q{2010-11-18}
|
13
|
+
s.description = %q{A very simple mongomapper plugin to add vote up/down functionality to your application}
|
14
|
+
s.email = %q{luke@icaruswings.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/mm-voteable.rb",
|
29
|
+
"lib/mongo_mapper/plugins/voteable.rb",
|
30
|
+
"lib/mongo_mapper/plugins/voteable/vote.rb",
|
31
|
+
"mm-voteable.gemspec",
|
32
|
+
"spec/mm-voteable_spec.rb",
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/support/models.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/icaruswings/mm-voteable}
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.7}
|
40
|
+
s.summary = %q{A very simple mongomapper plugin to add vote up/down functionality to your application}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/mm-voteable_spec.rb",
|
43
|
+
"spec/spec_helper.rb",
|
44
|
+
"spec/support/models.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<mongo>, ["= 1.1.2"])
|
53
|
+
s.add_development_dependency(%q<bson_ext>, ["= 1.1.2"])
|
54
|
+
s.add_development_dependency(%q<mongo_ext>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<mongo_mapper>, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<i18n>, [">= 0"])
|
57
|
+
s.add_development_dependency(%q<log_buddy>, [">= 0"])
|
58
|
+
s.add_development_dependency(%q<ruby-debug>, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
|
60
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
61
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
62
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
|
63
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<mongo>, ["= 1.1.2"])
|
66
|
+
s.add_dependency(%q<bson_ext>, ["= 1.1.2"])
|
67
|
+
s.add_dependency(%q<mongo_ext>, [">= 0"])
|
68
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0"])
|
69
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
70
|
+
s.add_dependency(%q<log_buddy>, [">= 0"])
|
71
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
72
|
+
s.add_dependency(%q<rspec>, ["~> 2.1.0"])
|
73
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
74
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
75
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
76
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
77
|
+
end
|
78
|
+
else
|
79
|
+
s.add_dependency(%q<mongo>, ["= 1.1.2"])
|
80
|
+
s.add_dependency(%q<bson_ext>, ["= 1.1.2"])
|
81
|
+
s.add_dependency(%q<mongo_ext>, [">= 0"])
|
82
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0"])
|
83
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
84
|
+
s.add_dependency(%q<log_buddy>, [">= 0"])
|
85
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
86
|
+
s.add_dependency(%q<rspec>, ["~> 2.1.0"])
|
87
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
88
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
89
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
90
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Vote" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@user = User.create(:email => 'luke@icaruswings.com')
|
7
|
+
@voteable = VoteableWithCallback.create(:user_id => User.create().id)
|
8
|
+
@vote = @voteable.votes.build(:value => 1, :voter => @user)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should not be embedded?" do
|
12
|
+
Vote.embeddable?.should_not be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
it "should be valid" do
|
17
|
+
@vote.should be_valid
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be valid if already voted" do
|
21
|
+
voteable = VoteableWithCallback.create(:user_id => @user.id)
|
22
|
+
vote = voteable.votes.build(:value => 1, :voter => @user)
|
23
|
+
|
24
|
+
vote.should_not be_valid
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be valid if voteable owner" do
|
28
|
+
voteable = VoteableWithCallback.create(:user_id => @user.id)
|
29
|
+
vote = voteable.votes.build(:value => 1, :voter => @user)
|
30
|
+
|
31
|
+
vote.should_not be_valid
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should have a belongs_to voteable association" do
|
35
|
+
@vote.voteable.association.should be_belongs_to
|
36
|
+
@vote.voteable.should == @voteable
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should have an associated voter" do
|
40
|
+
@vote.voter.association.should be_belongs_to
|
41
|
+
@vote.voter.should == @user
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "MongoMapper::Plugins::Voteable" do
|
47
|
+
|
48
|
+
before(:each) do
|
49
|
+
@user = User.create(:email => 'luke@icaruswings.com')
|
50
|
+
@user_2 = User.create(:email => 'lukec@icaruswings.com')
|
51
|
+
@voteable = VoteableWithCallback.create(:user_id => User.create().id)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should be voteable" do
|
55
|
+
@voteable.should be_voteable
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should have a votes_count attribute that defaults to 0" do
|
59
|
+
@voteable.votes_count.should equal 0
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "add_vote!" do
|
63
|
+
|
64
|
+
it "should increment the votes_count attribute" do
|
65
|
+
lambda {
|
66
|
+
@voteable.add_vote!(1, @user)
|
67
|
+
}.should change(@voteable, :votes_count).by(1)
|
68
|
+
|
69
|
+
lambda {
|
70
|
+
@voteable.add_vote!(1, User.create(:email => 'lukec@icaruswings.com'))
|
71
|
+
}.should change(@voteable, :votes_count).by(1)
|
72
|
+
|
73
|
+
@voteable.votes_count.should equal 2
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should add the vote" do
|
77
|
+
@voteable.add_vote!(1, @user)
|
78
|
+
|
79
|
+
@voteable.should have(1).vote
|
80
|
+
@voteable.votes.count.should equal 1
|
81
|
+
@voteable.votes_count.should equal 1
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should add a second vote" do
|
85
|
+
@voteable.add_vote!(1, @user)
|
86
|
+
@voteable.add_vote!(-1, @user_2)
|
87
|
+
|
88
|
+
@voteable.should have(2).votes
|
89
|
+
@voteable.votes.count.should equal 2
|
90
|
+
@voteable.votes_count.should equal 2
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should assign the voter" do
|
94
|
+
@voteable.add_vote!(1, @user)
|
95
|
+
vote = @voteable.votes.first
|
96
|
+
|
97
|
+
vote.voter.should == @user
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should have a way of retrieving all voters on voteable" do
|
101
|
+
@voteable.add_vote!(1, @user)
|
102
|
+
|
103
|
+
@voteable.voters.should include(@user)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should throw NotImplementedError if the :on_add_vote callback has not inplemented" do
|
107
|
+
voteable = VoteableWithoutCallback.new
|
108
|
+
lambda {
|
109
|
+
voteable.add_vote!(1, @user)
|
110
|
+
}.should raise_error(NotImplementedError)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should call :on_add_vote if the callback has been implemented" do
|
114
|
+
@voteable.should_receive(:on_add_vote)
|
115
|
+
@voteable.add_vote!(1, @user)
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
|
7
|
+
require 'fileutils'
|
8
|
+
require 'ostruct'
|
9
|
+
require 'log_buddy'
|
10
|
+
|
11
|
+
require 'rspec'
|
12
|
+
|
13
|
+
require 'mm-voteable'
|
14
|
+
|
15
|
+
# Requires supporting files with custom matchers and macros, etc,
|
16
|
+
# in ./support/ and its subdirectories.
|
17
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
18
|
+
|
19
|
+
log_dir = File.expand_path('../../log', __FILE__)
|
20
|
+
FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
|
21
|
+
|
22
|
+
logger = Logger.new(log_dir + '/test.log')
|
23
|
+
LogBuddy.init(:logger => logger)
|
24
|
+
|
25
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
|
26
|
+
MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
|
27
|
+
MongoMapper.database.collections.each { |c| c.drop_indexes }
|
28
|
+
|
29
|
+
|
30
|
+
RSpec.configure do |config|
|
31
|
+
|
32
|
+
config.after(:each) do
|
33
|
+
MongoMapper.database.collections.collect do |collection|
|
34
|
+
unless %w(system.js system.indexes system.users).any? { |name| name == collection.name }
|
35
|
+
MongoMapper.database.drop_collection(collection.name)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class VoteableWithCallback
|
2
|
+
include MongoMapper::Document
|
3
|
+
plugin MongoMapper::Plugins::Voteable
|
4
|
+
|
5
|
+
key :user_id, ObjectId
|
6
|
+
|
7
|
+
def on_add_vote(vote)
|
8
|
+
reload
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class VoteableWithoutCallback
|
13
|
+
include MongoMapper::Document
|
14
|
+
plugin MongoMapper::Plugins::Voteable
|
15
|
+
|
16
|
+
key :user_id, ObjectId
|
17
|
+
end
|
18
|
+
|
19
|
+
class User
|
20
|
+
include MongoMapper::Document
|
21
|
+
|
22
|
+
key :email, String
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,263 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mm-voteable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Luke Cunningham
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-18 00:00:00 +11:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 23
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
version: 1.1.2
|
34
|
+
name: mongo
|
35
|
+
requirement: *id001
|
36
|
+
type: :development
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - "="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 23
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 1
|
48
|
+
- 2
|
49
|
+
version: 1.1.2
|
50
|
+
name: bson_ext
|
51
|
+
requirement: *id002
|
52
|
+
type: :development
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
prerelease: false
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
name: mongo_ext
|
65
|
+
requirement: *id003
|
66
|
+
type: :development
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
name: mongo_mapper
|
79
|
+
requirement: *id004
|
80
|
+
type: :development
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
prerelease: false
|
83
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
name: i18n
|
93
|
+
requirement: *id005
|
94
|
+
type: :development
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
prerelease: false
|
97
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
name: log_buddy
|
107
|
+
requirement: *id006
|
108
|
+
type: :development
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
prerelease: false
|
111
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
hash: 3
|
117
|
+
segments:
|
118
|
+
- 0
|
119
|
+
version: "0"
|
120
|
+
name: ruby-debug
|
121
|
+
requirement: *id007
|
122
|
+
type: :development
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
prerelease: false
|
125
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ~>
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
hash: 11
|
131
|
+
segments:
|
132
|
+
- 2
|
133
|
+
- 1
|
134
|
+
- 0
|
135
|
+
version: 2.1.0
|
136
|
+
name: rspec
|
137
|
+
requirement: *id008
|
138
|
+
type: :development
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
prerelease: false
|
141
|
+
version_requirements: &id009 !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ~>
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
hash: 7
|
147
|
+
segments:
|
148
|
+
- 0
|
149
|
+
- 6
|
150
|
+
- 0
|
151
|
+
version: 0.6.0
|
152
|
+
name: yard
|
153
|
+
requirement: *id009
|
154
|
+
type: :development
|
155
|
+
- !ruby/object:Gem::Dependency
|
156
|
+
prerelease: false
|
157
|
+
version_requirements: &id010 !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ~>
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
hash: 23
|
163
|
+
segments:
|
164
|
+
- 1
|
165
|
+
- 0
|
166
|
+
- 0
|
167
|
+
version: 1.0.0
|
168
|
+
name: bundler
|
169
|
+
requirement: *id010
|
170
|
+
type: :development
|
171
|
+
- !ruby/object:Gem::Dependency
|
172
|
+
prerelease: false
|
173
|
+
version_requirements: &id011 !ruby/object:Gem::Requirement
|
174
|
+
none: false
|
175
|
+
requirements:
|
176
|
+
- - ~>
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
hash: 1
|
179
|
+
segments:
|
180
|
+
- 1
|
181
|
+
- 5
|
182
|
+
- 1
|
183
|
+
version: 1.5.1
|
184
|
+
name: jeweler
|
185
|
+
requirement: *id011
|
186
|
+
type: :development
|
187
|
+
- !ruby/object:Gem::Dependency
|
188
|
+
prerelease: false
|
189
|
+
version_requirements: &id012 !ruby/object:Gem::Requirement
|
190
|
+
none: false
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
hash: 3
|
195
|
+
segments:
|
196
|
+
- 0
|
197
|
+
version: "0"
|
198
|
+
name: rcov
|
199
|
+
requirement: *id012
|
200
|
+
type: :development
|
201
|
+
description: A very simple mongomapper plugin to add vote up/down functionality to your application
|
202
|
+
email: luke@icaruswings.com
|
203
|
+
executables: []
|
204
|
+
|
205
|
+
extensions: []
|
206
|
+
|
207
|
+
extra_rdoc_files:
|
208
|
+
- LICENSE.txt
|
209
|
+
- README.rdoc
|
210
|
+
files:
|
211
|
+
- .document
|
212
|
+
- .rspec
|
213
|
+
- Gemfile
|
214
|
+
- Gemfile.lock
|
215
|
+
- LICENSE.txt
|
216
|
+
- README.rdoc
|
217
|
+
- Rakefile
|
218
|
+
- VERSION
|
219
|
+
- lib/mm-voteable.rb
|
220
|
+
- lib/mongo_mapper/plugins/voteable.rb
|
221
|
+
- lib/mongo_mapper/plugins/voteable/vote.rb
|
222
|
+
- mm-voteable.gemspec
|
223
|
+
- spec/mm-voteable_spec.rb
|
224
|
+
- spec/spec_helper.rb
|
225
|
+
- spec/support/models.rb
|
226
|
+
has_rdoc: true
|
227
|
+
homepage: http://github.com/icaruswings/mm-voteable
|
228
|
+
licenses:
|
229
|
+
- MIT
|
230
|
+
post_install_message:
|
231
|
+
rdoc_options: []
|
232
|
+
|
233
|
+
require_paths:
|
234
|
+
- lib
|
235
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
236
|
+
none: false
|
237
|
+
requirements:
|
238
|
+
- - ">="
|
239
|
+
- !ruby/object:Gem::Version
|
240
|
+
hash: 3
|
241
|
+
segments:
|
242
|
+
- 0
|
243
|
+
version: "0"
|
244
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
245
|
+
none: false
|
246
|
+
requirements:
|
247
|
+
- - ">="
|
248
|
+
- !ruby/object:Gem::Version
|
249
|
+
hash: 3
|
250
|
+
segments:
|
251
|
+
- 0
|
252
|
+
version: "0"
|
253
|
+
requirements: []
|
254
|
+
|
255
|
+
rubyforge_project:
|
256
|
+
rubygems_version: 1.3.7
|
257
|
+
signing_key:
|
258
|
+
specification_version: 3
|
259
|
+
summary: A very simple mongomapper plugin to add vote up/down functionality to your application
|
260
|
+
test_files:
|
261
|
+
- spec/mm-voteable_spec.rb
|
262
|
+
- spec/spec_helper.rb
|
263
|
+
- spec/support/models.rb
|