Skip to content

Commit 66c67c8

Browse files
committed
Fix creating fsm visualizer with lazy routes
Previously, the visualizer html could be created very simply with ``` $ rails runner "Rails.application.routes.router.visualizer" > viz.html ``` This doesn't work with `LazyRouteSet` because it doesn't trigger route loading when calling `router` (so the visualizer ends up displaying no routes). This could be fixed by added `router` to `LazyRouteSet`, but since `.router.visualizer` is undocumented I'd like to propose moving it to `Routes` instead. `LazyRouteSet#routes` already triggers loading routes, and `Router#visualizer` ended up delegating all of its logic to `Routes` anyways. The command changes slightly, but remains as simple as before ``` $ rails runner "Rails.application.routes.routes.visualizer" > viz.html ``` This commit also includes two small changes along with the move. The first is that `partitioned_routes` is replaced with `Routes#anchored_routes`. This is an improvement because `partitioned_routes` previously relied on `requirements_anchored?` which can't be called after `eager_load` (it uses the path's `Journey::Ast`, which is cleared on `eager_load`). `anchored_routes` get filtered with the same conditions as `partitioned_routes` except the filtering is done as `Route`s are added to `Routes`. The other change is removing `return [] unless ast` from `Router#find_routes`. This is safe because `Routes#ast` already always returns a `Journey::Node` (and it enables removing `Router#ast` because `#find_routes` and `Router#visualizer` were the only two users of the method).
1 parent 27ff6c0 commit 66c67c8

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

actionpack/lib/action_dispatch/journey/router.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,7 @@ def recognize(req, &block)
9898
end
9999
end
100100

101-
def visualizer
102-
tt = GTG::Builder.new(ast).transition_table
103-
groups = partitioned_routes.first.map(&:ast).group_by(&:to_s)
104-
asts = groups.values.map(&:first)
105-
tt.visualizer(asts)
106-
end
107-
108101
private
109-
def partitioned_routes
110-
routes.partition { |r|
111-
r.path.anchored && r.path.requirements_anchored?
112-
}
113-
end
114-
115-
def ast
116-
routes.ast
117-
end
118-
119102
def simulator
120103
routes.simulator
121104
end
@@ -125,7 +108,6 @@ def custom_routes
125108
end
126109

127110
def filter_routes(path)
128-
return [] unless ast
129111
simulator.memos(path) { [] }
130112
end
131113

actionpack/lib/action_dispatch/journey/routes.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ def add_route(name, mapping)
7272
route
7373
end
7474

75+
def visualizer
76+
tt = GTG::Builder.new(ast).transition_table
77+
groups = anchored_routes.map(&:ast).group_by(&:to_s)
78+
asts = groups.values.map(&:first)
79+
tt.visualizer(asts)
80+
end
81+
7582
private
7683
def clear_cache!
7784
@ast = nil

0 commit comments

Comments
 (0)