mbeditor 0.12.1 → 0.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1072,6 +1072,19 @@ module Mbeditor
1072
1072
  }
1073
1073
  end
1074
1074
 
1075
+ # GET /mbeditor/routes?path=app/controllers/users_controller.rb
1076
+ #
1077
+ # Which routes reach each action in a controller, for the inline hints the
1078
+ # editor draws beside every `def`. Returns {} for anything that is not a
1079
+ # controller rather than erroring — the client asks for every Ruby file it
1080
+ # opens and should not have to know the convention.
1081
+ def routes
1082
+ key = RouteService.controller_key(params[:path].to_s)
1083
+ return render json: { controller: nil, actions: {} } unless key
1084
+
1085
+ render json: { controller: key, actions: RouteService.for_controller(key) }
1086
+ end
1087
+
1075
1088
  # GET /mbeditor/related_files?path=...
1076
1089
  def related_files
1077
1090
  path = resolve_path(params[:path])
@@ -22,7 +22,16 @@ module Mbeditor
22
22
  SolidQueue SolidCache SolidCable
23
23
  ].freeze
24
24
 
25
- MAX_MODELS = 300
25
+ # Cap on models drawn. 300 was chosen before the layout could handle that
26
+ # many; a schema slightly over it silently lost models and only said
27
+ # "(truncated)". Raised, and configurable for anything larger — the cost of
28
+ # a bigger graph is now the browser's rendering, not the layout.
29
+ DEFAULT_MAX_MODELS = 1000
30
+
31
+ def self.max_models
32
+ value = Mbeditor.configuration.model_graph_max_models.to_i
33
+ value.positive? ? value : DEFAULT_MAX_MODELS
34
+ end
26
35
 
27
36
  # Only the first few columns travel: the diagram box shows that many and the
28
37
  # full list is a click away in the schema modal, which fetches its own data
@@ -86,7 +95,8 @@ module Mbeditor
86
95
  classes = model_classes
87
96
  return unavailable("No ActiveRecord models found in this application.") if classes.empty?
88
97
 
89
- models = classes.first(MAX_MODELS).map { |klass| describe_model(klass, root) }
98
+ limit = max_models
99
+ models = classes.first(limit).map { |klass| describe_model(klass, root) }
90
100
  known = models.map { |m| m[:name] }.to_set
91
101
 
92
102
  {
@@ -95,7 +105,7 @@ module Mbeditor
95
105
  # Edges to classes outside the graph (a gem's model, or a typo in
96
106
  # class_name:) would render as arrows into nowhere.
97
107
  edges: models.flat_map { |m| m.delete(:edges) }.select { |e| known.include?(e[:to]) }.uniq,
98
- truncated: classes.length > MAX_MODELS,
108
+ truncated: classes.length > limit,
99
109
  generatedAt: Time.now.utc.iso8601
100
110
  }
