Class: RailsRoutesAnalyzer::RouteLine

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_routes_analyzer/route_line.rb

Overview

Represents a single line in Rails routes file with all the collected information about that line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_filename:, line_number:, records:) ⇒ RouteLine

Returns a new instance of RouteLine.



9
10
11
12
13
# File 'lib/rails_routes_analyzer/route_line.rb', line 9

def initialize(full_filename:, line_number:, records:)
  @full_filename = full_filename
  @line_number   = line_number
  @records       = records
end

Instance Attribute Details

#full_filenameObject (readonly)

Returns the value of attribute full_filename.



7
8
9
# File 'lib/rails_routes_analyzer/route_line.rb', line 7

def full_filename
  @full_filename
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



7
8
9
# File 'lib/rails_routes_analyzer/route_line.rb', line 7

def line_number
  @line_number
end

#recordsObject (readonly)

Returns the value of attribute records.



7
8
9
# File 'lib/rails_routes_analyzer/route_line.rb', line 7

def records
  @records
end

Instance Method Details

#add_suggestions_to(line, suggestions) ⇒ Object



63
64
65
66
# File 'lib/rails_routes_analyzer/route_line.rb', line 63

def add_suggestions_to(line, suggestions)
  line.sub(/( # SUGGESTION.*)?$/,
           suggestions.present? ? " # SUGGESTION #{suggestions}" : "")
end

#all_controller_class_namesObject



68
69
70
# File 'lib/rails_routes_analyzer/route_line.rb', line 68

def all_controller_class_names
  records.map(&:controller_class_name).uniq
end

#annotate(line, try_to_fix:, allow_deleting:) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rails_routes_analyzer/route_line.rb', line 31

def annotate(line, try_to_fix:, allow_deleting:)
  suggestions = combined_suggestions
  if try_to_fix && !(fix_for_line = try_to_fix_line(line, allow_deleting: allow_deleting,
                                                          suggestion_comment: "# SUGGESTION #{suggestions}")).nil?
    fix_for_line
  else
    add_suggestions_to(line, suggestions)
  end
end

#combined_suggestionsObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/rails_routes_analyzer/route_line.rb', line 72

def combined_suggestions
  return unless issues?

  context = {
    has_present_actions: present_actions?,
    num_controllers:     all_controller_class_names.count,
  }

  issues.map { |issue| issue.suggestion(**context) }.flatten.join(', ')
end

#file_locationObject



15
16
17
# File 'lib/rails_routes_analyzer/route_line.rb', line 15

def file_location
  @file_location ||= "#{full_filename}:#{line_number}"
end

#issuesObject



23
24
25
# File 'lib/rails_routes_analyzer/route_line.rb', line 23

def issues
  @issues ||= records.select(&:issue?)
end

#issues?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rails_routes_analyzer/route_line.rb', line 27

def issues?
  issues.any?
end

#present_actions?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rails_routes_analyzer/route_line.rb', line 19

def present_actions?
  records.any?(&:present_actions?)
end

#safely_deletable_line?(line) ⇒ Boolean

Should avoid deleting lines that look like they might start a block because we’re not smart enough to also be able to delete the end of that block.

Returns:

  • (Boolean)


59
60
61
# File 'lib/rails_routes_analyzer/route_line.rb', line 59

def safely_deletable_line?(line)
  line !~ /( do(\s|$)|{)/
end

#try_to_fix_line(line, allow_deleting:, suggestion_comment:) ⇒ Object

Try to generate an automatic fix for the line, this does not apply to lines with multiple issues (iterations) as those will most likely require changes to the surrounding code.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_routes_analyzer/route_line.rb', line 44

def try_to_fix_line(line, allow_deleting:, suggestion_comment:)
  has_one_issue = issues.size == 1
  has_one_iteration = records.size == 1

  return unless has_one_issue && has_one_iteration

  fix = issues[0].try_to_fix_line(line)

  return unless fix.present? || (fix == '' && allow_deleting && safely_deletable_line?(line))

  fix.gsub(suggestion_comment, '').gsub(/\ +$/, '')
end