lib/voteable_mongo/voteable.rb in voteable_mongo-0.8.1 vs lib/voteable_mongo/voteable.rb in voteable_mongo-0.9.0

- old
+ new

@@ -1,38 +1,47 @@ -require 'voteable_mongo/voteable/votes' -require 'voteable_mongo/voteable/voting' +require 'voteable_mongo/voting' +require 'voteable_mongo/integrations/mongoid' +require 'voteable_mongo/integrations/mongo_mapper' module Mongo module Voteable extend ActiveSupport::Concern + DEFAULT_VOTES = { + 'up' => [], + 'down' => [], + 'up_count' => 0, + 'down_count' => 0, + 'count' => 0, + 'point' => 0 + } + included do include ::Mongo::Voteable::Voting - - field :votes, :type => Votes - before_create do - # Init votes so that counters and point have numeric values (0) - self.votes = Votes::DEFAULT_ATTRIBUTES + if defined?(Mongoid) && defined?(field) + include ::Mongo::Voteable::Integrations::Mongoid + elsif defined?(MongoMapper) + include ::Mongo::Voteable::Integrations::MongoMapper end scope :voted_by, lambda { |voter| - voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.id - any_of({ 'votes.up' => voter_id }, { 'votes.down' => voter_id }) + voter_id = voter.is_a?(::BSON::ObjectId) ? voter : voter.id + where('$or' => [{ 'votes.up' => voter_id }, { 'votes.down' => voter_id }]) } - + scope :up_voted_by, lambda { |voter| - voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.id - where( 'votes.up' => voter_id ) + voter_id = voter.is_a?(::BSON::ObjectId) ? voter : voter.id + where('votes.up' => voter_id) } - + scope :down_voted_by, lambda { |voter| - voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.id - where( 'votes.down' => voter_id ) + voter_id = voter.is_a?(::BSON::ObjectId) ? voter : voter.id + where('votes.down' => voter_id) } - end # include - + end + # How many points should be assigned for each up or down vote and other options # This hash should manipulated using voteable method VOTEABLE = {} module ClassMethods @@ -87,27 +96,24 @@ # @return [true, false] def down_voted?(options) validate_and_normalize_vote_options(options) down_voted_by(options[:voter_id]).where(:_id => options[:votee_id]).count == 1 end - - private + def create_voteable_indexes - class_eval do - # 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 - index [['votes.up', 1], ['_id', 1]], :unique => true - index [['votes.down', 1], ['_id', 1]], :unique => true + # 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 - index [['votes.up_count', -1]] - index [['votes.down_count', -1]] - index [['votes.count', -1]] - index [['votes.point', -1]] - end + # 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 end module InstanceMethods # Make a vote on this votee