Skip to content

Commit

Permalink
Reduce iterations of Namespace#routes
Browse files Browse the repository at this point in the history
This method is called during all requests and this change reduces the
amount of work needed to be done.
  • Loading branch information
enviable authored and nickcharlton committed Dec 12, 2023
1 parent 17e2774 commit b8fec65
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 b8fec65

Please sign in to comment.