Class: Voterable::Voter
- Inherits:
-
Object
- Object
- Voterable::Voter
- Includes:
- Mongoid::Document
- Defined in:
- lib/voterable/voter.rb
Instance Method Summary collapse
-
#calculate_reputation ⇒ Object
Recalculates user’s reputation.
-
#vote(voteable, value) ⇒ Object
Vote on voteable thing.
-
#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.
-
#vote_for(vtable) ⇒ Vote
Vote for specified voteable.
Instance Method Details
#calculate_reputation ⇒ Object
Recalculates user’s 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.(: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
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 |