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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, RangeHelp
Defined in:
lib/rubocop/cop/rails/file_path.rb

Overview

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

NOTE: This cop ignores leading slashes in string literal arguments for Rails.root.join

and multiple slashes in string literal arguments for `Rails.root.join` and `File.join`.

Examples:

EnforcedStyle: slashes (default)

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

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

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

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

EnforcedStyle: arguments

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

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

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

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

Constant Summary collapse

MSG_SLASHES =
'Prefer `Rails.root.join(\'path/to\')%<to_s>s`.'
MSG_ARGUMENTS =
'Prefer `Rails.root.join(\'path\', \'to\')%<to_s>s`.'
RESTRICT_ON_SEND =
i[join].freeze

Instance Method Summary collapse

Instance Method Details

#on_dstr(node) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rubocop/cop/rails/file_path.rb', line 62

def on_dstr(node)
  return unless rails_root_nodes?(node)
  return if dstr_separated_by_colon?(node)

  check_for_slash_after_rails_root_in_dstr(node)
  check_for_extension_after_rails_root_join_in_dstr(node)
end

#on_send(node) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rubocop/cop/rails/file_path.rb', line 70

def on_send(node)
  check_for_file_join_with_rails_root(node)
  return unless node.receiver

  check_for_rails_root_join_with_slash_separated_path(node)
  check_for_rails_root_join_with_string_arguments(node)
end