Module: Mongo::Voteable::ClassMethods

Defined in:
lib/voteable_mongo/voteable.rb

Instance Method Summary collapse

Instance Method Details

#create_voteable_indexesObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/voteable_mongo/voteable.rb', line 96

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})
  create_indexes
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)


91
92
93
94
# File 'lib/voteable_mongo/voteable.rb', line 91

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)


79
80
81
82
# File 'lib/voteable_mongo/voteable.rb', line 79

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:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/voteable_mongo/voteable.rb', line 48

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)


67
68
69
70
# File 'lib/voteable_mongo/voteable.rb', line 67

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