Class: RuboCop::Cop::Rails::FilePath

Inherits:
RuboCop::Cop show all
Includes:
ConfigurableEnforcedStyle, RangeHelp
Defined in:
lib/rubocop/cop/rails/file_path.rb

Overview

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.

Examples:

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')

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')

Constant Summary collapse

MSG_SLASHES =
'Please use `Rails.root.join(\'path/to\')` ' \
'instead.'
MSG_ARGUMENTS =
'Please use `Rails.root.join(\'path\', \'to\')` ' \
'instead.'

Instance Method Summary collapse

Instance Method Details

#on_dstr(node) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rails/file_path.rb', line 49

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) ⇒ Object



58
59
60
61
62
# File 'lib/rubocop/cop/rails/file_path.rb', line 58

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