Module: Mongoid::Voter
Instance Method Summary collapse
-
#unvote(options) ⇒ Object
Cancel the vote on a votee.
-
#vote(options, value = nil) ⇒ Object
Vote on a votee.
-
#vote_value(options) ⇒ Symbol?
Get the voted value on a votee.
-
#voted?(options) ⇒ true, false
Check to see if this voter voted on the votee or not.
-
#votees(klass) ⇒ Array?
Get list of voted votees.
Instance Method Details
#unvote(options) ⇒ Object
Cancel the vote on a votee
56 57 58 59 60 61 62 63 |
# File 'lib/mongoid/voter.rb', line 56 def unvote() unless .is_a?(Hash) = { :votee => } end [:unvote] = true [:revote] = false vote() end |
#vote(options, value = nil) ⇒ Object
Vote on a votee
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mongoid/voter.rb', line 69 def vote(, value = nil) if .is_a?(Hash) votee = [:votee] else votee = = { :votee => votee, :value => value } end if votee [:votee_id] = votee._id votee_class = votee.class else votee_class = [:votee_type].classify.constantize end if [:value].nil? [:unvote] = true [:value] = vote_value() else [:revote] = .has_key?(:revote) ? ![:revote].blank? : voted?() end [:voter_id] = _id ( votee || votee_class ).vote() end |
#vote_value(options) ⇒ Symbol?
Get the voted value on a votee
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongoid/voter.rb', line 42 def vote_value() votee = unless .is_a?(Hash) else [:votee] || [:votee_type].classify.constantize.only(:up_vote_ids, :down_vote_ids).where( :_id => [:votee_id] ).first end votee.vote_value(_id) end |
#voted?(options) ⇒ true, false
Check to see if this voter voted on the votee or not
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mongoid/voter.rb', line 20 def voted?() unless .is_a?(Hash) votee_class = .class votee_id = ._id else votee = [:votee] if votee votee_class = votee.class votee_id = votee._id else votee_class = [:votee_type].classify.constantize votee_id = [:votee_id] end end votees(votee_class).where(:_id => votee_id).count == 1 end |
#votees(klass) ⇒ Array?
Get list of voted votees
9 10 11 12 13 14 |
# File 'lib/mongoid/voter.rb', line 9 def votees(klass) # Is this id, or id.to_s ? # let's not convert to string, lets use bson::objectid. # _vp_ 2022-08-31 klass.any_of({ "votable.up_voter_ids" => _id }, { "votable.down_voter_ids" => _id }) end |