101
111
  rescue StandardError => e
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mbeditor
4
+ # Which routes reach a controller's actions, for the inline hints shown beside
5
+ # each `def` in a controller file.
6
+ #
7
+ # Read from the host app's own route set rather than by parsing config/routes.rb.
8
+ # mbeditor runs inside the host app, so the routes are already built — and they
9
+ # are the only source that accounts for `resources` expansion, `member`/
10
+ # `collection` blocks, scopes, constraints, mounted engines and anything a
11
+ # routes file does in plain Ruby. Parsing the file would get all of that wrong.
12
+ module RouteService
13
+ module_function
14
+
15
+ # Actions with no route are worth calling out, but only for controllers Rails
16
+ # would actually dispatch to. These are the inherited ones every controller
17
+ # has and nobody routes.
18
+ NON_ACTION_METHODS = %w[
19
+ new inspect method_of to_s hash class dup freeze
20
+ ].freeze
21
+
22
+ # "app/controllers/admin/users_controller.rb" -> "admin/users", which is the
23
+ # key Rails stores in a route's defaults.
24
+ def controller_key(relative_path)
25
+ path = relative_path.to_s.sub(%r{\A/+}, "")
26
+ match = path.match(%r{\Aapp/controllers/(.+)_controller\.rb\z})
27
+ return nil unless match
28
+
29
+ match[1]
30
+ end
31
+
32
+ # => { "show" => [{ verb:, path:, name: }], ... }
33
+ def for_controller(key)
34
+ return {} if key.nil? || key.empty?
35
+ return {} unless defined?(Rails) && Rails.respond_to?(:application) && Rails.application
36
+
37
+ routes_for(key)
38
+ rescue StandardError
39
+ # A broken route set must not take the editor down with it — the file still
40
+ # opens, just without hints.
41
+ {}
42
+ end
43
+
44
+ def routes_for(key)
45
+ out = {}
46
+ Rails.application.routes.routes.each do |route|
47
+ defaults = route.defaults
48
+ next unless defaults[:controller] == key
49
+
50
+ action = defaults[:action].to_s
51
+ next if action.empty?
52
+
53
+ (out[action] ||= []) << {
54
+ verb: verb_for(route),
55
+ path: path_for(route),
56
+ name: route.name
57
+ }
58
+ end
59
+ out
60
+ end
61
+ private_class_method :routes_for
62
+
63
+ # Rails has moved this between a String and a Regexp across versions; both
64
+ # stringify usefully, but a Regexp needs its slashes stripped.
65
+ def verb_for(route)
66
+ verb = route.verb
67
+ verb = verb.source.gsub(%r{[$^]}, "") if verb.is_a?(Regexp)
68
+ verb = verb.to_s
69
+ verb.empty? ? "ANY" : verb
70
+ end
71
+ private_class_method :verb_for
72
+
73
+ # "(.:format)" is noise in a hint that has to fit on one line.
74
+ def path_for(route)
75
+ route.path.spec.to_s.sub(/\(\.:format\)\z/, "")
76
+ end
77
+ private_class_method :path_for
78
+ end
79
+ end
@@ -5,14 +5,14 @@ module Mbeditor
5
5
  attr_accessor :allowed_environments, :workspace_root, :excluded_paths, :rubocop_command, :rubocop_server,
6
6
  :redmine_enabled, :redmine_url, :redmine_api_key, :redmine_ticket_source,
7
7
  :test_framework, :test_command, :test_timeout,
8
- :authenticate_with, :authentication_cache_ttl, :user_name_callback, :user_name_methods,
8
+ :authenticate_with, :cable_authenticate_with, :authentication_cache_ttl, :user_name_callback, :user_name_methods,
9
9
  :lint_timeout, :base_branch_candidates, :git_timeout, :search_timeout,
10
10
  :ruby_def_include_dirs, :related_files_custom_paths,
11
11
  :mount_path, :resilient_routing, :js_global_identifiers,
12
12
  :js_program, :js_program_exclude,
13
13
  :js_syntax_check, :babel_standalone_path,
14
14
  :ruby_lsp, :ruby_lsp_command, :ruby_lsp_timeout,
15
- :exception_capture,
15
+ :exception_capture, :model_graph_max_models,
16
16
  :search_respect_gitignore, :ripgrep_command
17
17
 
18
18
  def initialize
@@ -58,6 +58,16 @@ module Mbeditor
58
58
  @ruby_def_include_dirs = %w[app/models app/controllers app/helpers app/concerns]
59
59
  @related_files_custom_paths = []
60
60
  @authentication_cache_ttl = 0
61
+ # Authentication for the collaboration WebSocket. nil falls back to
62
+ # authenticate_with.
63
+ #
64
+ # Exists because the two contexts genuinely differ: a WebSocket subscribe
65
+ # runs no controller, so anything a before_action populates —
66
+ # ActiveSupport::CurrentAttributes, an Authlogic session, a memoised
67
+ # current_user — is nil or raises here, and the hook then denies the socket
68
+ # while working perfectly over HTTP. Set this to a proc that resolves the
69
+ # user from `session` directly when the HTTP hook cannot.
70
+ @cable_authenticate_with = nil
61
71
  @user_name_callback = nil # proc resolved in controller context (instance_exec) → collaboration display name; nil falls through to current_user, then to the client-generated name
