Module: Mbeditor::RouteService

Defined in:
app/services/mbeditor/route_service.rb

Overview

Which routes reach a controller's actions, for the inline hints shown beside each def in a controller file.

Read from the host app's own route set rather than by parsing config/routes.rb. mbeditor runs inside the host app, so the routes are already built — and they are the only source that accounts for resources expansion, member/ collection blocks, scopes, constraints, mounted engines and anything a routes file does in plain Ruby. Parsing the file would get all of that wrong.

Constant Summary collapse

NON_ACTION_METHODS =

Actions with no route are worth calling out, but only for controllers Rails would actually dispatch to. These are the inherited ones every controller has and nobody routes.

%w[
  new inspect method_of to_s hash class dup freeze
].freeze

Class Method Summary collapse

Class Method Details

.controller_key(relative_path) ⇒ Object

"app/controllers/admin/users_controller.rb" -> "admin/users", which is the key Rails stores in a route's defaults.



24
25
26
27
28
29
30
# File 'app/services/mbeditor/route_service.rb', line 24

def controller_key(relative_path)
  path = relative_path.to_s.sub(%r{\A/+}, "")
  match = path.match(%r{\Aapp/controllers/(.+)_controller\.rb\z})
  return nil unless match

  match[1]
end

.for_controller(key) ⇒ Object

=> { "show" => [{ verb:, path:, name: }], ... }



33
34
35
36
37
38
39
40
41
42
# File 'app/services/mbeditor/route_service.rb', line 33

def for_controller(key)
  return {} if key.nil? || key.empty?
  return {} unless defined?(Rails) && Rails.respond_to?(:application) && Rails.application

  routes_for(key)
rescue StandardError
  # A broken route set must not take the editor down with it — the file still
  # opens, just without hints.
  {}
end