Commit 337eca2db8111af3c6fcfaf775c8c396d7db9e16

Authored by Marcin Ciunelis
1 parent 26c5f32b
Exists in master and in 1 other branch production

BacktraceLineHelper refactoring

app/helpers/backtrace_line_helper.rb
1 1 module BacktraceLineHelper
2   - def link_to_source_file(app, line, &block)
  2 + def link_to_source_file(line, &block)
3 3 text = capture_haml(&block)
4   - if line.in_app?
5   - return link_to_github(app, line, text) if app.github_repo?
6   - return link_to_bitbucket(app, line, text) if app.bitbucket_repo?
7   - if app.issue_tracker && app.issue_tracker.respond_to?(:url_to_file)
8   - # Return link to file on tracker if issue tracker supports this
9   - return link_to_issue_tracker_file(app, line, text)
10   - end
11   - end
12   - text
  4 + line.in_app? ? link_to_in_app_source_file(line, text) : link_to_external_source_file(text)
13 5 end
14 6  
15   - def filepath_parts(file)
16   - [file.split('/').last, file]
  7 + private
  8 + def link_to_in_app_source_file(line, text)
  9 + link_to_repo_source_file(line, text) || link_to_issue_tracker_file(line, text)
17 10 end
18 11  
19   - def link_to_github(app, line, text = nil)
20   - file_name, file_path = filepath_parts(line.file)
21   - href = "%s#L%s" % [app.github_url_to_file(file_path), line.number]
22   - link_to(text || file_name, href, :target => '_blank')
  12 + def link_to_repo_source_file(line, text)
  13 + link_to_github(line, text) || link_to_bitbucket(line, text)
23 14 end
24 15  
25   - def link_to_bitbucket(app, line, text = nil)
26   - file_name, file_path = filepath_parts(line.file)
27   - href = "%s#cl-%s" % [app.bitbucket_url_to_file(file_path), line.number]
28   - link_to(text || file_name, href, :target => '_blank')
  16 + def link_to_external_source_file(text)
  17 + text
29 18 end
30 19  
31   - def link_to_issue_tracker_file(app, line, text = nil)
32   - file_name, file_path = filepath_parts(line.file_relative)
33   - href = app.issue_tracker.url_to_file(file_path, line.number)
34   - link_to(text || file_name, href, :target => '_blank')
  20 + def link_to_github(line, text = nil)
  21 + return unless line.app.github_repo?
  22 + href = "%s#L%s" % [line.app.github_url_to_file(line.file), line.number]
  23 + link_to(text || line.file_name, href, :target => '_blank')
35 24 end
36 25  
37   - def path_for_backtrace_line(line)
38   - path = File.dirname(line.file)
39   - return '' if path == '.'
40   - # Remove [PROJECT_ROOT]
41   - path.gsub!('[PROJECT_ROOT]/', '')
42   - # Make gem name bold if starts with [GEM_ROOT]/gems
43   - path.gsub!(/\[GEM_ROOT\]\/gems\/([^\/]+)/, "<strong>\\1</strong>")
44   - (path << '/').html_safe
  26 + def link_to_bitbucket(line, text = nil)
  27 + return unless line.app.bitbucket_repo?
  28 + href = "%s#cl-%s" % [line.app.bitbucket_url_to_file(line.file), line.number]
  29 + link_to(text || line.file_name, href, :target => '_blank')
45 30 end
46 31  
47   - def file_for_backtrace_line(line)
48   - file = File.basename(line.file)
  32 + def link_to_issue_tracker_file(line, text = nil)
  33 + return unless line.app.issue_tracker && line.app.issue_tracker.respond_to?(:url_to_file)
  34 + href = line.app.issue_tracker.url_to_file(line.file, line.number)
  35 + link_to(text || line.file_name, href, :target => '_blank')
49 36 end
50 37  
51 38 end
... ...
app/models/backtrace.rb
... ... @@ -6,10 +6,14 @@ class Backtrace
6 6 index :fingerprint
7 7  
8 8 has_many :notices
  9 + has_one :notice
  10 +
9 11 embeds_many :lines, :class_name => "BacktraceLine"
10 12  
11 13 after_initialize :generate_fingerprint
12 14  
  15 + delegate :app, :to => :notice
  16 +
13 17 def self.find_or_create(attributes = {})
14 18 new(attributes).similar || create(attributes)
15 19 end
... ...
app/models/backtrace_line.rb
1 1 class BacktraceLine
2 2 include Mongoid::Document
3   - IN_APP_REGEXP = %r{^\[PROJECT_ROOT\]\/(?!(vendor))}
  3 + IN_APP_PATH = %r{^\[PROJECT_ROOT\]\/(?!(vendor))}
  4 + GEMS_PATH = %r{\[GEM_ROOT\]\/gems\/([^\/]+)}
4 5  
5 6 field :number, :type => Integer
6 7 field :file
... ... @@ -8,18 +9,33 @@ class BacktraceLine
8 9  
9 10 embedded_in :backtrace
10 11  
11   - scope :in_app, where(:file => IN_APP_REGEXP)
  12 + scope :in_app, where(:file => IN_APP_PATH)
  13 +
  14 + delegate :app, :to => :backtrace
12 15  
13 16 def to_s
14 17 "#{file}:#{number}"
15 18 end
16 19  
17 20 def in_app?
18   - !!(file =~ IN_APP_REGEXP)
  21 + !!(file =~ IN_APP_PATH)
  22 + end
  23 +
  24 + def path
  25 + File.dirname(file).gsub(/^\.$/, '') + "/"
19 26 end
20 27  
21 28 def file_relative
22   - file.to_s.sub(/^\[PROJECT_ROOT\]/, '')
  29 + file.to_s.sub(IN_APP_PATH, '')
  30 + end
  31 +
  32 + def file_name
  33 + File.basename file
  34 + end
  35 +
  36 + def decorated_path
  37 + path.sub(BacktraceLine::IN_APP_PATH, '').
  38 + sub(BacktraceLine::GEMS_PATH, "<strong>\\1</strong>")
23 39 end
24 40  
25 41 end
... ...
app/models/notice.rb
... ... @@ -13,6 +13,7 @@ class Notice
13 13 field :current_user, :type => Hash
14 14 field :error_class
15 15 delegate :lines, :to => :backtrace, :prefix => true
  16 + delegate :app, :to => :err
16 17  
17 18 belongs_to :err
18 19 belongs_to :backtrace, :index => true
... ...
app/views/notices/_backtrace_line.html.haml
1   -%tr{:class => defined?(row_class) ? row_class : nil}
  1 +%tr{:class => defined?(row_class) && row_class}
2 2 %td.line{:class => line.in_app? && 'in-app' }
3   - = link_to_source_file(@app, line) do
4   - %span.path>= path_for_backtrace_line(line)
5   - %span.file>= file_for_backtrace_line(line)
  3 + = link_to_source_file(line) do
  4 + %span.path>=raw line.decorated_path
  5 + %span.file>= line.file_name
6 6 - if line.number.present?
7 7 %span.number>= ":#{line.number}"
8 8 &rarr;
... ...