Commit 8d8b82127f2563ee24a7621793b19c9c984c199e

Authored by Ariejan de Vroom
1 parent ed11ca13

Count +1 votes in issue notes.

app/models/issue.rb
... ... @@ -45,6 +45,11 @@ class Issue < ActiveRecord::Base
45 45 def new?
46 46 today? && created_at == updated_at
47 47 end
  48 +
  49 + # Return the number of +1 comments (upvotes)
  50 + def upvotes
  51 + notes.select(&:upvote?).size
  52 + end
48 53 end
49 54 # == Schema Information
50 55 #
... ...
app/models/note.rb
... ... @@ -47,27 +47,27 @@ class Note < ActiveRecord::Base
47 47 end
48 48  
49 49 def target
50   - if noteable_type == "Commit"
  50 + if noteable_type == "Commit"
51 51 project.commit(noteable_id)
52   - else
  52 + else
53 53 noteable
54 54 end
55 55 # Temp fix to prevent app crash
56 56 # if note commit id doesnt exist
57   - rescue
  57 + rescue
58 58 nil
59 59 end
60 60  
61 61 # Check if we can notify commit author
62 62 # with email about our comment
63 63 #
64   - # If commit author email exist in project
65   - # and commit author is not passed user we can
  64 + # If commit author email exist in project
  65 + # and commit author is not passed user we can
66 66 # send email to him
67 67 #
68 68 # params:
69 69 # user - current user
70   - #
  70 + #
71 71 # return:
72 72 # Boolean
73 73 #
... ... @@ -81,12 +81,18 @@ class Note < ActiveRecord::Base
81 81 end
82 82  
83 83 def commit_author
84   - @commit_author ||=
85   - project.users.find_by_email(target.author_email) ||
  84 + @commit_author ||=
  85 + project.users.find_by_email(target.author_email) ||
86 86 project.users.find_by_name(target.author_name)
87   - rescue
  87 + rescue
88 88 nil
89 89 end
  90 +
  91 + # Returns true if this is an upvote note,
  92 + # otherwise false is returned
  93 + def upvote?
  94 + note =~ /^\+1/ ? true : false
  95 + end
90 96 end
91 97 # == Schema Information
92 98 #
... ...
app/views/issues/_show.html.haml
... ... @@ -14,6 +14,8 @@
14 14 %span.label.important critical
15 15 - if issue.today?
16 16 %span.label.success today
  17 + - if issue.upvotes > 0
  18 + %span.label.success= "+#{issue.upvotes}"
17 19  
18 20  
19 21  
... ...
app/views/issues/show.html.haml
... ... @@ -35,6 +35,9 @@
35 35 %cite.cgray and currently assigned to
36 36 = image_tag gravatar_icon(@issue.assignee_email), :width => 16, :class => "lil_av"
37 37 %strong.author= link_to_issue_assignee(@issue)
  38 +
  39 + - if @issue.upvotes > 0
  40 + %span.label.success= "+#{@issue.upvotes}"
38 41  
39 42 %hr
40 43  
... ...
spec/models/issue_spec.rb
... ... @@ -24,6 +24,37 @@ describe Issue do
24 24 :assignee => Factory(:user),
25 25 :project => Factory.create(:project)).should be_valid }
26 26  
  27 + describe "plus 1" do
  28 + let(:project) { Factory(:project) }
  29 + subject {
  30 + Factory.create(:issue,
  31 + :author => Factory(:user),
  32 + :assignee => Factory(:user),
  33 + :project => project)
  34 + }
  35 +
  36 + it "with no notes has a 0/0 score" do
  37 + subject.upvotes.should == 0
  38 + end
  39 +
  40 + it "should recognize non-+1 notes" do
  41 + subject.notes << Factory(:note, note: "No +1 here", project: Factory(:project, path: 'plusone', code: 'plusone'))
  42 + subject.should have(1).note
  43 + subject.notes.first.upvote?.should be_false
  44 + subject.upvotes.should == 0
  45 + end
  46 +
  47 + it "should recognize a single +1 note" do
  48 + subject.notes << Factory(:note, note: "+1 This is awesome", project: Factory(:project, path: 'plusone', code: 'plusone'))
  49 + subject.upvotes.should == 1
  50 + end
  51 +
  52 + it "should recognize a multiple +1 notes" do
  53 + subject.notes << Factory(:note, note: "+1 This is awesome", project: Factory(:project, path: 'plusone', code: 'plusone'))
  54 + subject.notes << Factory(:note, note: "+1 I want this", project: Factory(:project, path: 'plustwo', code: 'plustwo'))
  55 + subject.upvotes.should == 2
  56 + end
  57 + end
27 58 end
28 59 # == Schema Information
29 60 #
... ...
spec/models/note_spec.rb
... ... @@ -20,10 +20,29 @@ describe Note do
20 20 Note.today.where_values.should == ["created_at >= '#{Date.today}'"]
21 21 end
22 22 end
23   -
24   - describe "Commit notes" do
25 23  
26   - before do
  24 + describe "Voting score" do
  25 + let(:project) { Factory(:project) }
  26 +
  27 + it "recognizes a neutral note" do
  28 + note = Factory(:note, project: project, note: "This is not a +1 note")
  29 + note.should_not be_upvote
  30 + end
  31 +
  32 + it "recognizes a +1 note" do
  33 + note = Factory(:note, project: project, note: "+1 for this")
  34 + note.should be_upvote
  35 + end
  36 +
  37 + it "recognizes a -1 note as no vote" do
  38 + note = Factory(:note, project: project, note: "-1 for this")
  39 + note.should_not be_upvote
  40 + end
  41 + end
  42 +
  43 + describe "Commit notes" do
  44 +
  45 + before do
27 46 @note = Factory :note,
28 47 :project => project,
29 48 :noteable_id => commit.id,
... ... @@ -36,12 +55,12 @@ describe Note do
36 55 end
37 56 end
38 57  
39   - describe "Pre-line commit notes" do
40   - before do
  58 + describe "Pre-line commit notes" do
  59 + before do
41 60 @note = Factory :note,
42 61 :project => project,
43 62 :noteable_id => commit.id,
44   - :noteable_type => "Commit",
  63 + :noteable_type => "Commit",
45 64 :line_code => "0_16_1"
46 65 end
47 66  
... ...