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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/voteable_mongo/voteable.rb', line 102 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]] end |
#down_voted?(options) ⇒ true, false
Check if voter_id do a down vote on votee_id
97 98 99 100 |
# File 'lib/voteable_mongo/voteable.rb', line 97 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
85 86 87 88 |
# File 'lib/voteable_mongo/voteable.rb', line 85 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
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/voteable_mongo/voteable.rb', line 54 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
73 74 75 76 |
# File 'lib/voteable_mongo/voteable.rb', line 73 def voted?() () up_voted?() || down_voted?() end |