voter_love 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/README.rdoc +53 -1
- data/Rakefile +2 -23
- data/lib/generators/voter_love/voter_love_generator.rb +1 -1
- data/lib/tasks/voter_love_tasks.rake +1 -1
- data/lib/voter_love/version.rb +1 -1
- data/lib/voter_love/votable.rb +1 -1
- data/lib/voter_love/votes.rb +1 -1
- data/spec/lib/voter_love_spec.rb +23 -56
- metadata +16 -16
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -1,3 +1,55 @@
|
|
1
1
|
= Voter Love
|
2
2
|
|
3
|
-
|
3
|
+
A simple, easy to use Rails 3.1 voting Gem.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
add the voter_love gem to your Gemfile:
|
8
|
+
|
9
|
+
gem 'voter_love'
|
10
|
+
|
11
|
+
create and generate the voter_love migration:
|
12
|
+
|
13
|
+
rails generate voter_love
|
14
|
+
|
15
|
+
you will then need to add up_votes and down_votes columns to your User (voter) and Object (e.g. links) (votable) migrations:
|
16
|
+
|
17
|
+
add_column :users, :up_votes, :integer, :null => false, :default => 0
|
18
|
+
add_column :users, :down_votes, :integer, :null => false, :default => 0
|
19
|
+
add_column :links, :up_votes, :integer, :null => false, :default => 0
|
20
|
+
add_column :links, :down_votes, :integer, :null => false, :default => 0
|
21
|
+
|
22
|
+
== Usage
|
23
|
+
|
24
|
+
Turn your objects (e.g. links) into a votable model:
|
25
|
+
|
26
|
+
class Link < ActiveRecord::Base
|
27
|
+
acts_as_votable
|
28
|
+
end
|
29
|
+
|
30
|
+
Turn your users (or any other model) into a voter model:
|
31
|
+
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
acts_as_voter
|
34
|
+
end
|
35
|
+
|
36
|
+
To vote for an object (with an error raised if user already voted on the object):
|
37
|
+
|
38
|
+
user.up_vote(link)
|
39
|
+
user.down_vote(link)
|
40
|
+
|
41
|
+
To vote for an object (and ignore the vote if the user has already voted on the object):
|
42
|
+
|
43
|
+
user.up_vote!(link)
|
44
|
+
user.down_vote!(link)
|
45
|
+
|
46
|
+
Total score (up votes - down votes)
|
47
|
+
|
48
|
+
link.score
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
data/Rakefile
CHANGED
@@ -1,28 +1,7 @@
|
|
1
|
-
|
2
|
-
begin
|
3
|
-
require 'bundler/setup'
|
4
|
-
rescue LoadError
|
5
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
-
end
|
7
|
-
begin
|
8
|
-
require 'rdoc/task'
|
9
|
-
rescue LoadError
|
10
|
-
require 'rdoc/rdoc'
|
11
|
-
require 'rake/rdoctask'
|
12
|
-
RDoc::Task = Rake::RDocTask
|
13
|
-
end
|
14
|
-
|
15
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
-
rdoc.rdoc_dir = 'rdoc'
|
17
|
-
rdoc.title = 'VoterLove'
|
18
|
-
rdoc.options << '--line-numbers'
|
19
|
-
rdoc.rdoc_files.include('README.rdoc')
|
20
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
-
end
|
22
|
-
|
1
|
+
require 'bundler'
|
23
2
|
Bundler::GemHelper.install_tasks
|
24
3
|
|
25
4
|
require 'rspec/core/rake_task'
|
26
5
|
RSpec::Core::RakeTask.new(:spec)
|
27
6
|
|
28
|
-
task :default => :spec
|
7
|
+
task :default => :spec
|
@@ -4,7 +4,7 @@ require 'rails/generators/active_record'
|
|
4
4
|
class VoterLoveGenerator < Rails::Generators::Base
|
5
5
|
include Rails::Generators::Migration
|
6
6
|
|
7
|
-
desc "Generates a migration for the
|
7
|
+
desc "Generates a migration for the Votes model"
|
8
8
|
|
9
9
|
def self.source_root
|
10
10
|
@source_root ||= File.dirname(__FILE__) + '/templates'
|
data/lib/voter_love/version.rb
CHANGED
data/lib/voter_love/votable.rb
CHANGED
data/lib/voter_love/votes.rb
CHANGED
data/spec/lib/voter_love_spec.rb
CHANGED
@@ -2,11 +2,11 @@ require File.expand_path('../../spec_helper', __FILE__)
|
|
2
2
|
|
3
3
|
describe "Voter Love" do
|
4
4
|
before(:each) do
|
5
|
-
@votable = VotableModel.create(:name => "
|
6
|
-
@voter = VoterModel.create(:name => "
|
5
|
+
@votable = VotableModel.create(:name => "votable a")
|
6
|
+
@voter = VoterModel.create(:name => "voter b")
|
7
7
|
end
|
8
8
|
|
9
|
-
it "should
|
9
|
+
it "should be votable" do
|
10
10
|
@votable.class.should == VotableModel
|
11
11
|
@votable.class.votable?.should == true
|
12
12
|
end
|
@@ -18,11 +18,11 @@ describe "Voter Love" do
|
|
18
18
|
|
19
19
|
it "should get correct vote summary" do
|
20
20
|
@voter.up_vote(@votable).should == true
|
21
|
-
@votable.
|
21
|
+
@votable.score.should == 1
|
22
22
|
@voter.down_vote(@votable).should == true
|
23
|
-
@votable.
|
23
|
+
@votable.score.should == -1
|
24
24
|
@voter.unvote(@votable).should == true
|
25
|
-
@votable.
|
25
|
+
@votable.score.should == 0
|
26
26
|
end
|
27
27
|
|
28
28
|
it "votable should have up vote votes" do
|
@@ -67,10 +67,10 @@ describe "Voter Love" do
|
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should create a voting" do
|
70
|
-
VoterLove::
|
70
|
+
VoterLove::Vote.count.should == 0
|
71
71
|
@voter.up_vote(@votable)
|
72
|
-
VoterLove::
|
73
|
-
voting = VoterLove::
|
72
|
+
VoterLove::Vote.count.should == 1
|
73
|
+
voting = VoterLove::Vote.first
|
74
74
|
voting.votable.should == @votable
|
75
75
|
voting.voter.should == @voter
|
76
76
|
voting.up_vote.should == true
|
@@ -95,15 +95,15 @@ describe "Voter Love" do
|
|
95
95
|
@votable.down_votes.should == 1
|
96
96
|
@voter.up_votes.should == 0
|
97
97
|
@voter.down_votes.should == 1
|
98
|
-
VoterLove::
|
99
|
-
VoterLove::
|
98
|
+
VoterLove::Vote.count.should == 1
|
99
|
+
VoterLove::Vote.first.up_vote.should be_false
|
100
100
|
@voter.up_vote(@votable)
|
101
101
|
@votable.up_votes.should == 1
|
102
102
|
@votable.down_votes.should == 0
|
103
103
|
@voter.up_votes.should == 1
|
104
104
|
@voter.down_votes.should == 0
|
105
|
-
VoterLove::
|
106
|
-
VoterLove::
|
105
|
+
VoterLove::Vote.count.should == 1
|
106
|
+
VoterLove::Vote.first.up_vote.should be_true
|
107
107
|
end
|
108
108
|
|
109
109
|
it "should allow up votes from different voters" do
|
@@ -111,7 +111,7 @@ describe "Voter Love" do
|
|
111
111
|
@voter.up_vote(@votable)
|
112
112
|
@voter2.up_vote(@votable)
|
113
113
|
@votable.up_votes.should == 2
|
114
|
-
VoterLove::
|
114
|
+
VoterLove::Vote.count.should == 2
|
115
115
|
end
|
116
116
|
|
117
117
|
it "should raise an error for an invalid votable" do
|
@@ -141,10 +141,10 @@ describe "Voter Love" do
|
|
141
141
|
end
|
142
142
|
|
143
143
|
it "should create a voting" do
|
144
|
-
VoterLove::
|
144
|
+
VoterLove::Vote.count.should == 0
|
145
145
|
@voter.down_vote(@votable)
|
146
|
-
VoterLove::
|
147
|
-
voting = VoterLove::
|
146
|
+
VoterLove::Vote.count.should == 1
|
147
|
+
voting = VoterLove::Vote.first
|
148
148
|
voting.votable.should == @votable
|
149
149
|
voting.voter.should == @voter
|
150
150
|
voting.up_vote.should == false
|
@@ -160,7 +160,7 @@ describe "Voter Love" do
|
|
160
160
|
lambda {
|
161
161
|
@voter.down_vote!(@votable).should == false
|
162
162
|
}.should_not raise_error(VoterLove::Exceptions::AlreadyVotedError)
|
163
|
-
VoterLove::
|
163
|
+
VoterLove::Vote.count.should == 1
|
164
164
|
end
|
165
165
|
|
166
166
|
it "should change an up vote to a down vote" do
|
@@ -169,15 +169,15 @@ describe "Voter Love" do
|
|
169
169
|
@votable.down_votes.should == 0
|
170
170
|
@voter.up_votes.should == 1
|
171
171
|
@voter.down_votes.should == 0
|
172
|
-
VoterLove::
|
173
|
-
VoterLove::
|
172
|
+
VoterLove::Vote.count.should == 1
|
173
|
+
VoterLove::Vote.first.up_vote.should be_true
|
174
174
|
@voter.down_vote(@votable)
|
175
175
|
@votable.up_votes.should == 0
|
176
176
|
@votable.down_votes.should == 1
|
177
177
|
@voter.up_votes.should == 0
|
178
178
|
@voter.down_votes.should == 1
|
179
|
-
VoterLove::
|
180
|
-
VoterLove::
|
179
|
+
VoterLove::Vote.count.should == 1
|
180
|
+
VoterLove::Vote.first.up_vote.should be_false
|
181
181
|
end
|
182
182
|
|
183
183
|
it "should allow down votes from different voters" do
|
@@ -185,7 +185,7 @@ describe "Voter Love" do
|
|
185
185
|
@voter.down_vote(@votable)
|
186
186
|
@voter2.down_vote(@votable)
|
187
187
|
@votable.down_votes.should == 2
|
188
|
-
VoterLove::
|
188
|
+
VoterLove::Vote.count.should == 2
|
189
189
|
end
|
190
190
|
|
191
191
|
it "should raise an error for an invalid votable" do
|
@@ -200,37 +200,4 @@ describe "Voter Love" do
|
|
200
200
|
@voter.down_voted?(@votable).should be_true
|
201
201
|
end
|
202
202
|
end
|
203
|
-
|
204
|
-
describe "unvote" do
|
205
|
-
it "should decrease the up votes if up voted before" do
|
206
|
-
@voter.up_vote(@votable)
|
207
|
-
@votable.up_votes.should == 1
|
208
|
-
@voter.up_votes.should == 1
|
209
|
-
@voter.unvote(@votable)
|
210
|
-
@votable.up_votes.should == 0
|
211
|
-
@voter.up_votes.should == 0
|
212
|
-
end
|
213
|
-
|
214
|
-
it "should remove the voting" do
|
215
|
-
@voter.up_vote(@votable)
|
216
|
-
VoterLove::Votes.count.should == 1
|
217
|
-
@voter.unvote(@votable)
|
218
|
-
VoterLove::Votes.count.should == 0
|
219
|
-
end
|
220
|
-
|
221
|
-
it "should raise an error if voter didn't vote for the votable" do
|
222
|
-
lambda { @voter.unvote(@votable) }.should raise_error(VoterLove::Exceptions::NotVotedError)
|
223
|
-
end
|
224
|
-
|
225
|
-
it "should not raise error if voter didn't vote for the votable and unvote! is called" do
|
226
|
-
lambda {
|
227
|
-
@voter.unvote!(@votable).should == false
|
228
|
-
}.should_not raise_error(VoterLove::Exceptions::NotVotedError)
|
229
|
-
end
|
230
|
-
|
231
|
-
it "should raise an error for an invalid votable" do
|
232
|
-
invalid_votable = InvalidVotableModel.create
|
233
|
-
lambda { @voter.unvote(invalid_votable) }.should raise_error(VoterLove::Exceptions::InvalidVotableError)
|
234
|
-
end
|
235
|
-
end
|
236
203
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voter_love
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &2158069260 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2158069260
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
requirement: &
|
27
|
+
requirement: &2158068760 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2158068760
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bundler
|
38
|
-
requirement: &
|
38
|
+
requirement: &2158068300 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2158068300
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: database_cleaner
|
49
|
-
requirement: &
|
49
|
+
requirement: &2158067840 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - =
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 0.6.7
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2158067840
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: sqlite3-ruby
|
60
|
-
requirement: &
|
60
|
+
requirement: &2158067380 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.3.0
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2158067380
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &2158066920 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 2.5.0
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2158066920
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: generator_spec
|
82
|
-
requirement: &
|
82
|
+
requirement: &2158066460 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: 0.8.2
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *2158066460
|
91
91
|
description: The voter_love Gem allows users to easily vote on objects
|
92
92
|
email:
|
93
93
|
- karlgusner@gmail.com
|