Commit f7c70eaaedd196accbe8e952ddc4738a96b81998
1 parent
a5164ea2
Exists in
master
and in
4 other branches
Add *votes_in_percent
Showing
2 changed files
with
66 additions
and
0 deletions
Show diff stats
app/roles/votes.rb
... | ... | @@ -4,11 +4,27 @@ module Votes |
4 | 4 | notes.select(&:upvote?).size |
5 | 5 | end |
6 | 6 | |
7 | + def upvotes_in_percent | |
8 | + if votes_count.zero? | |
9 | + 0 | |
10 | + else | |
11 | + 100.0 / votes_count * upvotes | |
12 | + end | |
13 | + end | |
14 | + | |
7 | 15 | # Return the number of -1 comments (downvotes) |
8 | 16 | def downvotes |
9 | 17 | notes.select(&:downvote?).size |
10 | 18 | end |
11 | 19 | |
20 | + def downvotes_in_percent | |
21 | + if votes_count.zero? | |
22 | + 0 | |
23 | + else | |
24 | + 100.0 - upvotes_in_percent | |
25 | + end | |
26 | + end | |
27 | + | |
12 | 28 | # Return the total number of votes |
13 | 29 | def votes_count |
14 | 30 | upvotes + downvotes | ... | ... |
spec/roles/votes_spec.rb
... | ... | @@ -79,4 +79,54 @@ describe Issue do |
79 | 79 | issue.votes_count.should == 3 |
80 | 80 | end |
81 | 81 | end |
82 | + | |
83 | + describe "#upvotes_in_percent" do | |
84 | + it "with no notes has a 0% score" do | |
85 | + issue.upvotes_in_percent.should == 0 | |
86 | + end | |
87 | + | |
88 | + it "should count a single 1 note as 100%" do | |
89 | + issue.notes << create(:note, note: "+1 This is awesome") | |
90 | + issue.upvotes_in_percent.should == 100 | |
91 | + end | |
92 | + | |
93 | + it "should count multiple +1 notes as 100%" do | |
94 | + issue.notes << create(:note, note: "+1 This is awesome") | |
95 | + issue.notes << create(:note, note: "+1 I want this") | |
96 | + issue.upvotes_in_percent.should == 100 | |
97 | + end | |
98 | + | |
99 | + it "should count fractions for multiple +1 and -1 notes correctly" do | |
100 | + issue.notes << create(:note, note: "+1 This is awesome") | |
101 | + issue.notes << create(:note, note: "+1 I want this") | |
102 | + issue.notes << create(:note, note: "-1 This is bad") | |
103 | + issue.notes << create(:note, note: "+1 me too") | |
104 | + issue.upvotes_in_percent.should == 75 | |
105 | + end | |
106 | + end | |
107 | + | |
108 | + describe "#downvotes_in_percent" do | |
109 | + it "with no notes has a 0% score" do | |
110 | + issue.downvotes_in_percent.should == 0 | |
111 | + end | |
112 | + | |
113 | + it "should count a single -1 note as 100%" do | |
114 | + issue.notes << create(:note, note: "-1 This is bad") | |
115 | + issue.downvotes_in_percent.should == 100 | |
116 | + end | |
117 | + | |
118 | + it "should count multiple -1 notes as 100%" do | |
119 | + issue.notes << create(:note, note: "-1 This is bad") | |
120 | + issue.notes << create(:note, note: "-1 Away with this") | |
121 | + issue.downvotes_in_percent.should == 100 | |
122 | + end | |
123 | + | |
124 | + it "should count fractions for multiple +1 and -1 notes correctly" do | |
125 | + issue.notes << create(:note, note: "+1 This is awesome") | |
126 | + issue.notes << create(:note, note: "+1 I want this") | |
127 | + issue.notes << create(:note, note: "-1 This is bad") | |
128 | + issue.notes << create(:note, note: "+1 me too") | |
129 | + issue.downvotes_in_percent.should == 25 | |
130 | + end | |
131 | + end | |
82 | 132 | end | ... | ... |