Skip to content

Commit

Permalink
Reduce iterations and allocations for Namespace#routes (#2440)
Browse files Browse the repository at this point in the history
* Reduce iterations of `Namespace#routes`

This method is called during all requests and this change reduces the
amount of work needed to be done.

* Fix standardrb linting

---------

Co-authored-by: Nick Charlton <[email protected]>
  • Loading branch information
enviable and nickcharlton authored Jan 24, 2025
1 parent ccf0703 commit 69e0f06
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/administrate/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ def resources
end

def routes
@routes ||= all_routes.select do |controller, _action|
controller.starts_with?("#{namespace}/")
end.map do |controller, action|
[controller.gsub(/^#{namespace}\//, ""), action]
@routes ||= begin
prefix = "#{namespace}/".freeze
Rails.application.routes.routes.filter_map do |route|
next unless route.defaults[:controller]&.start_with?(prefix)

[
route.defaults[:controller].delete_prefix(prefix),
route.defaults[:action]
]
end
end
end

Expand All @@ -25,11 +31,5 @@ def resources_with_index_route
private

attr_reader :namespace

def all_routes
Rails.application.routes.routes.map do |route|
route.defaults.values_at(:controller, :action).map(&:to_s)
end
end
end
end

0 comments on commit 69e0f06

Please sign in to comment.