class RuboCop::Cop::Rails::FilePath

This cop is used to identify usages of file path joining process to use `Rails.root.join` clause. It is used to add uniformity when joining paths.

@example EnforcedStyle: arguments

# bad
Rails.root.join('app/models/goober')
File.join(Rails.root, 'app/models/goober')
"#{Rails.root}/app/models/goober"

# good
Rails.root.join('app', 'models', 'goober')

@example EnforcedStyle: slashes (default)

# bad
Rails.root.join('app', 'models', 'goober')
File.join(Rails.root, 'app/models/goober')
"#{Rails.root}/app/models/goober"

# good
Rails.root.join('app/models/goober')

Constants

MSG_ARGUMENTS
MSG_SLASHES
RESTRICT_ON_SEND

Public Instance Methods

on_dstr(node) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 48
def on_dstr(node)
  return unless rails_root_nodes?(node)
  return unless node.children.last.str_type?
  return unless node.children.last.source.start_with?('.') ||
                node.children.last.source.include?(File::SEPARATOR)

  register_offense(node)
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 57
def on_send(node)
  check_for_file_join_with_rails_root(node)
  check_for_rails_root_join_with_slash_separated_path(node)
  check_for_rails_root_join_with_string_arguments(node)
end

Private Instance Methods

check_for_file_join_with_rails_root(node) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 65
def check_for_file_join_with_rails_root(node)
  return unless file_join_nodes?(node)
  return unless node.arguments.any? { |e| rails_root_nodes?(e) }

  register_offense(node)
end
check_for_rails_root_join_with_slash_separated_path(node) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 82
def check_for_rails_root_join_with_slash_separated_path(node)
  return unless style == :arguments
  return unless rails_root_nodes?(node)
  return unless rails_root_join_nodes?(node)
  return unless node.arguments.any? { |arg| string_with_slash?(arg) }

  register_offense(node)
end
check_for_rails_root_join_with_string_arguments(node) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 72
def check_for_rails_root_join_with_string_arguments(node)
  return unless style == :slashes
  return unless rails_root_nodes?(node)
  return unless rails_root_join_nodes?(node)
  return unless node.arguments.size > 1
  return unless node.arguments.all?(&:str_type?)

  register_offense(node)
end
message(_range) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 102
def message(_range)
  format(style == :arguments ? MSG_ARGUMENTS : MSG_SLASHES)
end
register_offense(node) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 95
def register_offense(node)
  line_range = node.loc.column...node.loc.last_column
  source_range = source_range(processed_source.buffer, node.first_line,
                              line_range)
  add_offense(source_range)
end
string_with_slash?(node) click to toggle source
# File lib/rubocop/cop/rails/file_path.rb, line 91
def string_with_slash?(node)
  node.str_type? && node.source.include?('/')
end