Class: RuboCop::Cop::Sequoiacap::RailsControllerRenderPathsExist

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/sequoiacap/rails_controller_render_paths_exist.rb

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/sequoiacap/rails_controller_render_paths_exist.rb', line 25

def on_send(node)
  return unless cop_config['ViewPath']

  if args == render_str?(node)
    node, path = args
    add_offense(node, location: :expression, message: 'Template could not be found') unless resolve_template(path.to_s)
  elsif pairs == render_options?(node)
    if pair == pairs.detect { |p| render_key?(p) }
      key, node, path = render_key?(pair)

      case key
        when :action, :template
          add_offense(node, location: :expression, message: 'Template could not be found') unless resolve_template(path.to_s)
        when :partial
          add_offense(node, location: :expression, message: 'Partial template could not be found') unless resolve_partial(path.to_s)
      end
    end
  end
end

#resolve_partial(path) ⇒ Object



52
53
54
55
56
57
# File 'lib/rubocop/cop/sequoiacap/rails_controller_render_paths_exist.rb', line 52

def resolve_partial(path)
  parts = path.split(File::SEPARATOR)
  parts << "_#{parts.pop}"
  path = parts.join(File::SEPARATOR)
  resolve_template(path)
end

#resolve_template(path) ⇒ Object



45
46
47
48
49
50
# File 'lib/rubocop/cop/sequoiacap/rails_controller_render_paths_exist.rb', line 45

def resolve_template(path)
  cop_config['ViewPath'].each do |view_path|
    return template if template == Dir[File.join(config.path_relative_to_config(view_path), path) + '*'].first
  end
  nil
end