From 337eca2db8111af3c6fcfaf775c8c396d7db9e16 Mon Sep 17 00:00:00 2001 From: Marcin Ciunelis Date: Fri, 5 Oct 2012 19:09:29 +0200 Subject: [PATCH] BacktraceLineHelper refactoring --- app/helpers/backtrace_line_helper.rb | 55 +++++++++++++++++++++---------------------------------- app/models/backtrace.rb | 4 ++++ app/models/backtrace_line.rb | 24 ++++++++++++++++++++---- app/models/notice.rb | 1 + app/views/notices/_backtrace_line.html.haml | 8 ++++---- 5 files changed, 50 insertions(+), 42 deletions(-) diff --git a/app/helpers/backtrace_line_helper.rb b/app/helpers/backtrace_line_helper.rb index b83ab77..41be795 100644 --- a/app/helpers/backtrace_line_helper.rb +++ b/app/helpers/backtrace_line_helper.rb @@ -1,51 +1,38 @@ module BacktraceLineHelper - def link_to_source_file(app, line, &block) + def link_to_source_file(line, &block) text = capture_haml(&block) - if line.in_app? - return link_to_github(app, line, text) if app.github_repo? - return link_to_bitbucket(app, line, text) if app.bitbucket_repo? - if app.issue_tracker && app.issue_tracker.respond_to?(:url_to_file) - # Return link to file on tracker if issue tracker supports this - return link_to_issue_tracker_file(app, line, text) - end - end - text + line.in_app? ? link_to_in_app_source_file(line, text) : link_to_external_source_file(text) end - def filepath_parts(file) - [file.split('/').last, file] + private + def link_to_in_app_source_file(line, text) + link_to_repo_source_file(line, text) || link_to_issue_tracker_file(line, text) end - def link_to_github(app, line, text = nil) - file_name, file_path = filepath_parts(line.file) - href = "%s#L%s" % [app.github_url_to_file(file_path), line.number] - link_to(text || file_name, href, :target => '_blank') + def link_to_repo_source_file(line, text) + link_to_github(line, text) || link_to_bitbucket(line, text) end - def link_to_bitbucket(app, line, text = nil) - file_name, file_path = filepath_parts(line.file) - href = "%s#cl-%s" % [app.bitbucket_url_to_file(file_path), line.number] - link_to(text || file_name, href, :target => '_blank') + def link_to_external_source_file(text) + text end - def link_to_issue_tracker_file(app, line, text = nil) - file_name, file_path = filepath_parts(line.file_relative) - href = app.issue_tracker.url_to_file(file_path, line.number) - link_to(text || file_name, href, :target => '_blank') + def link_to_github(line, text = nil) + return unless line.app.github_repo? + href = "%s#L%s" % [line.app.github_url_to_file(line.file), line.number] + link_to(text || line.file_name, href, :target => '_blank') end - def path_for_backtrace_line(line) - path = File.dirname(line.file) - return '' if path == '.' - # Remove [PROJECT_ROOT] - path.gsub!('[PROJECT_ROOT]/', '') - # Make gem name bold if starts with [GEM_ROOT]/gems - path.gsub!(/\[GEM_ROOT\]\/gems\/([^\/]+)/, "\\1") - (path << '/').html_safe + def link_to_bitbucket(line, text = nil) + return unless line.app.bitbucket_repo? + href = "%s#cl-%s" % [line.app.bitbucket_url_to_file(line.file), line.number] + link_to(text || line.file_name, href, :target => '_blank') end - def file_for_backtrace_line(line) - file = File.basename(line.file) + def link_to_issue_tracker_file(line, text = nil) + return unless line.app.issue_tracker && line.app.issue_tracker.respond_to?(:url_to_file) + href = line.app.issue_tracker.url_to_file(line.file, line.number) + link_to(text || line.file_name, href, :target => '_blank') end end diff --git a/app/models/backtrace.rb b/app/models/backtrace.rb index 53defec..0edd38e 100644 --- a/app/models/backtrace.rb +++ b/app/models/backtrace.rb @@ -6,10 +6,14 @@ class Backtrace index :fingerprint has_many :notices + has_one :notice + embeds_many :lines, :class_name => "BacktraceLine" after_initialize :generate_fingerprint + delegate :app, :to => :notice + def self.find_or_create(attributes = {}) new(attributes).similar || create(attributes) end diff --git a/app/models/backtrace_line.rb b/app/models/backtrace_line.rb index d513948..506e492 100644 --- a/app/models/backtrace_line.rb +++ b/app/models/backtrace_line.rb @@ -1,6 +1,7 @@ class BacktraceLine include Mongoid::Document - IN_APP_REGEXP = %r{^\[PROJECT_ROOT\]\/(?!(vendor))} + IN_APP_PATH = %r{^\[PROJECT_ROOT\]\/(?!(vendor))} + GEMS_PATH = %r{\[GEM_ROOT\]\/gems\/([^\/]+)} field :number, :type => Integer field :file @@ -8,18 +9,33 @@ class BacktraceLine embedded_in :backtrace - scope :in_app, where(:file => IN_APP_REGEXP) + scope :in_app, where(:file => IN_APP_PATH) + + delegate :app, :to => :backtrace def to_s "#{file}:#{number}" end def in_app? - !!(file =~ IN_APP_REGEXP) + !!(file =~ IN_APP_PATH) + end + + def path + File.dirname(file).gsub(/^\.$/, '') + "/" end def file_relative - file.to_s.sub(/^\[PROJECT_ROOT\]/, '') + file.to_s.sub(IN_APP_PATH, '') + end + + def file_name + File.basename file + end + + def decorated_path + path.sub(BacktraceLine::IN_APP_PATH, ''). + sub(BacktraceLine::GEMS_PATH, "\\1") end end diff --git a/app/models/notice.rb b/app/models/notice.rb index 29c30b5..6a9c78c 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -13,6 +13,7 @@ class Notice field :current_user, :type => Hash field :error_class delegate :lines, :to => :backtrace, :prefix => true + delegate :app, :to => :err belongs_to :err belongs_to :backtrace, :index => true diff --git a/app/views/notices/_backtrace_line.html.haml b/app/views/notices/_backtrace_line.html.haml index be8362d..28eeafd 100644 --- a/app/views/notices/_backtrace_line.html.haml +++ b/app/views/notices/_backtrace_line.html.haml @@ -1,8 +1,8 @@ -%tr{:class => defined?(row_class) ? row_class : nil} +%tr{:class => defined?(row_class) && row_class} %td.line{:class => line.in_app? && 'in-app' } - = link_to_source_file(@app, line) do - %span.path>= path_for_backtrace_line(line) - %span.file>= file_for_backtrace_line(line) + = link_to_source_file(line) do + %span.path>=raw line.decorated_path + %span.file>= line.file_name - if line.number.present? %span.number>= ":#{line.number}" → -- libgit2 0.21.2