Module: Mongo::Voteable::ClassMethods

Defined in:
lib/voteable_mongo/voteable.rb

Instance Method Summary collapse

Instance Method Details

#create_voteable_indexesObject



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

Parameters:

  • options (Hash)

    a hash containings:

    • :votee_id: the votee document id

    • :voter_id: the voter document id

Returns:

  • (true, false)


97
98
99
100
# File 'lib/voteable_mongo/voteable.rb', line 97

def down_voted?(options)
  validate_and_normalize_vote_options(options)
  down_voted_by(options[:voter_id]).where(:_id => options[:votee_id]).count == 1
end

#up_voted?(options) ⇒ true, false

Check if voter_id do an up vote on votee_id

Parameters:

  • options (Hash)

    a hash containings:

    • :votee_id: the votee document id

    • :voter_id: the voter document id

Returns:

  • (true, false)


85
86
87
88
# File 'lib/voteable_mongo/voteable.rb', line 85

def up_voted?(options)
  validate_and_normalize_vote_options(options)
  up_voted_by(options[:voter_id]).where(:_id => options[: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

Parameters:

  • options (Hash) (defaults to: nil)

    a hash containings:



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/voteable_mongo/voteable.rb', line 54

def voteable(klass = self, options = nil)
  VOTEABLE[name] ||= {}
  VOTEABLE[name][klass.name] ||= options
  if klass == self
    if options[: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

Parameters:

  • options (Hash)

    a hash containings:

    • :votee_id: the votee document id

    • :voter_id: the voter document id

Returns:

  • (true, false)


73
74
75
76
# File 'lib/voteable_mongo/voteable.rb', line 73

def voted?(options)
  validate_and_normalize_vote_options(options)
  up_voted?(options) || down_voted?(options)
end