Module: FormalVote::ActsAsVoter::InstanceMethods

Defined in:
lib/acts_as_voter.rb

Overview

This module contains instance methods

Instance Method Summary collapse

Instance Method Details

#_votes_byObject

wraps the dynamic, configured, relationship name



34
35
36
# File 'lib/acts_as_voter.rb', line 34

def _votes_by
  self.send(FormalVote.configuration[:voter_relationship_name])
end

#unvote_for(voteable) ⇒ Object Also known as: clear_votes



97
98
99
100
101
102
103
104
# 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

#vote(voteable, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



73
74
75
# 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) ⇒ Object

Usage user.vote_count(:up) # All +1 votes

user.vote_count(:down) # All -1 votes
user.vote_count()      # All votes


42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



81
82
83
# 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) ⇒ Object



77
78
79
# File 'lib/acts_as_voter.rb', line 77

def vote_exclusively_for(voteable)
  self.vote(voteable, { :direction => :up, :exclusive => true })
end

#vote_for(voteable) ⇒ Object



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

def vote_for(voteable)
  self.vote(voteable, { :direction => :up, :exclusive => false })
end

#voted_against?(voteable) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/acts_as_voter.rb', line 56

def voted_against?(voteable)
  voted_which_way?(voteable, :down)
end

#voted_for?(voteable) ⇒ Boolean

Returns:

  • (Boolean)


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

def voted_for?(voteable)
  voted_which_way?(voteable, :up)
end

#voted_how?(voteable) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
115
116
117
# 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