module Mongoid::Votable
Constants
- VERSION
Public Instance Methods
vote!(value, voter) { |0, :destroyed| ... }
click to toggle source
# File lib/mongoid/votable.rb, line 24 def vote!(value, voter) vote = votes.where(voter: voter).first if vote new_value = vote.value + value if new_value == 0 votes.delete(vote) do_vote!(vote, value, :destroyed) if block_given? yield 0, :destroyed else return :destroyed end else vote.inc(:value, value) do_vote!(vote, value, :updated) if block_given? yield vote.value, :updated else return :updated end end else votes.create(voter: voter, value: value) do_vote!(vote, value, :created) if block_given? yield value, :created else return :created end end end
voted?(voter)
click to toggle source
# File lib/mongoid/votable.rb, line 20 def voted?(voter) votes.where(voter: voter).first != nil end
Private Instance Methods
do_vote!(vote, value, status)
click to toggle source
# File lib/mongoid/votable.rb, line 57 def do_vote!(vote, value, status) case status when :created if value > 0 inc(:votes_up, 1) else inc(:votes_down, 1) end inc(:votes_count, 1) when :updated #Origin is vote down, but now is vote up if vote.value - value < 0 && vote.value > 0 inc(:votes_up, 1) inc(:votes_down, -1) #Origin is vote up, but now is vote down elsif vote.value - value > 0 && vote.value < 0 inc(:votes_down, 1) inc(:votes_up, -1) end when :destroyed if value > 0 inc(:votes_down, -1) else inc(:votes_up, -1) end inc(:votes_count, -1) else raise "Not accept status" end inc(:votes_average, value) #set(:votes_average, votes.map(&:value).reduce(0, &:+)) end