module FormalVote::ActsAsVoter::InstanceMethods
This module contains instance methods
Public Instance Methods
_votes_by()
click to toggle source
wraps the dynamic, configured, relationship name
# File lib/acts_as_voter.rb, line 34 def _votes_by self.send(FormalVote.configuration[:voter_relationship_name]) end
unvote_for(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 97 def unvote_for(voteable) Vote.where( :voter_id => self.id, :voter_type => self.class.base_class.name, :voteable_id => voteable.id, :voteable_type => voteable.class.base_class.name ).map(&:destroy) end
Also aliased as: clear_votes
vote(voteable, options = {})
click to toggle source
# File lib/acts_as_voter.rb, line 85 def vote(voteable, options = {}) raise ArgumentError, "you must specify :up or :down in order to vote" unless options[:direction] && [:up, :down].include?(options[:direction].to_sym) if options[:exclusive] self.unvote_for(voteable) end direction = (options[:direction].to_sym == :up) # create! does not return the created object v = Vote.new(:vote => direction, :voteable => voteable, :voter => self) v.save! v end
vote_against(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 73 def vote_against(voteable) self.vote(voteable, { :direction => :down, :exclusive => false }) end
vote_count(for_or_against = :all)
click to toggle source
Usage user.vote_count(:up) # All +1 votes
user.vote_count(:down) # All -1 votes user.vote_count() # All votes
# File lib/acts_as_voter.rb, line 42 def vote_count(for_or_against = :all) v = Vote.where(:voter_id => id).where(:voter_type => self.class.base_class.name) v = case for_or_against when :all then v when :up then v.where(:vote => true) when :down then v.where(:vote => false) end v.count end
vote_exclusively_against(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 81 def vote_exclusively_against(voteable) self.vote(voteable, { :direction => :down, :exclusive => true }) end
vote_exclusively_for(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 77 def vote_exclusively_for(voteable) self.vote(voteable, { :direction => :up, :exclusive => true }) end
vote_for(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 69 def vote_for(voteable) self.vote(voteable, { :direction => :up, :exclusive => false }) end
voted_against?(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 56 def voted_against?(voteable) voted_which_way?(voteable, :down) end
voted_for?(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 52 def voted_for?(voteable) voted_which_way?(voteable, :up) end
voted_how?(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 119 def voted_how?(voteable) votes = Vote.where( :voter_id => self.id, :voter_type => self.class.base_class.name, :voteable_id => voteable.id, :voteable_type => voteable.class.base_class.name ).map(&:vote) #in case votes is premitted to be duplicated if votes.count == 1 votes.first elsif votes.count == 0 nil else votes end end
voted_on?(voteable)
click to toggle source
# File lib/acts_as_voter.rb, line 60 def voted_on?(voteable) 0 < Vote.where( :voter_id => self.id, :voter_type => self.class.base_class.name, :voteable_id => voteable.id, :voteable_type => voteable.class.base_class.name ).count end
voted_which_way?(voteable, direction)
click to toggle source
# File lib/acts_as_voter.rb, line 108 def voted_which_way?(voteable, direction) raise ArgumentError, "expected :up or :down" unless [:up, :down].include?(direction) 0 < Vote.where( :voter_id => self.id, :voter_type => self.class.base_class.name, :vote => direction == :up ? true : false, :voteable_id => voteable.id, :voteable_type => voteable.class.base_class.name ).count end