62
72
  # Attributes tried on current_user, in order, when no user_name_callback
63
73
  # is set. First non-blank one wins. Name your own column here rather than
@@ -84,6 +94,9 @@ module Mbeditor
84
94
  # same exposure the log panel already has, since it tails the dev log.
85
95
  # Set to false to record nothing.
86
96
  @exception_capture = :auto
97
+ # Models drawn in the model graph before it reports itself truncated.
98
+ # nil uses ModelGraphService::DEFAULT_MAX_MODELS.
99
+ @model_graph_max_models = nil
87
100
  @mount_path = nil # explicit URL prefix override; nil falls through to detection/"/mbeditor"
88
101
  @resilient_routing = true # serve /mbeditor from middleware so the editor survives a broken host routes.rb; false is the escape hatch
89
102
  end
@@ -54,9 +54,23 @@ module Mbeditor
54
54
  # Insert before CheckPending so our middleware wraps it and can rescue
55
55
  # the error it raises. Falls back silently if CheckPending is absent
56
56
  # (e.g. host app does not use ActiveRecord).
57
- # Note: app.middleware is a MiddlewareStackProxy during initializers and
58
- # does not support .to_a rely solely on defined? to detect ActiveRecord.
59
- if defined?(ActiveRecord::Migration::CheckPending)
57
+ #
58
+ # The constant being defined is not enough: Rails only puts CheckPending
59
+ # in the stack when config.active_record.migration_error is :page_load, so
60
+ # an app that loads ActiveRecord with any other setting has the constant
61
+ # but no middleware — and insert_before then raises "No such middleware",
62
+ # taking the host app's boot down with it. app.middleware is a
63
+ # MiddlewareStackProxy here and cannot be inspected, so the only way to
64
+ # know is to try it. Losing this middleware costs a friendlier pending-
65
+ # migration page, which is never worth failing to boot over.
66
+ # Rescuing the call is not an option: app.middleware is a
67
+ # MiddlewareStackProxy, so insert_before only records the operation and the
68
+ # "No such middleware" error surfaces later, during merge_into. Test the
69
+ # same condition Rails uses to add CheckPending in the first place.
70
+ migration_error = app.config.respond_to?(:active_record) &&
71
+ app.config.active_record[:migration_error]
72
+
73
+ if defined?(ActiveRecord::Migration::CheckPending) && migration_error == :page_load
60
74
  app.middleware.insert_before ActiveRecord::Migration::CheckPending,
61
75
  Mbeditor::Rack::HandlePendingMigrations
62
76
  end
@@ -44,6 +44,7 @@ module Mbeditor
44
44
  get 'client_config', to: 'editors#client_config'
45
45
  get 'related_files', to: 'editors#related_files'
46
46
  get 'model_schema', to: 'editors#model_schema'
47
+ get 'routes', to: 'editors#routes'
47
48
  get 'changelog', to: 'editors#changelog'
48
49
  get 'git_info', to: 'editors#git_info'
49
50
  get 'git_status', to: 'editors#git_status'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mbeditor
4
- VERSION = "0.12.1"
4
+ VERSION = "0.12.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbeditor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.1
4
+ version: 0.12.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Noonan
@@ -133,6 +133,7 @@ files:
133
133
  - app/services/mbeditor/rails_related_files_service.rb
134
134
  - app/services/mbeditor/redmine_service.rb
135
135
  - app/services/mbeditor/ri_definition_service.rb
136
+ - app/services/mbeditor/route_service.rb
136
137
  - app/services/mbeditor/ruby_definition_service.rb
137
138
  - app/services/mbeditor/safe_path.rb
138
139
  - app/services/mbeditor/schema_service.rb