Module: Mongoid::Voteable::Voting::ClassMethods
- Defined in:
- lib/voteable_mongoid/voteable/voting.rb
Instance Method Summary (collapse)
-
- (votes, ...) vote(options)
Make a vote on an object of this class.
Instance Method Details
- (votes, ...) vote(options)
Make a vote on an object of this class
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/voteable_mongoid/voteable/voting.rb', line 18 def vote() .symbolize_keys! [:votee_id] = BSON::ObjectId([:votee_id]) if [:votee_id].is_a?(String) [:voter_id] = BSON::ObjectId([:voter_id]) if [:voter_id].is_a?(String) [:value] = [:value].to_sym [:voteable] = VOTEABLE[name][name] update_parents = [:voteable][:update_parents] if [:voteable] query, update = if [:revote] revote_query_n_update() elsif [:unvote] unvote_query_n_update() else new_vote_query_n_update() end if update_parents || [:votee] || [:return_votes] # If votee exits or need to update parent # use Collection#find_and_modify to retrieve updated votes data and parent_ids begin doc = collection.master.collection.find_and_modify( :query => query, :update => update, :new => true ) # Update new votes data votes = doc['votes'] [:votee].write_attribute('votes', votes) if [:votee] update_parent_votes(doc, ) if update_parents return votes rescue # Don't update parents if operation fail or no matching object found return false end else # Just update and don't care the result collection.update(query, update) end end end |