Commit fc1c250d404902c9b34ccd28d9cfea7108f80831

Authored by Riyad Preukschas
1 parent 63dac843

Reorder Note methods and add helpers

Showing 1 changed file with 58 additions and 27 deletions   Show diff stats
app/models/note.rb
@@ -18,7 +18,6 @@ require 'carrierwave/orm/activerecord' @@ -18,7 +18,6 @@ require 'carrierwave/orm/activerecord'
18 require 'file_size_validator' 18 require 'file_size_validator'
19 19
20 class Note < ActiveRecord::Base 20 class Note < ActiveRecord::Base
21 -  
22 attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id, 21 attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id,
23 :attachment, :line_code 22 :attachment, :line_code
24 23
@@ -55,12 +54,58 @@ class Note &lt; ActiveRecord::Base @@ -55,12 +54,58 @@ class Note &lt; ActiveRecord::Base
55 }, without_protection: true) 54 }, without_protection: true)
56 end 55 end
57 56
58 - def notify  
59 - @notify ||= false 57 + def commit_author
  58 + @commit_author ||=
  59 + project.users.find_by_email(noteable.author_email) ||
  60 + project.users.find_by_name(noteable.author_name)
  61 + rescue
  62 + nil
60 end 63 end
61 64
62 - def notify_author  
63 - @notify_author ||= false 65 + def diff
  66 + noteable.diffs[diff_file_index]
  67 + end
  68 +
  69 + def diff_file_index
  70 + line_code.split('_')[0].to_i
  71 + end
  72 +
  73 + def diff_file_name
  74 + diff.b_path
  75 + end
  76 +
  77 + def diff_new_line
  78 + line_code.split('_')[2].to_i
  79 + end
  80 +
  81 + def discussion_id
  82 + @discussion_id ||= [noteable_type, noteable_id, line_code].join.underscore.to_sym
  83 + end
  84 +
  85 + # Returns true if this is a downvote note,
  86 + # otherwise false is returned
  87 + def downvote?
  88 + note.start_with?('-1') || note.start_with?(':-1:')
  89 + end
  90 +
  91 + def for_commit?
  92 + noteable_type == "Commit"
  93 + end
  94 +
  95 + def for_commit_diff_line?
  96 + for_commit? && for_diff_line?
  97 + end
  98 +
  99 + def for_diff_line?
  100 + line_code.present?
  101 + end
  102 +
  103 + def for_merge_request?
  104 + noteable_type == "MergeRequest"
  105 + end
  106 +
  107 + def for_merge_request_diff_line?
  108 + for_merge_request? && for_diff_line?
64 end 109 end
65 110
66 # override to return commits, which are not active record 111 # override to return commits, which are not active record
@@ -76,6 +121,14 @@ class Note &lt; ActiveRecord::Base @@ -76,6 +121,14 @@ class Note &lt; ActiveRecord::Base
76 nil 121 nil
77 end 122 end
78 123
  124 + def notify
  125 + @notify ||= false
  126 + end
  127 +
  128 + def notify_author
  129 + @notify_author ||= false
  130 + end
  131 +
79 # Check if we can notify commit author 132 # Check if we can notify commit author
80 # with email about our comment 133 # with email about our comment
81 # 134 #
@@ -94,31 +147,9 @@ class Note &lt; ActiveRecord::Base @@ -94,31 +147,9 @@ class Note &lt; ActiveRecord::Base
94 commit_author.email != user.email 147 commit_author.email != user.email
95 end 148 end
96 149
97 - def for_commit?  
98 - noteable_type == "Commit"  
99 - end  
100 -  
101 - def for_diff_line?  
102 - line_code.present?  
103 - end  
104 -  
105 - def commit_author  
106 - @commit_author ||=  
107 - project.users.find_by_email(noteable.author_email) ||  
108 - project.users.find_by_name(noteable.author_name)  
109 - rescue  
110 - nil  
111 - end  
112 -  
113 # Returns true if this is an upvote note, 150 # Returns true if this is an upvote note,
114 # otherwise false is returned 151 # otherwise false is returned
115 def upvote? 152 def upvote?
116 note.start_with?('+1') || note.start_with?(':+1:') 153 note.start_with?('+1') || note.start_with?(':+1:')
117 end 154 end
118 -  
119 - # Returns true if this is a downvote note,  
120 - # otherwise false is returned  
121 - def downvote?  
122 - note.start_with?('-1') || note.start_with?(':-1:')  
123 - end  
124 end 155 end