Commit b177f0e5ae4b3e65171b6ff54c3f1b65df0ceb10

Authored by Stephen Crosby
1 parent d3b6d050
Exists in master and in 1 other branch production

simplify line grouper and add coverage

app/decorators/backtrace_decorator.rb
@@ -2,4 +2,11 @@ class BacktraceDecorator < Draper::Decorator @@ -2,4 +2,11 @@ class BacktraceDecorator < Draper::Decorator
2 def lines 2 def lines
3 @lines ||= object.lines.map { |line| BacktraceLineDecorator.new line } 3 @lines ||= object.lines.map { |line| BacktraceLineDecorator.new line }
4 end 4 end
  5 +
  6 + # Group lines into sections of in-app files and external files
  7 + def grouped_lines
  8 + lines.chunk do |line|
  9 + line.in_app? || false
  10 + end
  11 + end
5 end 12 end
app/helpers/backtrace_helper.rb
@@ -1,16 +0,0 @@ @@ -1,16 +0,0 @@
1 -module BacktraceHelper  
2 - # Group lines into sections of in-app files and external files  
3 - # (An implementation of Enumerable#chunk so we don't break 1.8.7 support.)  
4 - def grouped_lines(lines)  
5 - line_groups = []  
6 - lines.each do |line|  
7 - in_app = line.in_app?  
8 - if line_groups.last && line_groups.last[0] == in_app  
9 - line_groups.last[1] << line  
10 - else  
11 - line_groups << [in_app, [line]]  
12 - end  
13 - end  
14 - line_groups  
15 - end  
16 -end  
app/views/notices/_backtrace.html.haml
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 -# Group lines into internal / external so we can toggle the external backtrace 3 -# Group lines into internal / external so we can toggle the external backtrace
4 -# Includes a margin of x lines that are not toggled. 4 -# Includes a margin of x lines that are not toggled.
5 - em = 4 # (external backtrace margin) 5 - em = 4 # (external backtrace margin)
6 - - grouped_lines(lines).each do |in_app, line_group| 6 + - backtrace.grouped_lines.each do |in_app, line_group|
7 - if !in_app && line_group.size > (em * 3) 7 - if !in_app && line_group.size > (em * 3)
8 = render :partial => 'notices/backtrace_line', :collection => line_group[0, em], :as => :line 8 = render :partial => 'notices/backtrace_line', :collection => line_group[0, em], :as => :line
9 = render :partial => 'notices/backtrace_line', :collection => line_group[em, line_group.size - (em * 2)], :as => :line, :locals => {:row_class => 'toggle_external_backtrace'} 9 = render :partial => 'notices/backtrace_line', :collection => line_group[em, line_group.size - (em * 2)], :as => :line, :locals => {:row_class => 'toggle_external_backtrace'}
app/views/problems/show.html.haml
@@ -68,7 +68,7 @@ @@ -68,7 +68,7 @@
68 68
69 #backtrace 69 #backtrace
70 %h3 Backtrace 70 %h3 Backtrace
71 - = render 'notices/backtrace', :lines => @notice.backtrace.lines 71 + = render 'notices/backtrace', :backtrace => @notice.backtrace
72 72
73 - if @notice.user_attributes.present? 73 - if @notice.user_attributes.present?
74 #user_attributes 74 #user_attributes
spec/decorators/backtrace_decorator_spec.rb 0 → 100644
@@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
  1 +describe BacktraceDecorator, type: :decorator do
  2 + let(:backtrace) do
  3 + described_class.new(Backtrace.new(
  4 + lines: [
  5 + { number: 131,
  6 + file: '[PROJECT_ROOT]app/controllers/accounts_controller.rb',
  7 + method: :update_preferences },
  8 + { number: 61,
  9 + file: '[PROJECT_ROOT]app/controllers/application_controller.rb',
  10 + method: :call },
  11 + { number: 182,
  12 + file: '[GEM_ROOT]activesupport-2.3.18/lib/active_support/callbacks.rb',
  13 + method: :call },
  14 + { number: 384,
  15 + file: '[PROJECT_ROOT]app/models/account.rb',
  16 + method: :update_server_tag_scope },
  17 + { number: 182,
  18 + file: '[GEM_ROOT]activesupport-2.3.18/lib/active_support/callbacks.rb',
  19 + method: :evaluate_method },
  20 + { number: 23,
  21 + file: '/home/rails/library/current/vendor/bundle/ruby/2.1.0/bin/rainbows',
  22 + method: '<main>' }
  23 + ]
  24 + ))
  25 + end
  26 +
  27 + describe '#grouped_lines' do
  28 + let(:grouped) { backtrace.grouped_lines.to_a }
  29 +
  30 + it 'puts the first two in-app lines in separate groups' do
  31 + expect(grouped[0][0]).to be_present
  32 + expect(grouped[0][1]).to eq [backtrace.lines[0]]
  33 + expect(grouped[1][0]).to be_present
  34 + expect(grouped[1][1]).to eq [backtrace.lines[1]]
  35 + end
  36 +
  37 + it 'puts the first out-of-app line in its own group' do
  38 + expect(grouped[2][0]).to eq false
  39 + expect(grouped[2][1]).to eq [backtrace.lines[2]]
  40 + end
  41 +
  42 + it 'puts the last two out-of-app lines together in one group' do
  43 + expect(grouped[4][0]).to eq false
  44 + expect(grouped[4][1]).to eq [backtrace.lines[4], backtrace.lines[5]]
  45 + end
  46 + end
  47 +end