Module: Mongoid::Votable

Extended by:
ActiveSupport::Concern
Included in:
Newsitem, Video
Defined in:
lib/mongoid/votable.rb

Constant Summary collapse

VOTE_POINT =

How many points should be assigned for each up or down vote. This array should be manipulated using Votable.vote_point method

{}

Instance Method Summary collapse

Instance Method Details

#down_voter_idsObject

Array of down voter ids



239
240
241
242
243
244
245
# File 'lib/mongoid/votable.rb', line 239

def down_voter_ids
  if self.has_attribute?(:votable)
    self["votable"]["down_voter_ids"]
  else
    []
  end
end

#down_votes_countObject

Get the number of down votes



206
207
208
# File 'lib/mongoid/votable.rb', line 206

def down_votes_count
  down_voter_ids.length
end

#up_voter_idsObject

Array of up voter ids



230
231
232
233
234
235
236
# File 'lib/mongoid/votable.rb', line 230

def up_voter_ids
  if self.has_attribute?(:votable)
    self['votable']["up_voter_ids"]
  else
    []
  end
end

#up_votes_countObject

Get the number of up votes



201
202
203
# File 'lib/mongoid/votable.rb', line 201

def up_votes_count
  up_voter_ids.length
end

#vote(options) ⇒ Object

Make a vote on this votee

Parameters:

  • options (Hash)

    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/votable.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

#vote_value(voter) ⇒ Object

Get a voted value on this votee

Let’s NOT CONVERT bson::objectid’s to strings. vp 2022-08-31

Parameters:

  • voter (Mongoid Object, BSON::ObjectId)

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



192
193
194
195
196
197
198
# File 'lib/mongoid/votable.rb', line 192

def vote_value(voter)
  ## The last last one, voter.id not voter.id.to_s _vp_ 2022-09-19
  voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.is_a?(String) ? voter : voter.id

  return :up if up_voter_ids.try(:include?, voter_id)
  return :down if down_voter_ids.try(:include?, voter_id)
end

#votes_countObject

Get the number of votes count



211
212
213
214
215
216
217
# File 'lib/mongoid/votable.rb', line 211

def votes_count
  if self.has_attribute?(:votable)
    self["votable"]["votes_count"]
  else
    0
  end
end

#votes_pointObject Also known as: votes_score

Get the votes point



220
221
222
223
224
225
226
# File 'lib/mongoid/votable.rb', line 220

def votes_point
  if self.has_attribute?(:votable)
    self["votable"]["votes_point"]
  else
    0
  end
end