Module: Mongo::Voteable::ClassMethods
- Defined in:
- lib/voteable_mongo/voteable.rb
Instance Method Summary collapse
- #create_voteable_indexes ⇒ Object
-
#down_voted?(options) ⇒ true, false
Check if voter_id do a down vote on votee_id.
-
#up_voted?(options) ⇒ true, false
Check if voter_id do an up vote on votee_id.
-
#voteable(klass = self, options = nil) ⇒ Object
Set vote point for each up (down) vote on an object of this class.
-
#voted?(options) ⇒ true, false
Check if voter_id do a vote on votee_id.
Instance Method Details
#create_voteable_indexes ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# 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) ⇒ true, false
Check if voter_id do a down vote on votee_id
91 92 93 94 |
# File 'lib/voteable_mongo/voteable.rb', line 91 def down_voted?() () down_voted_by([:voter_id]).where(:_id => [:votee_id]).count == 1 end |
#up_voted?(options) ⇒ true, false
Check if voter_id do an up vote on votee_id
79 80 81 82 |
# File 'lib/voteable_mongo/voteable.rb', line 79 def up_voted?() () up_voted_by([:voter_id]).where(:_id => [:votee_id]).count == 1 end |
#voteable(klass = self, options = nil) ⇒ Object
Set vote point for each up (down) vote on an object of this class
voteable self, :up => 1, :down => -3 voteable Post, :up => 2, :down => -1, :update_counters => false # skip counter update
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/voteable_mongo/voteable.rb', line 48 def voteable(klass = self, = nil) VOTEABLE[name] ||= {} VOTEABLE[name][klass.name] ||= if klass == self if [:index] == true create_voteable_indexes end else VOTEABLE[name][name][:update_parents] ||= true end end |
#voted?(options) ⇒ true, false
Check if voter_id do a vote on votee_id
67 68 69 70 |
# File 'lib/voteable_mongo/voteable.rb', line 67 def voted?() () up_voted?() || down_voted?() end |