Module: VoterLove::Voter

Extended by:
ActiveSupport::Concern
Defined in:
lib/voter_love/voter.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#down_vote(votable) ⇒ Object

Down vote a “votable” object.

Raises an AlreadyVotedError if the voter already voted on the object.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/voter_love/voter.rb', line 59

def down_vote(votable)
  is_votable?(votable)
  
  vote = get_vote(votable)
  
  if vote
    unless vote.up_vote
      raise Exceptions::AlreadyVotedError.new(false)
    else
      vote.up_vote = false
      votable.up_votes -= 1
      self.up_votes -= 1 if has_attribute?(:up_votes)
    end
  else
    vote = Vote.create(:votable => votable, :voter => self, :up_vote => false)     
  end
  
  votable.down_votes += 1
  self.down_votes += 1 if has_attribute?(:down_votes)
  
  Vote.transaction do
    save
    votable.save
    vote.save
  end
  
  true
end

#down_vote!(votable) ⇒ Object

Down vote a “votable” object without raising an error. Vote is ignored.



89
90
91
92
93
94
95
96
97
# File 'lib/voter_love/voter.rb', line 89

def down_vote!(votable)
  begin
    down_vote(votable)
    success = true
  rescue Exceptions::AlreadyVotedError
    success = false
  end
  success
end

#down_voted?(votable) ⇒ Boolean

Returns true if the voter down voted the “votable”.

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'lib/voter_love/voter.rb', line 116

def down_voted?(votable)
  is_votable?(votable)
  vote = get_vote(votable)
  return false if vote.nil?
  return true if vote.has_attribute?(:up_vote) && !vote.up_vote
  false
end

#up_vote(votable) ⇒ Object

Up vote any “votable” object. Raises an AlreadyVotedError if the voter already voted on the object.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/voter_love/voter.rb', line 17

def up_vote(votable)
  is_votable?(votable)
  
  vote = get_vote(votable)
  
  if vote
    if vote.up_vote
      raise Exceptions::AlreadyVotedError.new(true)
    else
      vote.up_vote = true
      votable.down_votes -= 1
      self.down_votes -= 1 if has_attribute?(:down_votes)
    end
  else
    vote = Vote.create(:votable => votable, :voter => self, :up_vote => true)
  end
  
  votable.up_votes += 1
  self.up_votes += 1 if has_attribute?(:up_votes)  
  
  Vote.transaction do
    save
    votable.save
    vote.save
  end
  
  true
end

#up_vote!(votable) ⇒ Object

Up vote any “votable” object without raising an error. Vote is ignored.



47
48
49
50
51
52
53
54
55
# File 'lib/voter_love/voter.rb', line 47

def up_vote!(votable)
  begin
    up_vote(votable)
    success = true
  rescue Exceptions::AlreadyVotedError
    success = false
  end
  success
end

#up_voted?(votable) ⇒ Boolean

Returns true if the voter up voted the “votable”.

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/voter_love/voter.rb', line 107

def up_voted?(votable)
  is_votable?(votable)
  vote = get_vote(votable)
  return false if vote.nil?
  return true if vote.has_attribute?(:up_vote) && vote.up_vote
  false
end

#voted?(votable) ⇒ Boolean

Returns true if the voter voted for the “votable”.

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/voter_love/voter.rb', line 100

def voted?(votable)
  is_votable?(votable)
  vote = get_vote(votable)
  !vote.nil?
end