From b177f0e5ae4b3e65171b6ff54c3f1b65df0ceb10 Mon Sep 17 00:00:00 2001 From: Stephen Crosby Date: Mon, 28 Sep 2015 21:27:35 -0700 Subject: [PATCH] simplify line grouper and add coverage --- app/decorators/backtrace_decorator.rb | 7 +++++++ app/helpers/backtrace_helper.rb | 16 ---------------- app/views/notices/_backtrace.html.haml | 2 +- app/views/problems/show.html.haml | 2 +- spec/decorators/backtrace_decorator_spec.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 18 deletions(-) delete mode 100644 app/helpers/backtrace_helper.rb create mode 100644 spec/decorators/backtrace_decorator_spec.rb diff --git a/app/decorators/backtrace_decorator.rb b/app/decorators/backtrace_decorator.rb index 9c2392d..6ee5495 100644 --- a/app/decorators/backtrace_decorator.rb +++ b/app/decorators/backtrace_decorator.rb @@ -2,4 +2,11 @@ class BacktraceDecorator < Draper::Decorator def lines @lines ||= object.lines.map { |line| BacktraceLineDecorator.new line } end + + # Group lines into sections of in-app files and external files + def grouped_lines + lines.chunk do |line| + line.in_app? || false + end + end end diff --git a/app/helpers/backtrace_helper.rb b/app/helpers/backtrace_helper.rb deleted file mode 100644 index ba48753..0000000 --- a/app/helpers/backtrace_helper.rb +++ /dev/null @@ -1,16 +0,0 @@ -module BacktraceHelper - # Group lines into sections of in-app files and external files - # (An implementation of Enumerable#chunk so we don't break 1.8.7 support.) - def grouped_lines(lines) - line_groups = [] - lines.each do |line| - in_app = line.in_app? - if line_groups.last && line_groups.last[0] == in_app - line_groups.last[1] << line - else - line_groups << [in_app, [line]] - end - end - line_groups - end -end diff --git a/app/views/notices/_backtrace.html.haml b/app/views/notices/_backtrace.html.haml index 9ccddd8..991d551 100644 --- a/app/views/notices/_backtrace.html.haml +++ b/app/views/notices/_backtrace.html.haml @@ -3,7 +3,7 @@ -# Group lines into internal / external so we can toggle the external backtrace -# Includes a margin of x lines that are not toggled. - em = 4 # (external backtrace margin) - - grouped_lines(lines).each do |in_app, line_group| + - backtrace.grouped_lines.each do |in_app, line_group| - if !in_app && line_group.size > (em * 3) = render :partial => 'notices/backtrace_line', :collection => line_group[0, em], :as => :line = render :partial => 'notices/backtrace_line', :collection => line_group[em, line_group.size - (em * 2)], :as => :line, :locals => {:row_class => 'toggle_external_backtrace'} diff --git a/app/views/problems/show.html.haml b/app/views/problems/show.html.haml index d1a7ae3..1a4ee29 100644 --- a/app/views/problems/show.html.haml +++ b/app/views/problems/show.html.haml @@ -68,7 +68,7 @@ #backtrace %h3 Backtrace - = render 'notices/backtrace', :lines => @notice.backtrace.lines + = render 'notices/backtrace', :backtrace => @notice.backtrace - if @notice.user_attributes.present? #user_attributes diff --git a/spec/decorators/backtrace_decorator_spec.rb b/spec/decorators/backtrace_decorator_spec.rb new file mode 100644 index 0000000..dfba0f9 --- /dev/null +++ b/spec/decorators/backtrace_decorator_spec.rb @@ -0,0 +1,47 @@ +describe BacktraceDecorator, type: :decorator do + let(:backtrace) do + described_class.new(Backtrace.new( + lines: [ + { number: 131, + file: '[PROJECT_ROOT]app/controllers/accounts_controller.rb', + method: :update_preferences }, + { number: 61, + file: '[PROJECT_ROOT]app/controllers/application_controller.rb', + method: :call }, + { number: 182, + file: '[GEM_ROOT]activesupport-2.3.18/lib/active_support/callbacks.rb', + method: :call }, + { number: 384, + file: '[PROJECT_ROOT]app/models/account.rb', + method: :update_server_tag_scope }, + { number: 182, + file: '[GEM_ROOT]activesupport-2.3.18/lib/active_support/callbacks.rb', + method: :evaluate_method }, + { number: 23, + file: '/home/rails/library/current/vendor/bundle/ruby/2.1.0/bin/rainbows', + method: '
' } + ] + )) + end + + describe '#grouped_lines' do + let(:grouped) { backtrace.grouped_lines.to_a } + + it 'puts the first two in-app lines in separate groups' do + expect(grouped[0][0]).to be_present + expect(grouped[0][1]).to eq [backtrace.lines[0]] + expect(grouped[1][0]).to be_present + expect(grouped[1][1]).to eq [backtrace.lines[1]] + end + + it 'puts the first out-of-app line in its own group' do + expect(grouped[2][0]).to eq false + expect(grouped[2][1]).to eq [backtrace.lines[2]] + end + + it 'puts the last two out-of-app lines together in one group' do + expect(grouped[4][0]).to eq false + expect(grouped[4][1]).to eq [backtrace.lines[4], backtrace.lines[5]] + end + end +end -- libgit2 0.21.2