Module: Mongoid::Voteable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/voteable.rb

Constant Summary

VOTE_POINT =

How many points should be assigned for each up or down vote. This hash should be accessed and manipulated using Voteable.vote_point method

{}

Instance Method Summary (collapse)

Instance Method Details

- (Object) down_voter_ids

Array of down voter ids



222
223
224
# File 'lib/mongoid/voteable.rb', line 222

def down_voter_ids
  voteable.try(:[], 'down_voter_ids') || []
end

- (Object) down_votes_count

Get the number of down votes



202
203
204
# File 'lib/mongoid/voteable.rb', line 202

def down_votes_count
  down_voter_ids.length
end

- (Object) up_voter_ids

Array of up voter ids



217
218
219
# File 'lib/mongoid/voteable.rb', line 217

def up_voter_ids
  voteable.try(:[], 'up_voter_ids') || []
end

- (Object) up_votes_count

Get the number of up votes



197
198
199
# File 'lib/mongoid/voteable.rb', line 197

def up_votes_count
  up_voter_ids.length
end

- (Object) vote(options)

Make a vote on this votee

Parameters:

  • (Hash) options

    a hash containings:

    • :voter_id: the voter document id

    • :value: vote :up or vote :down



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mongoid/voteable.rb', line 174

def vote(options)
  options[:votee_id] ||= _id
  options[:votee] ||= self

  if options[:unvote]
    options[:value] ||= vote_value(options[:voter_id])
  else
    options[:revote] ||= !vote_value(options[:voter_id]).nil?
  end

  self.class.vote(options)
end

- (Object) vote_value(voter)

Get a voted value on this votee

Parameters:

  • (Mongoid Object, BSON::ObjectId) voter

    is Mongoid object the id of the voter who made the vote



190
191
192
193
194
# File 'lib/mongoid/voteable.rb', line 190

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

- (Object) votes_count

Get the number of votes count



207
208
209
# File 'lib/mongoid/voteable.rb', line 207

def votes_count
  voteable.try(:[], 'votes_count') || 0
end

- (Object) votes_point

Get the votes point



212
213
214
# File 'lib/mongoid/voteable.rb', line 212

def votes_point
  voteable.try(:[], 'votes_point') || 0
end