summaryrefslogtreecommitdiff
path: root/app/roles/votes.rb
blob: dfd751b1b115991fa6cb3b222169a24884fb75b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# == Votes role
#
# Provides functionality to upvote/downvote entity
# based on +1 and -1 notes
#
# Used for Issue and Merge Request
#
module Votes
  # Return the number of +1 comments (upvotes)
  def upvotes
    notes.select(&:upvote?).size
  end

  def upvotes_in_percent
    if votes_count.zero?
      0
    else
      100.0 / votes_count * upvotes
    end
  end

  # Return the number of -1 comments (downvotes)
  def downvotes
    notes.select(&:downvote?).size
  end

  def downvotes_in_percent
    if votes_count.zero?
      0
    else
      100.0 - upvotes_in_percent
    end
  end

  # Return the total number of votes
  def votes_count
    upvotes + downvotes
  end
end