module Mongo::Voteable

Constants

DEFAULT_VOTES
VOTEABLE

How many points should be assigned for each up or down vote and other options This hash should manipulated using voteable method

Public Instance Methods

down_voter_ids() click to toggle source

Array of down voter ids

# File lib/voteable_mongo/voteable.rb, line 154
def down_voter_ids
  votes.try(:[], 'down') || []
end
down_voters(klass) click to toggle source

Get down voters

# File lib/voteable_mongo/voteable.rb, line 189
def down_voters(klass)
  klass.where(:_id => { '$in' => down_voter_ids })
end
down_votes_count() click to toggle source

Get the number of down votes

# File lib/voteable_mongo/voteable.rb, line 169
def down_votes_count
  votes.try(:[], 'down_count') || 0
end
up_voter_ids() click to toggle source

Array of up voter ids

# File lib/voteable_mongo/voteable.rb, line 149
def up_voter_ids
  votes.try(:[], 'up') || []
end
up_voters(klass) click to toggle source

Get up voters

# File lib/voteable_mongo/voteable.rb, line 184
def up_voters(klass)
  klass.where(:_id => { '$in' =>  up_voter_ids })
end
up_votes_count() click to toggle source

Get the number of up votes

# File lib/voteable_mongo/voteable.rb, line 164
def up_votes_count
  votes.try(:[], 'up_count') || 0
end
vote(options) click to toggle source

Make a vote on this votee

@param [Hash] options a hash containings:

- :voter_id: the voter document id
- :value: vote :up or vote :down
- :revote: change from vote up to vote down
- :unvote: unvote the vote value (:up or :down)
# File lib/voteable_mongo/voteable.rb, line 121
def vote(options)
  options[:votee_id] = id
  options[:votee] = self
  options[:voter_id] ||= options[:voter].id

  if options[:unvote]
    options[:value] ||= vote_value(options[:voter_id])
  else
    options[:revote] ||= vote_value(options[:voter_id]).present?
  end

  self.class.vote(options)
end
vote_value(voter) click to toggle source

Get a voted value on this votee

@param voter is object or the id of the voter who made the vote

# File lib/voteable_mongo/voteable.rb, line 138
def vote_value(voter)
  voter_id = Helpers.get_mongo_id(voter)
  return :up if up_voter_ids.include?(voter_id)
  return :down if down_voter_ids.include?(voter_id)
end
voted_by?(voter) click to toggle source
# File lib/voteable_mongo/voteable.rb, line 144
def voted_by?(voter)
  !!vote_value(voter)
end
voter_ids() click to toggle source

Array of voter ids

# File lib/voteable_mongo/voteable.rb, line 159
def voter_ids
  up_voter_ids + down_voter_ids
end
voters(klass) click to toggle source

Get voters

# File lib/voteable_mongo/voteable.rb, line 194
def voters(klass)
  klass.where(:_id => { '$in' => voter_ids })
end
votes_count() click to toggle source

Get the number of votes

# File lib/voteable_mongo/voteable.rb, line 174
def votes_count
  votes.try(:[], 'count') || 0
end
votes_point() click to toggle source

Get the votes point

# File lib/voteable_mongo/voteable.rb, line 179
def votes_point
  votes.try(:[], 'point') || 0
end