Class: Voterable::Voter

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
lib/voterable/voter.rb

Instance Method Summary collapse

Instance Method Details

#calculate_reputationObject

Recalculates user’s reputation

Examples:

recalculating a current_user’s reputation

current_user.calculate_reputation


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/voterable/voter.rb', line 59

def calculate_reputation
   #Contributed things
   sum = 0
   
   #Things that got votes
   self.voteables.each do |t| 
      sum+=t.class.options(:init)  # Voteable initial value
      sum+=t.point
   end

   #Vote Back
   self.votes.each do |v|
      sum += v.voteable.class.vtback(v.vote)
   end
   self.reputation = sum
   self.save
   # sum = sum > 0 ? sum : 0
end

#vote(voteable, value) ⇒ Object

Vote on voteable thing

Examples:

Current user voting on an emoticon

current_user.vote(emoticon, :up)

Parameters:

  • voteable (Voterable::Voteable)

    The thing that is being voted on

  • value (Symbol)

    The vote value either :up or :down



25
26
27
# File 'lib/voterable/voter.rb', line 25

def vote(voteable, value)
   voteable.vote(self, value)
end

#vote_count(period = [0, 1.days_in_seconds]) ⇒ Object

Retuns the number of votes cast by user @example:

a_voter.vote_count([1.days_in_seconds, 5.days_in_seconds])
10

Arguments:

period: (array) time between which votes are counted, going backwards from now


47
48
49
50
51
52
# File 'lib/voterable/voter.rb', line 47

def vote_count(period = [0, 1.days_in_seconds])
   time_1 = Time.now - period[1]
   time_2 = Time.now - period[0]

   self.votes.where(:updated_at.lte => time_2).and(:updated_at.gte => time_1).count
end

#vote_for(vtable) ⇒ Vote

Vote for specified voteable

Examples:

getting a vote for a voteable

Voter.vote_for(voteable).vote # => :up

Returns:

  • (Vote)

    vote that voter cast



35
36
37
# File 'lib/voterable/voter.rb', line 35

def vote_for(vtable)
   Vote.first(conditions: { voter_id: self.id, voteable_id: vtable.id })
end