Module: RuboCop::Cop::Rails::RouteHelper

Included in:
RouteConsistentSpacing, RouteGrouping, RouteRootPosition, RouteSeparation, RouteSorting, RouteUnnecessaryArrayNotation
Defined in:
lib/rubocop/cop/shared/route_helper.rb

Instance Method Summary collapse

Instance Method Details

#collect_routes(routes_block) ⇒ Object



24
25
26
27
28
29
# File 'lib/rubocop/cop/shared/route_helper.rb', line 24

def collect_routes(routes_block)
  collector = RouteCollector.new
  body = routes_block.body
  collector.collect(body) if body
  collector.routes.sort_by { |route| route[:line] }
end

#collect_routes_from_file(ast_node) ⇒ Object



31
32
33
34
35
# File 'lib/rubocop/cop/shared/route_helper.rb', line 31

def collect_routes_from_file(ast_node)
  collector = RouteCollector.new
  collector.collect(ast_node) if ast_node
  collector.routes.sort_by { |route| route[:line] }
end

#process_route_block(node) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/shared/route_helper.rb', line 37

def process_route_block(node)
  return unless route_block?(node)

  routes = collect_routes(node)
  return if routes.size < minimum_routes_for_check

  check_routes(routes)
end

#process_route_file(processed_source) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/shared/route_helper.rb', line 46

def process_route_file(processed_source)
  return unless route_file?
  return if processed_source.ast.nil?

  routes = collect_routes_from_file(processed_source.ast)
  return if routes.size < minimum_routes_for_check

  check_routes(routes)
end

#route_block?(node) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/shared/route_helper.rb', line 13

def route_block?(node)
  return false unless node.block_type?

  send_node = node.send_node
  return false unless send_node.send_type?
  return true if send_node.receiver&.source == 'Rails.application.routes' && send_node.method_name == :draw
  return false unless route_file?

  [:scope, :namespace, :concern].include?(send_node.method_name)
end

#route_file?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/rubocop/cop/shared/route_helper.rb', line 9

def route_file?
  processed_source.file_path&.end_with?('routes.rb')
end