Permalink
Browse files

Use InstanceMethods modules

  • Loading branch information...
1 parent 9e2ea3e commit 9a7bb5cbff7a8c7489702331b7f0695cdaaa9628 @tiendung tiendung committed Apr 23, 2011
Showing with 125 additions and 120 deletions.
  1. +54 −52 lib/voteable_mongoid/voteable.rb
  2. +71 −68 lib/voteable_mongoid/voter.rb
@@ -88,68 +88,70 @@ def down_voted?(options)
end
end
- # Make a vote on this votee
- #
- # @param [Hash] options a hash containings:
- # - :voter_id: the voter document id
- # - :value: vote :up or vote :down
- # - :revote: change from vote up to vote down
- # - :unvote: unvote the vote value (:up or :down)
- def vote(options)
- options[:votee_id] = id
- options[:votee] = self
- options[:voter_id] ||= options[:voter].id
+ module InstanceMethods
+ # Make a vote on this votee
+ #
+ # @param [Hash] options a hash containings:
+ # - :voter_id: the voter document id
+ # - :value: vote :up or vote :down
+ # - :revote: change from vote up to vote down
+ # - :unvote: unvote the vote value (:up or :down)
+ def vote(options)
+ options[:votee_id] = id
+ options[:votee] = self
+ options[:voter_id] ||= options[:voter].id
- if options[:unvote]
- options[:value] ||= vote_value(options[:voter_id])
- else
- options[:revote] ||= vote_value(options[:voter_id]).present?
- end
+ if options[:unvote]
+ options[:value] ||= vote_value(options[:voter_id])
+ else
+ options[:revote] ||= vote_value(options[:voter_id]).present?
+ end
- self.class.vote(options)
- end
+ self.class.vote(options)
+ end
- # Get a voted value on this votee
- #
- # @param [Mongoid Object, BSON::ObjectId] voter is Mongoid object or the id of the voter who made the vote
- def vote_value(voter)
- voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.id
- return :up if up_voter_ids.include?(voter_id)
- return :down if down_voter_ids.include?(voter_id)
- end
+ # Get a voted value on this votee
+ #
+ # @param [Mongoid Object, BSON::ObjectId] voter is Mongoid object or the id of the voter who made the vote
+ def vote_value(voter)
+ voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.id
+ return :up if up_voter_ids.include?(voter_id)
+ return :down if down_voter_ids.include?(voter_id)
+ end
- def voted_by?(voter)
- !!vote_value(voter)
- end
+ def voted_by?(voter)
+ !!vote_value(voter)
+ end
- # Array of up voter ids
- def up_voter_ids
- votes.try(:[], 'up') || []
- end
+ # Array of up voter ids
+ def up_voter_ids
+ votes.try(:[], 'up') || []
+ end
- # Array of down voter ids
- def down_voter_ids
- votes.try(:[], 'down') || []
- end
+ # Array of down voter ids
+ def down_voter_ids
+ votes.try(:[], 'down') || []
+ end
- # Get the number of up votes
- def up_votes_count
- votes.try(:[], 'up_count') || 0
- end
+ # Get the number of up votes
+ def up_votes_count
+ votes.try(:[], 'up_count') || 0
+ end
- # Get the number of down votes
- def down_votes_count
- votes.try(:[], 'down_count') || 0
- end
+ # Get the number of down votes
+ def down_votes_count
+ votes.try(:[], 'down_count') || 0
+ end
- # Get the number of votes
- def votes_count
- votes.try(:[], 'count') || 0
- end
+ # Get the number of votes
+ def votes_count
+ votes.try(:[], 'count') || 0
+ end
- # Get the votes point
- def votes_point
- votes.try(:[], 'point') || 0
+ # Get the votes point
+ def votes_point
+ votes.try(:[], 'point') || 0
+ end
end
end
@@ -1,86 +1,89 @@
module Mongoid
module Voter
extend ActiveSupport::Concern
-
- # Check to see if this voter voted on the votee or not
- #
- # @param [Hash, Object] options the hash containing the votee, or the votee itself
- # @return [true, false] true if voted, false otherwise
- def voted?(options)
- unless options.is_a?(Hash)
- votee_class = options.class
- votee_id = options.id
- else
- votee = options[:votee]
- if votee
- votee_class = votee.class
- votee_id = votee.id
+
+ module InstanceMethods
+ # Check to see if this voter voted on the votee or not
+ #
+ # @param [Hash, Object] options the hash containing the votee, or the votee itself
+ # @return [true, false] true if voted, false otherwise
+ def voted?(options)
+ unless options.is_a?(Hash)
+ votee_class = options.class
+ votee_id = options.id
else
- votee_class = options[:votee_type].classify.constantize
- votee_id = options[:votee_id]
+ votee = options[:votee]
+ if votee
+ votee_class = votee.class
+ votee_id = votee.id
+ else
+ votee_class = options[:votee_type].classify.constantize
+ votee_id = options[:votee_id]
+ end
end
- end
- votee_class.voted?(:voter_id => id, :votee_id => votee_id)
- end
+ votee_class.voted?(:voter_id => id, :votee_id => votee_id)
+ end
- # Get the voted value on a votee
- #
- # @param (see #voted?)
- # @return [Symbol, nil] :up or :down or nil if not voted
- def vote_value(options)
- votee = unless options.is_a?(Hash)
- options
- else
- options[:votee] || options[:votee_type].classify.constantize.only(:votes).where(
- :_id => options[:votee_id]
- ).first
+ # Get the voted value on a votee
+ #
+ # @param (see #voted?)
+ # @return [Symbol, nil] :up or :down or nil if not voted
+ def vote_value(options)
+ votee = unless options.is_a?(Hash)
+ options
+ else
+ options[:votee] || options[:votee_type].classify.constantize.only(:votes).where(
+ :_id => options[:votee_id]
+ ).first
+ end
+ votee.vote_value(_id)
end
- votee.vote_value(_id)
- end
- # Cancel the vote on a votee
- #
- # @param [Object] votee the votee to be unvoted
- def unvote(options)
- unless options.is_a?(Hash)
- options = { :votee => options }
+ # Cancel the vote on a votee
+ #
+ # @param [Object] votee the votee to be unvoted
+ def unvote(options)
+ unless options.is_a?(Hash)
+ options = { :votee => options }
+ end
+ options[:unvote] = true
+ options[:revote] = false
+ vote(options)
end
- options[:unvote] = true
- options[:revote] = false
- vote(options)
- end
- # Vote on a votee
- #
- # @param (see #voted?)
- # @param [:up, :down] vote_value vote up or vote down, nil to unvote
- def vote(options, value = nil)
- if options.is_a?(Hash)
- votee = options[:votee]
- else
- votee = options
- options = { :votee => votee, :value => value }
- end
+ # Vote on a votee
+ #
+ # @param (see #voted?)
+ # @param [:up, :down] vote_value vote up or vote down, nil to unvote
+ def vote(options, value = nil)
+ if options.is_a?(Hash)
+ votee = options[:votee]
+ else
+ votee = options
+ options = { :votee => votee, :value => value }
+ end
- if votee
- options[:votee_id] = votee.id
- votee_class = votee.class
- else
- votee_class = options[:votee_type].classify.constantize
- end
+ if votee
+ options[:votee_id] = votee.id
+ votee_class = votee.class
+ else
+ votee_class = options[:votee_type].classify.constantize
+ end
- if options[:value].nil?
- options[:unvote] = true
- options[:value] = vote_value(options)
- else
- options[:revote] = options.has_key?(:revote) ? !options[:revote].blank? : voted?(options)
- end
+ if options[:value].nil?
+ options[:unvote] = true
+ options[:value] = vote_value(options)
+ else
+ options[:revote] = options.has_key?(:revote) ? !options[:revote].blank? : voted?(options)
+ end
- options[:voter] = self
- options[:voter_id] = id
+ options[:voter] = self
+ options[:voter_id] = id
- ( votee || votee_class ).vote(options)
+ ( votee || votee_class ).vote(options)
+ end
end
+
end
end

0 comments on commit 9a7bb5c

Please sign in to comment.