class Danger::DangerRailsBestPractices

Lints Ruby files via [rails_best_practices](rubygems.org/gems/rails_best_practices). Results are sent as inline comments.

@example Running rails_best_practices

# Runs rails_best_practices on modified and added files in the PR
rails_best_practices.lint

@see blooper05/danger-rails_best_practices @tags ruby, rails, rails_best_practices, lint

Public Instance Methods

lint() click to toggle source

Runs Ruby files through rails_best_practices. @return [Array<RailsBestPractices::Core::Error, nil>]

# File lib/rails_best_practices/plugin.rb, line 17
def lint
  files_to_lint = fetch_files_to_lint
  lint_errors   = run_linter(files_to_lint)
  warn_each_line(lint_errors)
end

Private Instance Methods

fetch_files_to_lint() click to toggle source
# File lib/rails_best_practices/plugin.rb, line 33
def fetch_files_to_lint
  files = git.modified_files + git.added_files
  files.map { |file| Regexp.compile(Regexp.escape(file)) }.to_a
end
run_linter(files_to_lint) click to toggle source
# File lib/rails_best_practices/plugin.rb, line 25
def run_linter(files_to_lint)
  analyzer = ::RailsBestPractices::Analyzer.new(
    '.', 'silent' => true, 'only' => files_to_lint
  )
  analyzer.analyze
  analyzer.errors
end
warn_each_line(lint_errors) click to toggle source
# File lib/rails_best_practices/plugin.rb, line 38
def warn_each_line(lint_errors)
  current_dir = "#{Dir.pwd}/"
  lint_errors.each do |error|
    message = error.message.capitalize
    file    = error.filename.sub(current_dir, '')
    line    = error.line_number.to_i
    warn(message, file: file, line: line)
  end
end