Module: PeteOnRails::Acts::Voter::InstanceMethods

Defined in:
lib/acts_as_voter.rb

Overview

This module contains instance methods

Instance Method Summary collapse

Instance Method Details

#vote(voteable, vote) ⇒ Object



67
68
69
70
# File 'lib/acts_as_voter.rb', line 67

def vote(voteable, vote)
  vote = Vote.new(:vote => vote, :voteable => voteable, :voter => self)
  vote.save
end

#vote_against(voteable) ⇒ Object



63
64
65
# File 'lib/acts_as_voter.rb', line 63

def vote_against(voteable)
  self.vote(voteable, false)
end

#vote_count(for_or_against = "all") ⇒ Object

Usage user.vote_count(true) # All +1 votes

user.vote_count(false) # All -1 votes
user.vote_count()      # All votes


29
30
31
32
33
34
35
36
# File 'lib/acts_as_voter.rb', line 29

def vote_count(for_or_against = "all")
  where = (for_or_against == "all") ? 
    ["voter_id = ? AND voter_type = ?", id, self.class.name ] : 
    ["voter_id = ? AND voter_type = ? AND vote = ?", id, self.class.name, for_or_against ]
                
  Vote.count(:all, :conditions => where)

end

#vote_for(voteable) ⇒ Object



59
60
61
# File 'lib/acts_as_voter.rb', line 59

def vote_for(voteable)
  self.vote(voteable, true)
end

#voted_against?(voteable) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/acts_as_voter.rb', line 45

def voted_against?(voteable)
  0 < Vote.count(:all, :conditions => [
          "voter_id = ? AND voter_type = ? AND vote = ? AND voteable_id = ? AND voteable_type = ?",
          self.id, self.class.name, false, voteable.id, voteable.class.name
          ])
end

#voted_for?(voteable) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/acts_as_voter.rb', line 38

def voted_for?(voteable)
  0 < Vote.count(:all, :conditions => [
          "voter_id = ? AND voter_type = ? AND vote = ? AND voteable_id = ? AND voteable_type = ?",
          self.id, self.class.name, true, voteable.id, voteable.class.name
          ])
end

#voted_on?(voteable) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/acts_as_voter.rb', line 52

def voted_on?(voteable)
  0 < Vote.count(:all, :conditions => [
          "voter_id = ? AND voter_type = ? AND voteable_id = ? AND voteable_type = ?",
          self.id, self.class.name, voteable.id, voteable.class.name
          ])
end