Skip to content

Commit

Permalink
Fix voter methods to workk with sti voters
Browse files Browse the repository at this point in the history
  • Loading branch information
Herman Moreno authored and gilbert committed Feb 27, 2013
1 parent c196c00 commit f6f8dfd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/acts_as_votable/voter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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={}
Expand All @@ -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

Expand Down
39 changes: 38 additions & 1 deletion spec/votable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,46 @@
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

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


Expand Down

0 comments on commit f6f8dfd

Please sign in to comment.