Module: Mongoid::Utils::Votable

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

Defined Under Namespace

Modules: ClassMethods Classes: Vote

Instance Method Summary collapse

Instance Method Details

#vote!(value, voter) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/utils/votable.rb', line 32

def vote!(value, voter)
  vote = self.votes.find_or_create_by(user: voter)

  if vote.value == value
    vote.destroy
    value = 0
  end

  update_values(value, vote)
  vote.update_attribute(:value, value) if value != 0

  vote
end

#vote_down(voter) ⇒ Object



28
29
30
# File 'lib/mongoid/utils/votable.rb', line 28

def vote_down(voter)
  vote!(-1, voter)
end

#vote_up(voter) ⇒ Object



24
25
26
# File 'lib/mongoid/utils/votable.rb', line 24

def vote_up(voter)
  vote!(1, voter)
end

#voted?(voter) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mongoid/utils/votable.rb', line 20

def voted?(voter)
  votes.where(voter: voter).first != nil
end

#votes_countObject



46
47
48
# File 'lib/mongoid/utils/votable.rb', line 46

def votes_count
  self.votes.count
end