Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ryanto/acts_as_votable
base: master
...
head fork: snicol/acts_as_votable
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 7 commits
  • 8 files changed
  • 0 commit comments
  • 4 contributors
View
8 README.markdown
@@ -175,7 +175,7 @@ end
@article.dislikes.size # => 0
```
-To check if a voter has voted on a model, you can use ``voted_for?``. You can
+To check if a voter has voted on a model, you can use ``voted_on?``. You can
check how the voter voted by using ``voted_as_when_voted_for``.
```ruby
@@ -183,9 +183,9 @@ check how the voter voted by using ``voted_as_when_voted_for``.
@user.up_votes @comment2
# user has not voted on @comment3
-@user.voted_for? @comment1 # => true
-@user.voted_for? @comment2 # => true
-@user.voted_for? @comment3 # => false
+@user.voted_on? @comment1 # => true
+@user.voted_on? @comment2 # => true
+@user.voted_on? @comment3 # => false
@user.voted_as_when_voted_for @comment1 # => true, he liked it
@user.voted_as_when_voted_for @comment2 # => false, he didnt like it
View
4 lib/acts_as_votable/helpers/words.rb
@@ -1,9 +1,9 @@
module ActsAsVotable::Helpers
- # this helper provides methods that help find what words are
+ # this helper provides methods that help find what words are
# up votes and what words are down votes
#
- # It can be called
+ # It can be called
#
# votable_object.votable_words.that_mean_true
#
View
18 lib/acts_as_votable/votable.rb
@@ -6,13 +6,13 @@ module Votable
include Helpers::Words
def self.included base
-
- # allow the user to define these himself
+
+ # allow the user to define these himself
aliases = {
:vote_up => [
- :up_by, :upvote_by, :like_by, :liked_by, :vote_by,
- :up_from, :upvote_from, :upvote_by, :like_from, :liked_from, :vote_from
+ :up_by, :upvote_by, :like_by, :liked_by, :vote_by,
+ :up_from, :upvote_from, :upvote_by, :like_from, :liked_from, :vote_from
],
:vote_down => [
@@ -27,7 +27,7 @@ def self.included base
:down_votes => [
:false_votes, :downs, :downvotes, :dislikes, :negatives
],
- :unvote => [
+ :unvote_by => [
:unliked_by, :undisliked_by
]
}
@@ -81,7 +81,7 @@ def vote args = {}
_votes_ = find_votes({
:voter_id => options[:voter].id,
:vote_scope => options[:vote_scope],
- :voter_type => options[:voter].class.name
+ :voter_type => options[:voter].class.base_class.name
})
if _votes_.count == 0
@@ -122,6 +122,8 @@ def unvote args = {}
return true
end
+ # helper methods
+
def vote_up voter, options={}
self.vote :voter => voter, :vote => true, :vote_scope => options[:vote_scope]
end
@@ -130,6 +132,10 @@ def vote_down voter, options={}
self.vote :voter => voter, :vote => false, :vote_scope => options[:vote_scope]
end
+ def unvote_by(voter, options={})
+ self.unvote :voter => voter, :vote_scope => options[:vote_scope]
+ end
+
# caching
def update_cached_votes
View
12 lib/acts_as_votable/voter.rb
@@ -50,26 +50,26 @@ def unvote_for model
# results
def voted_on? votable, args={}
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
:vote_scope => args[:vote_scope])
votes.size > 0
end
def voted_up_on? votable, args={}
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
:vote_scope => args[:vote_scope], :vote_flag => true)
votes.size > 0
end
def voted_down_on? votable, args={}
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
:vote_scope => args[:vote_scope], :vote_flag => false)
votes.size > 0
end
alias :voted_down_for? :voted_down_on?
def voted_as_when_voting_on votable, args={}
- votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name,
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name,
:vote_scope => args[:vote_scope])
return nil if votes.size == 0
return votes.first.vote_flag
@@ -89,7 +89,7 @@ def find_down_votes args={}
end
def find_votes_for_class klass, extra_conditions = {}
- find_votes extra_conditions.merge({:votable_type => klass.name})
+ find_votes extra_conditions.merge({:votable_type => klass.base_class.name})
end
def find_up_votes_for_class klass, args={}
@@ -106,7 +106,7 @@ def include_objects
end
def find_voted_items extra_conditions = {}
- options = extra_conditions.merge :voter_id => id, :voter_type => self.class.name
+ options = extra_conditions.merge :voter_id => id, :voter_type => self.class.base_class.name
include_objects.where(options).collect(&:votable)
end
View
46 spec/alias_spec.rb
@@ -0,0 +1,46 @@
+require 'acts_as_votable'
+require 'spec_helper'
+
+describe ActsAsVotable::Votable do
+
+ before(:each) do
+ clean_database
+ end
+
+ describe "voting on a votable object" do
+
+ before(:each) do
+ clean_database
+ @voter = Voter.new(:name => 'i can vote!')
+ @voter.save
+
+ @voter2 = Voter.new(:name => 'a new person')
+ @voter2.save
+
+ @votable = Votable.new(:name => 'a voting model')
+ @votable.save
+ end
+
+ it "should vote with liked_by and disliked_by" do
+ @votable.liked_by @voter
+ @votable.find_votes.count.should == 1
+
+ @votable.liked_by @voter2
+ @votable.find_votes.count.should == 2
+ end
+
+ it "should unvote with unliked_by and undisliked_by" do
+ @votable.liked_by @voter
+ @votable.find_votes.count.should == 1
+ @votable.unliked_by @voter
+ @votable.find_votes.count.should == 0
+
+ @votable.disliked_by @voter
+ @votable.find_votes.count.should == 1
+ @votable.undisliked_by @voter
+ @votable.find_votes.count.should == 0
+ end
+
+ end
+
+end
View
6 spec/spec_helper.rb
@@ -62,7 +62,11 @@ class Voter < ActiveRecord::Base
end
class NotVoter < ActiveRecord::Base
-
+
+end
+
+class StiVoter < Voter
+
end
class Votable < ActiveRecord::Base
View
4 spec/votable_spec.rb
@@ -282,10 +282,6 @@
votable.vote :voter => @voter, :vote => 'yes'
votable.votes.size.should == 1
end
-
end
-
end
-
-
end
View
55 spec/voter_spec.rb
@@ -300,5 +300,60 @@
subject.size.should == 1
end
end
+
+ describe 'STI voter' do
+ before(:each) do
+ clean_database
+ @voter = StiVoter.create(:name => 'i can vote!')
+ end
+
+ it "should be able to vote only once" do
+ votable = ChildOfStiVotable.create(:name => 'sti child')
+
+ votable.vote :voter => @voter, :vote => 'yes'
+ votable.vote :voter => @voter, :vote => 'yes'
+ votable.votes.size.should == 1
+ end
+
+ describe '#voted_on?' do
+ it "should return true if the voter has voted" do
+ votable = ChildOfStiVotable.create(:name => 'sti child')
+ votable.vote :voter => @voter, :vote => 'yes'
+ @voter.voted_on?(votable).should be_true
+ end
+ end
+
+ describe '#voted_up_on?' do
+ it "should return true if the voter has voted true" do
+ votable = ChildOfStiVotable.create(:name => 'sti child')
+ votable.vote :voter => @voter, :vote => 'yes'
+ @voter.voted_up_on?(votable).should be_true
+ end
+ end
+
+ describe '#voted_down_on?' do
+ it "should return true if the voter has voted false" do
+ votable = ChildOfStiVotable.create(:name => 'sti child')
+ votable.vote :voter => @voter, :vote => 'no'
+ @voter.voted_down_on?(votable).should be_true
+ end
+ end
+
+ describe '#find_votes_for_class' do
+ it "should get all of the votes votes for a sti class" do
+ votable = ChildOfStiVotable.create(:name => 'sti child')
+ votable.vote :voter => @voter, :vote => 'no'
+ @voter.find_votes_for_class(ChildOfStiVotable).size.should == 1
+ end
+ end
+
+ describe '#find_voted_items' do
+ it "returns objects that a user has upvoted for" do
+ votable = ChildOfStiVotable.create(:name => 'sti child')
+ votable.vote :voter => @voter, :vote => 'no'
+ @voter.find_voted_items.should include votable
+ end
+ end
+ end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.