diff options
Diffstat (limited to 'lib/votes.rb')
-rw-r--r-- | lib/votes.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/votes.rb b/lib/votes.rb new file mode 100644 index 00000000000..dfd751b1b11 --- /dev/null +++ b/lib/votes.rb @@ -0,0 +1,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 |