Module: Mongoid::Voter

Extended by:
ActiveSupport::Concern
Included in:
Ish::UserProfile
Defined in:
lib/mongoid/voter.rb

Instance Method Summary collapse

Instance Method Details

#unvote(options) ⇒ Object

Cancel the vote on a votee

Parameters:

  • votee (Object)

    the votee to be unvoted



56
57
58
59
60
61
62
63
# File 'lib/mongoid/voter.rb', line 56

def unvote(options)
  unless options.is_a?(Hash)
    options = { :votee => options }
  end
  options[:unvote] = true
  options[:revote] = false
  vote(options)
end

#vote(options, value = nil) ⇒ Object

Vote on a votee

Parameters:

  • vote_value (:up, :down)

    vote up or vote down, nil to unvote

  • options (Hash, Object)

    the hash containing the votee, or the votee itself



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mongoid/voter.rb', line 69

def vote(options, value = nil)
  if options.is_a?(Hash)
    votee = options[:votee]
  else
    votee = options
    options = { :votee => votee, :value => value }
  end

  if votee
    options[:votee_id] = votee._id
    votee_class = votee.class
  else
    votee_class = options[:votee_type].classify.constantize
  end

  if options[:value].nil?
    options[:unvote] = true
    options[:value] = vote_value(options)
  else
    options[:revote] = options.has_key?(:revote) ? !options[:revote].blank? : voted?(options)
  end

  options[:voter_id] = _id

  ( votee || votee_class ).vote(options)
end

#vote_value(options) ⇒ Symbol?

Get the voted value on a votee

Parameters:

  • options (Hash, Object)

    the hash containing the votee, or the votee itself

Returns:

  • (Symbol, nil)

    :up or :down or nil if not voted



42
43
44
45
46
47
48
49
50
51
# File 'lib/mongoid/voter.rb', line 42

def vote_value(options)
  votee = unless options.is_a?(Hash)
    options
  else
    options[:votee] || options[:votee_type].classify.constantize.only(:up_vote_ids, :down_vote_ids).where(
      :_id => options[:votee_id]
    ).first
  end
  votee.vote_value(_id)
end

#voted?(options) ⇒ true, false

Check to see if this voter voted on the votee or not

Parameters:

  • options (Hash, Object)

    the hash containing the votee, or the votee itself

Returns:

  • (true, false)

    true if voted, false otherwise



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongoid/voter.rb', line 20

def voted?(options)
  unless options.is_a?(Hash)
    votee_class = options.class
    votee_id = options._id
  else
    votee = options[:votee]
    if votee
      votee_class = votee.class
      votee_id = votee._id
    else
      votee_class = options[:votee_type].classify.constantize
      votee_id = options[:votee_id]
    end
  end

  votees(votee_class).where(:_id => votee_id).count == 1
end

#votees(klass) ⇒ Array?

Get list of voted votees

Parameters:

  • klass (Class)

    the votee class, e.g. ‘Post` or `Comment`

Returns:

  • (Array, nil)

    an array of votable objects voted by this voter



9
10
11
12
13
14
# File 'lib/mongoid/voter.rb', line 9

def votees(klass)
  # Is this id, or id.to_s ?
  # let's not convert to string, lets use bson::objectid.
  # _vp_ 2022-08-31
  klass.any_of({ "votable.up_voter_ids" => _id }, { "votable.down_voter_ids" => _id })
end