class RailsRoutesAnalyzer::RouteLine

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

Attributes

full_filename[R]
line_number[R]
records[R]

Public Class Methods

new(full_filename:, line_number:, records:) click to toggle source
# 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

Public Instance Methods

add_suggestions_to(line, suggestions) click to toggle source
# 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_names() click to toggle source
# 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:) click to toggle source
# 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_suggestions() click to toggle source
# 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_location() click to toggle source
# File lib/rails_routes_analyzer/route_line.rb, line 15
def file_location
  @file_location ||= "#{full_filename}:#{line_number}"
end
issues() click to toggle source
# File lib/rails_routes_analyzer/route_line.rb, line 23
def issues
  @issues ||= records.select(&:issue?)
end
issues?() click to toggle source
# File lib/rails_routes_analyzer/route_line.rb, line 27
def issues?
  issues.any?
end
present_actions?() click to toggle source
# File lib/rails_routes_analyzer/route_line.rb, line 19
def present_actions?
  records.any?(&:present_actions?)
end
safely_deletable_line?(line) click to toggle source

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.

# 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:) click to toggle source

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.

# 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