Module: Mongoid::Voteable::InstanceMethods

Defined in:
lib/mongoid_voteable.rb

Instance Method Summary collapse

Instance Method Details

#vote(num, voter) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/mongoid_voteable.rb', line 15

def vote(num, voter)

  unless voted? voter
    self.votes += num.to_i
    self.voters << voter._id
    collection.update(  { "_id" => _id, "voters" => { "$ne" => voter._id } },
                        { "$inc" => { "votes" => num.to_i }, "$push" => { "voters" => voter._id } } )
  end

end

#vote_averageObject



34
35
36
37
38
39
40
# File 'lib/mongoid_voteable.rb', line 34

def vote_average
  if voters.blank?
    nil
  else
    votes.to_f / voters.count
  end
end

#vote_countObject



30
31
32
# File 'lib/mongoid_voteable.rb', line 30

def vote_count
  voters.count
end

#voted?(voter) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/mongoid_voteable.rb', line 26

def voted?(voter)
  voters.include? voter._id
end