Skip to content

Commit

Permalink
fix: missing route names (#35)
Browse files Browse the repository at this point in the history
# Issue

Fixes #34.

# Overview

This PR fixes an issue where Chusaku doesn't annotate a name for a route
even when its path has one.
  • Loading branch information
nshki authored Aug 8, 2023
1 parent 3d924db commit 9a153de
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
29 changes: 27 additions & 2 deletions lib/chusaku/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,36 @@ def format(route:, verb:, defaults:)
def backfill_routes(routes)
paths = {}

# Map paths to their verbs and names.
#
# Resulting hash looks like:
#
# ```ruby
# {
# "/users/:id" => {
# "GET" => "edit_user",
# "PATCH" => "edit_user"
# }
# }
# ````
routes.each do |_controller, actions|
actions.each do |_action, data|
data.each do |datum|
paths[datum[:path]] ||= datum[:name]
datum[:name] ||= paths[datum[:path]]
paths[datum[:path]] ||= {}
paths[datum[:path]][datum[:verb]] ||= datum[:name]
end
end
end

# Backfill names for routes that don't have them.
#
# First try to match based on the path and verb. If that doesn't work,
# try to match based on the path alone.
routes.each do |_controller, actions|
actions.each do |_action, data|
data.each do |datum|
datum[:name] ||= paths.dig(datum[:path], datum[:verb])
datum[:name] ||= paths[datum[:path]]&.values&.compact&.first
end
end
end
Expand Down
15 changes: 14 additions & 1 deletion test/chusaku_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_exit_with_error_on_annotation_flag
out, _err = capture_io { exit_code = Chusaku.call(error_on_annotation: true) }

assert_equal(1, exit_code)
assert_equal(2, File.written_files.count)
assert_equal(3, File.written_files.count)
assert_includes(out, "Exited with status code 1.")
end

Expand Down Expand Up @@ -68,6 +68,19 @@ def test_mock_app
assert(2, files.count)
refute_includes(files, "#{base_path}/api/burritos_controller.rb")

expected =
<<~HEREDOC
class CakesController < ApplicationController
# This route's GET action should be named the same as its PUT action,
# even though only the PUT action is named.
# @route GET /api/cakes/inherit (inherit)
# @route PUT /api/cakes/inherit (inherit)
def inherit
end
end
HEREDOC
assert_equal(expected, files["#{base_path}/api/cakes_controller.rb"])

expected =
<<~HEREDOC
class TacosController < ApplicationController
Expand Down
6 changes: 6 additions & 0 deletions test/mock/app/controllers/api/cakes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CakesController < ApplicationController
# This route's GET action should be named the same as its PUT action,
# even though only the PUT action is named.
def inherit
end
end
14 changes: 14 additions & 0 deletions test/mock/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def application
verb: "POST",
path: "/api/burritos(.:format)",
name: "burritos"
routes.push \
mock_route \
controller: "api/cakes",
action: "inherit",
verb: "GET",
path: "/api/cakes/inherit(.:format)",
name: nil
routes.push \
mock_route \
controller: "api/cakes",
action: "inherit",
verb: "PUT",
path: "/api/cakes/inherit(.:format)",
name: "inherit"
routes.push \
mock_route \
controller: "api/tacos",
Expand Down
6 changes: 6 additions & 0 deletions test/routes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def test_mock_rails
{verb: "POST", path: "/api/burritos", name: "burritos", defaults: {}}
]
},
"api/cakes" => {
"inherit" => [
{verb: "GET", path: "/api/cakes/inherit", name: "inherit", defaults: {}},
{verb: "PUT", path: "/api/cakes/inherit", name: "inherit", defaults: {}}
]
},
"api/tacos" => {
"show" => [
{verb: "GET", path: "/", name: "root", defaults: {}},
Expand Down

0 comments on commit 9a153de

Please sign in to comment.