Skip to content

Commit 3699558

Browse files
committed
Reduce iterations and allocations for Namespace#routes
1 parent 7d497cd commit 3699558

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/administrate/namespace.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ def resources
1111
end
1212

1313
def routes
14-
@routes ||= all_routes.select do |controller, _action|
15-
controller.starts_with?("#{namespace}/")
16-
end.map do |controller, action|
17-
[controller.gsub(/^#{namespace}\//, ""), action]
14+
@routes ||= begin
15+
prefix = "#{namespace}/".freeze
16+
Rails.application.routes.routes.filter_map do |route|
17+
next unless route.defaults[:controller]&.start_with?(prefix)
18+
19+
[
20+
route.defaults[:controller].delete_prefix(prefix),
21+
route.defaults[:action]
22+
]
23+
end
1824
end
1925
end
2026

@@ -25,11 +31,5 @@ def resources_with_index_route
2531
private
2632

2733
attr_reader :namespace
28-
29-
def all_routes
30-
Rails.application.routes.routes.map do |route|
31-
route.defaults.values_at(:controller, :action).map(&:to_s)
32-
end
33-
end
3434
end
3535
end

0 commit comments

Comments
 (0)