[#19] small refactoring

This commit is contained in:
Alexander Machehin 2013-04-01 17:41:18 +06:00
parent cac49cf96d
commit f848fefd46
1 changed files with 12 additions and 12 deletions

View File

@ -4,8 +4,7 @@ class Comment < ActiveRecord::Base
# User/Project#Num
# User#Num
# #Num
ISSUES_REGEX = /(?:[a-zA-Z0-9\-_]*\/)?(?:[a-zA-Z0-9\-_]*)?#[0-9]+/
ISSUE_REGEX = /([a-zA-Z0-9\-_]*\/)?([a-zA-Z0-9\-_]*)?#([0-9]+)/
ISSUES_REGEX = /(?:[a-zA-Z0-9\-_]*\/)?(?:[a-zA-Z0-9\-_]*)?[#!][0-9]+/
belongs_to :commentable, :polymorphic => true, :touch => true
belongs_to :user
@ -137,23 +136,24 @@ class Comment < ActiveRecord::Base
end
def self.create_link_on_issues_from_item item, opts = {}
linker = item.user.present? ? item.user : User.find_by_uname('rosa_system')
linker = item.user
elements = if item.is_a? Comment
[[item, item.body]]
elsif item.is_a? GitHook
opts[:commits]
end
current_ability = Ability.new(linker)
elements.each do |element|
element[1].scan(ISSUES_REGEX).each do |hash|
hash =~ ISSUE_REGEX
owner_uname = Regexp.last_match[1].presence || Regexp.last_match[2].presence || item.project.owner.uname
project_name = Regexp.last_match[1] ? Regexp.last_match[2] : item.project.name
serial_id = Regexp.last_match[3]
project = Project.find_by_owner_and_name(owner_uname.chomp('/'), project_name)
next unless project
next unless Ability.new(item.user).can? :read, project
issue = project.issues.where(:serial_id => serial_id).first
delimiter = if hash.include? '!'
'!'
elsif hash.include? '#'
'#'
else
raise 'Unknown delimiter for the hash tag!'
end
issue = Issue.find_by_hash_tag hash, current_ability, item.project, delimiter
next unless issue
next if issue == item.try(:commentable) # dont create link to the same issue
# dont create duplicate link to issue
@ -165,7 +165,7 @@ class Comment < ActiveRecord::Base
next
end
comment = linker.comments.new :body => 'automatic comment'
comment.commentable, comment.project, comment.automatic = issue, project, true
comment.commentable, comment.project, comment.automatic = issue, issue.project, true
comment.data = {:comment_id => item.id, :from_project_id => item.project.id}
if item.is_a?(Comment) && item.commentable_type == 'Issue'
comment.created_from_issue_id = item.commentable_id