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

CACHE_TTL =

Matches the other read-through caches in this engine (FileTreeService 15s, GitInfoService 10s). A write invalidates it outright, so the TTL only ever bounds staleness from a route change made outside the editor.

10

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.



17
18
19
20
21
22
23
# File 'app/services/mbeditor/route_service.rb', line 17

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: }], ... }

Cached because every call walks the host app's ENTIRE route set, and the caller is the inline route hints — which re-request on every activation of a controller tab and on every external content change. Switching between two controllers repeatedly was therefore a full O(routes) scan per switch, and a large app has thousands of routes. The scan itself is unchanged; it just stops happening once per glance.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/mbeditor/route_service.rb', line 41

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

  cached = cached_routes(key)
  return cached if cached

  # Built outside the mutex: it runs arbitrary host-app route code, and
  # holding the lock across it would serialise every other controller's
  # lookup behind the slowest one.
  computed = routes_for(key)
  store_routes(key, computed)
  computed
rescue StandardError
  # A broken route set must not take the editor down with it — the file still
  # opens, just without hints.
  {}
end

.invalidateObject



60
61
62
63
# File 'app/services/mbeditor/route_service.rb', line 60

def invalidate
  MUTEX.synchronize { @cache = {} }
  nil
end