jy-acts_as_votable 0.3.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.
@@ -0,0 +1,250 @@
1
+ require 'acts_as_votable'
2
+ require 'spec_helper'
3
+
4
+ describe ActsAsVotable::Votable do
5
+
6
+ before(:each) do
7
+ clean_database
8
+ end
9
+
10
+ it "should not be votable" do
11
+ NotVotable.should_not be_votable
12
+ end
13
+
14
+ it "should be votable" do
15
+ Votable.should be_votable
16
+ end
17
+
18
+ describe "voting on a votable object" do
19
+
20
+ before(:each) do
21
+ clean_database
22
+ @voter = Voter.new(:name => 'i can vote!')
23
+ @voter.save
24
+
25
+ @voter2 = Voter.new(:name => 'a new person')
26
+ @voter2.save
27
+
28
+ @votable = Votable.new(:name => 'a voting model')
29
+ @votable.save
30
+ end
31
+
32
+ it "should return false when a vote with no voter is saved" do
33
+ @votable.vote.should be false
34
+ end
35
+
36
+ it "should have one vote when saved" do
37
+ @votable.vote :voter => @voter, :vote => 'yes'
38
+ @votable.votes.size.should == 1
39
+ end
40
+
41
+ it "should have one vote when voted on twice by the same person" do
42
+ @votable.vote :voter => @voter, :vote => 'yes'
43
+ @votable.vote :voter => @voter, :vote => 'no'
44
+ @votable.votes.size.should == 1
45
+ end
46
+
47
+ it "should be callable with vote_up" do
48
+ @votable.vote_up @voter
49
+ @votable.up_votes.first.voter.should == @voter
50
+ end
51
+
52
+ it "should be callable with vote_down" do
53
+ @votable.vote_down @voter
54
+ @votable.down_votes.first.voter.should == @voter
55
+ end
56
+
57
+ it "should have 2 votes when voted on once by two different people" do
58
+ @votable.vote :voter => @voter
59
+ @votable.vote :voter => @voter2
60
+ @votable.votes.size.should == 2
61
+ end
62
+
63
+ it "should have one true vote" do
64
+ @votable.vote :voter => @voter
65
+ @votable.vote :voter => @voter2, :vote => 'dislike'
66
+ @votable.up_votes.size.should == 1
67
+ end
68
+
69
+ it "should have 2 false votes" do
70
+ @votable.vote :voter => @voter, :vote => 'no'
71
+ @votable.vote :voter => @voter2, :vote => 'dislike'
72
+ @votable.down_votes.size.should == 2
73
+ end
74
+
75
+ it "should have been voted on by voter2" do
76
+ @votable.vote :voter => @voter2, :vote => true
77
+ @votable.find_votes.first.voter.id.should be @voter2.id
78
+ end
79
+
80
+ it "should count the vote as registered if this is the voters first vote" do
81
+ @votable.vote :voter => @voter
82
+ @votable.vote_registered?.should be true
83
+ end
84
+
85
+ it "should not count the vote as being registered if that voter has already voted and the vote has not changed" do
86
+ @votable.vote :voter => @voter, :vote => true
87
+ @votable.vote :voter => @voter, :vote => 'yes'
88
+ @votable.vote_registered?.should be false
89
+ end
90
+
91
+ it "should count the vote as registered if the voter has voted and the vote has changed" do
92
+ @votable.vote :voter => @voter, :vote => true
93
+ @votable.vote :voter => @voter, :vote => 'dislike'
94
+ @votable.vote_registered?.should be true
95
+ end
96
+
97
+ it "should be voted on by voter" do
98
+ @votable.vote :voter => @voter
99
+ @votable.voted_on_by?(@voter).should be true
100
+ end
101
+
102
+ it "should unvote a positive vote" do
103
+ @votable.vote :voter => @voter
104
+ @votable.unvote :voter => @voter
105
+ @votable.find_votes.count.should == 0
106
+ end
107
+
108
+ it "should set the votable to unregistered after unvoting" do
109
+ @votable.vote :voter => @voter
110
+ @votable.unvote :voter => @voter
111
+ @votable.vote_registered?.should be false
112
+ end
113
+
114
+ it "should unvote a negative vote" do
115
+ @votable.vote :voter => @voter, :vote => 'no'
116
+ @votable.unvote :voter => @voter
117
+ @votable.find_votes.count.should == 0
118
+ end
119
+
120
+ it "should unvote only the from a single voter" do
121
+ @votable.vote :voter => @voter
122
+ @votable.vote :voter => @voter2
123
+ @votable.unvote :voter => @voter
124
+ @votable.find_votes.count.should == 1
125
+ end
126
+
127
+ it "should be contained to instances" do
128
+ votable2 = Votable.new(:name => '2nd votable')
129
+ votable2.save
130
+
131
+ @votable.vote :voter => @voter, :vote => false
132
+ votable2.vote :voter => @voter, :vote => true
133
+ votable2.vote :voter => @voter, :vote => true
134
+
135
+ @votable.vote_registered?.should be true
136
+ votable2.vote_registered?.should be false
137
+ end
138
+
139
+ describe "with cached votes" do
140
+
141
+ before(:each) do
142
+ clean_database
143
+ @voter = Voter.new(:name => 'i can vote!')
144
+ @voter.save
145
+
146
+ @votable = Votable.new(:name => 'a voting model without a cache')
147
+ @votable.save
148
+
149
+ @votable_cache = VotableCache.new(:name => 'voting model with cache')
150
+ @votable_cache.save
151
+ end
152
+
153
+ it "should not update cached votes if there are no columns" do
154
+ @votable.vote :voter => @voter
155
+ end
156
+
157
+ it "should update cached total votes if there is a total column" do
158
+ @votable_cache.cached_votes_total = 50
159
+ @votable_cache.vote :voter => @voter
160
+ @votable_cache.cached_votes_total.should == 1
161
+ end
162
+
163
+ it "should update cached total votes when a vote up is removed" do
164
+ @votable_cache.vote :voter => @voter, :vote => 'true'
165
+ @votable_cache.unvote :voter => @voter
166
+ @votable_cache.cached_votes_total.should == 0
167
+ end
168
+
169
+ it "should update cached total votes when a vote down is removed" do
170
+ @votable_cache.vote :voter => @voter, :vote => 'false'
171
+ @votable_cache.unvote :voter => @voter
172
+ @votable_cache.cached_votes_total.should == 0
173
+ end
174
+
175
+ it "should update cached up votes if there is an up vote column" do
176
+ @votable_cache.cached_votes_up = 50
177
+ @votable_cache.vote :voter => @voter
178
+ @votable_cache.vote :voter => @voter
179
+ @votable_cache.cached_votes_up.should == 1
180
+ end
181
+
182
+ it "should update cached down votes if there is a down vote column" do
183
+ @votable_cache.cached_votes_down = 50
184
+ @votable_cache.vote :voter => @voter, :vote => 'false'
185
+ @votable_cache.cached_votes_down.should == 1
186
+ end
187
+
188
+ it "should update cached up votes when a vote up is removed" do
189
+ @votable_cache.vote :voter => @voter, :vote => 'true'
190
+ @votable_cache.unvote :voter => @voter
191
+ @votable_cache.cached_votes_up.should == 0
192
+ end
193
+
194
+ it "should update cached down votes when a vote down is removed" do
195
+ @votable_cache.vote :voter => @voter, :vote => 'false'
196
+ @votable_cache.unvote :voter => @voter
197
+ @votable_cache.cached_votes_down.should == 0
198
+ end
199
+
200
+ it "should select from cached total votes if there a total column" do
201
+ @votable_cache.vote :voter => @voter
202
+ @votable_cache.cached_votes_total = 50
203
+ @votable_cache.count_votes_total.should == 50
204
+ end
205
+
206
+ it "should select from cached up votes if there is an up vote column" do
207
+ @votable_cache.vote :voter => @voter
208
+ @votable_cache.cached_votes_up = 50
209
+ @votable_cache.count_votes_up.should == 50
210
+ end
211
+
212
+ it "should select from cached down votes if there is a down vote column" do
213
+ @votable_cache.vote :voter => @voter, :vote => 'false'
214
+ @votable_cache.cached_votes_down = 50
215
+ @votable_cache.count_votes_down.should == 50
216
+ end
217
+
218
+ end
219
+
220
+ describe "sti models" do
221
+
222
+ before(:each) do
223
+ clean_database
224
+ @voter = Voter.create(:name => 'i can vote!')
225
+ end
226
+
227
+ it "should be able to vote on a votable child of a non votable sti model" do
228
+ votable = VotableChildOfStiNotVotable.create(:name => 'sti child')
229
+
230
+ votable.vote :voter => @voter, :vote => 'yes'
231
+ votable.votes.size.should == 1
232
+ end
233
+
234
+ it "should not be able to vote on a parent non votable" do
235
+ StiNotVotable.should_not be_votable
236
+ end
237
+
238
+ it "should be able to vote on a child when its parent is votable" do
239
+ votable = ChildOfStiVotable.create(:name => 'sti child')
240
+
241
+ votable.vote :voter => @voter, :vote => 'yes'
242
+ votable.votes.size.should == 1
243
+ end
244
+
245
+ end
246
+
247
+ end
248
+
249
+
250
+ end
@@ -0,0 +1,157 @@
1
+ require 'acts_as_votable'
2
+ require 'spec_helper'
3
+
4
+ describe ActsAsVotable::Voter do
5
+
6
+ before(:each) do
7
+ clean_database
8
+ end
9
+
10
+ it "should not be a voter" do
11
+ NotVotable.should_not be_votable
12
+ end
13
+
14
+ it "should be a voter" do
15
+ Votable.should be_votable
16
+ end
17
+
18
+ describe "voting by a voter" do
19
+ ["", "sti_"].each do |prefix|
20
+
21
+ before(:each) do
22
+ clean_database
23
+ @voter = Voter.new(:name => 'i can vote!')
24
+ @voter.save
25
+
26
+ @voter2 = Voter.new(:name => 'a new person')
27
+ @voter2.save
28
+
29
+ @votable = Votable.new(:name => 'a voting model')
30
+ @votable.save
31
+
32
+ @votable2 = Votable.new(:name => 'a 2nd voting model')
33
+ @votable2.save
34
+
35
+ @sti_votable = ChildOfStiVotable.new(:name => 'STI voting model')
36
+ @sti_votable.save
37
+ @sti_votable = ChildOfStiVotable.new(:name => 'STI voting model')
38
+ @sti_votable.save
39
+
40
+ end
41
+
42
+ it "should be voted on after a voter has voted" do
43
+ @votable.vote :voter => @voter
44
+ @voter.voted_on?(@votable).should be true
45
+ end
46
+
47
+ it "should not be voted on if a voter has not voted" do
48
+ @voter.voted_on?(@votable).should be false
49
+ end
50
+
51
+ it "should be voted as true when a voter has voted true" do
52
+ @votable.vote :voter => @voter
53
+ @voter.voted_as_when_voted_for(@votable).should be true
54
+ end
55
+
56
+ it "should be voted as false when a voter has voted false" do
57
+ @votable.vote :voter => @voter, :vote => false
58
+ @voter.voted_as_when_voted_for(@votable).should be false
59
+ end
60
+
61
+ it "should be voted as nil when a voter has never voted" do
62
+ @voter.voted_as_when_voting_on(@votable).should be nil
63
+ end
64
+
65
+ it "should return true if voter has voted true" do
66
+ @votable.vote :voter => @voter
67
+ @voter.voted_up_on?(@votable).should be true
68
+ end
69
+
70
+ it "should return false if voter has not voted true" do
71
+ @votable.vote :voter => @voter, :vote => false
72
+ @voter.voted_up_on?(@votable).should be false
73
+ end
74
+
75
+ it "should return true if the voter has voted false" do
76
+ @votable.vote :voter => @voter, :vote => false
77
+ @voter.voted_down_on?(@votable).should be true
78
+ end
79
+
80
+ it "should return false if the voter has not voted false" do
81
+ @votable.vote :voter => @voter, :vote => true
82
+ @voter.voted_down_on?(@votable).should be false
83
+ end
84
+
85
+ it "should provide reserve functionality, voter can vote on votable" do
86
+ @voter.vote :votable => @votable, :vote => 'bad'
87
+ @voter.voted_as_when_voting_on(@votable).should be false
88
+ end
89
+
90
+ it "should allow the voter to vote up a model" do
91
+ @voter.vote_up_for @votable
92
+ @votable.up_votes.first.voter.should == @voter
93
+ @votable.votes.up.first.voter.should == @voter
94
+ end
95
+
96
+ it "should allow the voter to vote down a model" do
97
+ @voter.vote_down_for @votable
98
+ @votable.down_votes.first.voter.should == @voter
99
+ @votable.votes.down.first.voter.should == @voter
100
+ end
101
+
102
+ it "should allow the voter to unvote a model" do
103
+ @voter.vote_up_for @votable
104
+ @voter.unvote_for @votable
105
+ @votable.find_votes.size.should == 0
106
+ @votable.votes.count.should == 0
107
+ end
108
+
109
+ it "should get all of the voters votes" do
110
+ @voter.vote_up_for @votable
111
+ @voter.find_votes.size.should == 1
112
+ @voter.votes.up.count.should == 1
113
+ end
114
+
115
+ it "should get all of the voters up votes" do
116
+ @voter.vote_up_for @votable
117
+ @voter.find_up_votes.size.should == 1
118
+ @voter.votes.up.count.should == 1
119
+ end
120
+
121
+ it "should get all of the voters down votes" do
122
+ @voter.vote_down_for @votable
123
+ @voter.find_down_votes.size.should == 1
124
+ @voter.votes.down.count.should == 1
125
+ end
126
+
127
+ it "should get all of the votes votes for a class" do
128
+ @votable.vote :voter => @voter
129
+ @votable2.vote :voter => @voter, :vote => false
130
+ @voter.find_votes_for_class(Votable).size.should == 2
131
+ @voter.votes.for_type(Votable).count.should == 2
132
+ end
133
+
134
+ it "should get all of the voters up votes for a class" do
135
+ @votable.vote :voter => @voter
136
+ @votable2.vote :voter => @voter, :vote => false
137
+ @voter.find_up_votes_for_class(Votable).size.should == 1
138
+ @voter.votes.up.for_type(Votable).count.should == 1
139
+ end
140
+
141
+ it "should get all of the voters down votes for a class" do
142
+ @votable.vote :voter => @voter
143
+ @votable2.vote :voter => @voter, :vote => false
144
+ @voter.find_down_votes_for_class(Votable).size.should == 1
145
+ @voter.votes.down.for_type(Votable).count.should == 1
146
+ end
147
+
148
+ it "should be contained to instances" do
149
+ @voter.vote :votable => @votable, :vote => false
150
+ @voter2.vote :votable => @votable
151
+
152
+ @voter.voted_as_when_voting_on(@votable).should be false
153
+ end
154
+
155
+
156
+ end
157
+ end
@@ -0,0 +1,30 @@
1
+ require 'acts_as_votable'
2
+ require 'spec_helper'
3
+
4
+ describe ActsAsVotable::Helpers::Words do
5
+
6
+ before :each do
7
+ @vote = ActsAsVotable::Vote.new
8
+ end
9
+
10
+ it "should know that like is a true vote" do
11
+ @vote.votable_words.that_mean_true.should include "like"
12
+ end
13
+
14
+ it "should know that bad is a false vote" do
15
+ @vote.votable_words.that_mean_false.should include "bad"
16
+ end
17
+
18
+ it "should be a vote for true when word is good" do
19
+ @vote.votable_words.meaning_of('good').should be true
20
+ end
21
+
22
+ it "should be a vote for false when word is down" do
23
+ @vote.votable_words.meaning_of('down').should be false
24
+ end
25
+
26
+ it "should be a vote for true when the word is unknown" do
27
+ @vote.votable_words.meaning_of('lsdhklkadhfs').should be true
28
+ end
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jy-acts_as_votable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 1
10
+ version: 0.3.1
11
+ platform: ruby
12
+ authors:
13
+ - Ryan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-23 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: sqlite3
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rails
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 7
58
+ segments:
59
+ - 3
60
+ - 0
61
+ - 0
62
+ version: 3.0.0
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ description: Rails gem to allowing records to be votable
66
+ email:
67
+ - ryanto
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - README.markdown
79
+ - Rakefile
80
+ - acts_as_votable.gemspec
81
+ - lib/acts_as_votable.rb
82
+ - lib/acts_as_votable/extenders/votable.rb
83
+ - lib/acts_as_votable/extenders/voter.rb
84
+ - lib/acts_as_votable/helpers/words.rb
85
+ - lib/acts_as_votable/version.rb
86
+ - lib/acts_as_votable/votable.rb
87
+ - lib/acts_as_votable/vote.rb
88
+ - lib/acts_as_votable/voter.rb
89
+ - lib/generators/acts_as_votable/migration/migration_generator.rb
90
+ - lib/generators/acts_as_votable/migration/templates/active_record/migration.rb
91
+ - spec/spec_helper.rb
92
+ - spec/votable_spec.rb
93
+ - spec/voter_spec.rb
94
+ - spec/words_spec.rb
95
+ has_rdoc: true
96
+ homepage: http://rubygems.org/gems/acts_as_votable
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
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project: acts_as_votable
125
+ rubygems_version: 1.5.3
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Rails gem to allowing records to be votable
129
+ test_files: []
130
+