module Mongo::Voteable::ClassMethods
Public Instance Methods
create_voteable_indexes()
click to toggle source
# File lib/voteable_mongo/voteable.rb, line 96 def create_voteable_indexes # Compound index _id and voters.up, _id and voters.down # to make up_voted_by, down_voted_by, voted_by scopes and voting faster # Should run in background since it introduce new index value and # while waiting to build, the system can use _id for voting # http://www.mongodb.org/display/DOCS/Indexing+as+a+Background+Operation voteable_index({'votes.up' => 1, '_id' => 1}, {unique: true}) voteable_index({'votes.down' => 1, '_id' => 1}, {unique: true}) # Index counters and point for desc ordering voteable_index({'votes.up_count' => -1}) voteable_index({'votes.down_count' => -1}) voteable_index({'votes.count' => -1}) voteable_index({'votes.point' => -1}) create_indexes end
down_voted?(options)
click to toggle source
Check if voter_id do a down vote on votee_id
@param [Hash] options a hash containings:
- :votee_id: the votee document id - :voter_id: the voter document id
@return [true, false]
# File lib/voteable_mongo/voteable.rb, line 91 def down_voted?(options) validate_and_normalize_vote_options(options) down_voted_by(options[:voter_id]).where(:_id => options[:votee_id]).count == 1 end
up_voted?(options)
click to toggle source
Check if voter_id do an up vote on votee_id
@param [Hash] options a hash containings:
- :votee_id: the votee document id - :voter_id: the voter document id
@return [true, false]
# File lib/voteable_mongo/voteable.rb, line 79 def up_voted?(options) validate_and_normalize_vote_options(options) up_voted_by(options[:voter_id]).where(:_id => options[:votee_id]).count == 1 end
voteable(klass = self, options = nil)
click to toggle source
Set vote point for each up (down) vote on an object of this class
@param [Hash] options a hash containings:
voteable self, :up => +1, :down => -3 voteable Post, :up => +2, :down => -1, :update_counters => false # skip counter update
# File lib/voteable_mongo/voteable.rb, line 48 def voteable(klass = self, options = nil) VOTEABLE[name] ||= {} VOTEABLE[name][klass.name] ||= options if klass == self if options[:index] == true create_voteable_indexes end else VOTEABLE[name][name][:update_parents] ||= true end end
voted?(options)
click to toggle source
Check if voter_id do a vote on votee_id
@param [Hash] options a hash containings:
- :votee_id: the votee document id - :voter_id: the voter document id
@return [true, false]
# File lib/voteable_mongo/voteable.rb, line 67 def voted?(options) validate_and_normalize_vote_options(options) up_voted?(options) || down_voted?(options) end