rails-ai-context 5.30.2 → 5.32.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +868 -0
- data/CONTEXT.md +7 -3
- data/README.md +1 -1
- data/docs/ARCHITECTURE.md +3 -3
- data/docs/CLI.md +1 -1
- data/docs/COMPATIBILITY.md +50 -14
- data/docs/CONFIGURATION.md +4 -1
- data/docs/FAQ.md +1 -1
- data/docs/GUIDE.md +14 -8
- data/docs/INTROSPECTORS.md +50 -23
- data/docs/SECURITY.md +56 -1
- data/docs/TOOLS.md +58 -12
- data/lib/generators/rails_ai_context/install/install_generator.rb +1 -0
- data/lib/rails_ai_context/action_filters.rb +121 -48
- data/lib/rails_ai_context/app_commands.rb +2 -2
- data/lib/rails_ai_context/app_kind.rb +65 -8
- data/lib/rails_ai_context/ast_cache.rb +53 -18
- data/lib/rails_ai_context/base_mixins.rb +46 -16
- data/lib/rails_ai_context/boot_manager.rb +5 -1
- data/lib/rails_ai_context/cli/entry_boot.rb +17 -2
- data/lib/rails_ai_context/cli/tool_runner.rb +15 -1
- data/lib/rails_ai_context/concern_macros.rb +791 -111
- data/lib/rails_ai_context/concern_membership.rb +25 -5
- data/lib/rails_ai_context/concern_paths.rb +63 -17
- data/lib/rails_ai_context/config_yaml.rb +40 -0
- data/lib/rails_ai_context/configuration.rb +3 -3
- data/lib/rails_ai_context/data/docs/index.json +324 -0
- data/lib/rails_ai_context/database_yml.rb +136 -0
- data/lib/rails_ai_context/doctor.rb +30 -37
- data/lib/rails_ai_context/erb_source.rb +40 -0
- data/lib/rails_ai_context/fingerprinter.rb +48 -22
- data/lib/rails_ai_context/fixture_keys.rb +42 -1
- data/lib/rails_ai_context/gem_lock.rb +275 -47
- data/lib/rails_ai_context/git_ignore.rb +13 -11
- data/lib/rails_ai_context/hydrators/hydration_formatter.rb +8 -2
- data/lib/rails_ai_context/hydrators/schema_hint_builder.rb +10 -5
- data/lib/rails_ai_context/introspector.rb +10 -1
- data/lib/rails_ai_context/introspectors/action_mailbox_introspector.rb +27 -19
- data/lib/rails_ai_context/introspectors/action_presence.rb +11 -8
- data/lib/rails_ai_context/introspectors/action_resolver.rb +38 -26
- data/lib/rails_ai_context/introspectors/action_text_introspector.rb +2 -2
- data/lib/rails_ai_context/introspectors/active_record_settings.rb +56 -0
- data/lib/rails_ai_context/introspectors/active_storage_introspector.rb +1 -1
- data/lib/rails_ai_context/introspectors/active_support_introspector.rb +98 -22
- data/lib/rails_ai_context/introspectors/admin_resources.rb +101 -0
- data/lib/rails_ai_context/introspectors/anyway_configs.rb +64 -0
- data/lib/rails_ai_context/introspectors/apartment_config.rb +47 -0
- data/lib/rails_ai_context/introspectors/api_introspector.rb +125 -26
- data/lib/rails_ai_context/introspectors/asset_pipeline_introspector.rb +24 -19
- data/lib/rails_ai_context/introspectors/ast_walk.rb +18 -0
- data/lib/rails_ai_context/introspectors/auth_introspector.rb +71 -18
- data/lib/rails_ai_context/introspectors/autoload_introspector.rb +62 -15
- data/lib/rails_ai_context/introspectors/base.rb +38 -0
- data/lib/rails_ai_context/introspectors/call_site_expansion.rb +349 -39
- data/lib/rails_ai_context/introspectors/component_introspector.rb +26 -14
- data/lib/rails_ai_context/introspectors/config_introspector.rb +22 -9
- data/lib/rails_ai_context/introspectors/connection_pool_introspector.rb +1 -4
- data/lib/rails_ai_context/introspectors/controller_filters.rb +335 -31
- data/lib/rails_ai_context/introspectors/controller_introspector.rb +315 -129
- data/lib/rails_ai_context/introspectors/controller_settings.rb +182 -0
- data/lib/rails_ai_context/introspectors/convention_introspector.rb +18 -15
- data/lib/rails_ai_context/introspectors/database_stats_introspector.rb +1 -1
- data/lib/rails_ai_context/introspectors/declared_constant.rb +108 -23
- data/lib/rails_ai_context/introspectors/eager_load.rb +12 -7
- data/lib/rails_ai_context/introspectors/engine_introspector.rb +13 -5
- data/lib/rails_ai_context/introspectors/env_config_introspector.rb +62 -17
- data/lib/rails_ai_context/introspectors/env_introspector.rb +8 -6
- data/lib/rails_ai_context/introspectors/env_references.rb +21 -4
- data/lib/rails_ai_context/introspectors/frontend_framework_introspector.rb +37 -22
- data/lib/rails_ai_context/introspectors/gem_introspector.rb +3 -1
- data/lib/rails_ai_context/introspectors/gemfile_gems.rb +53 -5
- data/lib/rails_ai_context/introspectors/grape_endpoints.rb +109 -0
- data/lib/rails_ai_context/introspectors/helper_names.rb +12 -1
- data/lib/rails_ai_context/introspectors/i18n_introspector.rb +101 -46
- data/lib/rails_ai_context/introspectors/includers.rb +6 -1
- data/lib/rails_ai_context/introspectors/initializer_introspector.rb +2 -5
- data/lib/rails_ai_context/introspectors/job_introspector.rb +548 -103
- data/lib/rails_ai_context/introspectors/listener_registration.rb +11 -1
- data/lib/rails_ai_context/introspectors/listeners/associations_listener.rb +51 -4
- data/lib/rails_ai_context/introspectors/listeners/autoload_ignore_listener.rb +31 -0
- data/lib/rails_ai_context/introspectors/listeners/autoload_paths_listener.rb +4 -47
- data/lib/rails_ai_context/introspectors/listeners/base_listener.rb +80 -14
- data/lib/rails_ai_context/introspectors/listeners/branch_conditions.rb +156 -0
- data/lib/rails_ai_context/introspectors/listeners/callbacks_listener.rb +85 -23
- data/lib/rails_ai_context/introspectors/listeners/class_definition_listener.rb +35 -6
- data/lib/rails_ai_context/introspectors/listeners/component_structure_listener.rb +30 -2
- data/lib/rails_ai_context/introspectors/listeners/conditional_macro_listener.rb +19 -0
- data/lib/rails_ai_context/introspectors/listeners/config_assignment_listener.rb +255 -10
- data/lib/rails_ai_context/introspectors/listeners/constant_reference_listener.rb +34 -0
- data/lib/rails_ai_context/introspectors/listeners/constructor_macro_listener.rb +123 -0
- data/lib/rails_ai_context/introspectors/listeners/definition_file_paths_listener.rb +66 -0
- data/lib/rails_ai_context/introspectors/listeners/enums_listener.rb +2 -2
- data/lib/rails_ai_context/introspectors/listeners/env_access_listener.rb +31 -0
- data/lib/rails_ai_context/introspectors/listeners/filter_macro_listener.rb +54 -0
- data/lib/rails_ai_context/introspectors/listeners/fixture_paths_listener.rb +62 -0
- data/lib/rails_ai_context/introspectors/listeners/gemfile_dsl_listener.rb +42 -1
- data/lib/rails_ai_context/introspectors/listeners/generic_macro_listener.rb +28 -2
- data/lib/rails_ai_context/introspectors/listeners/grape_api_listener.rb +137 -0
- data/lib/rails_ai_context/introspectors/listeners/http_client_call_listener.rb +50 -0
- data/lib/rails_ai_context/introspectors/listeners/i18n_load_path_listener.rb +54 -0
- data/lib/rails_ai_context/introspectors/listeners/literal_paths.rb +123 -0
- data/lib/rails_ai_context/introspectors/listeners/macros_listener.rb +248 -4
- data/lib/rails_ai_context/introspectors/listeners/mailbox_routing_listener.rb +2 -1
- data/lib/rails_ai_context/introspectors/listeners/method_call_listener.rb +7 -1
- data/lib/rails_ai_context/introspectors/listeners/methods_listener.rb +352 -69
- data/lib/rails_ai_context/introspectors/listeners/migration_dsl_listener.rb +1 -1
- data/lib/rails_ai_context/introspectors/listeners/migration_replay_listener.rb +35 -26
- data/lib/rails_ai_context/introspectors/listeners/mixins_listener.rb +62 -29
- data/lib/rails_ai_context/introspectors/listeners/mount_listener.rb +8 -0
- data/lib/rails_ai_context/introspectors/listeners/namespaced_roots_listener.rb +35 -0
- data/lib/rails_ai_context/introspectors/listeners/nested_constants_listener.rb +32 -0
- data/lib/rails_ai_context/introspectors/listeners/owner_scope.rb +67 -0
- data/lib/rails_ai_context/introspectors/listeners/preview_paths_listener.rb +16 -7
- data/lib/rails_ai_context/introspectors/listeners/proc_literal_listener.rb +42 -0
- data/lib/rails_ai_context/introspectors/listeners/queue_assignment_listener.rb +54 -0
- data/lib/rails_ai_context/introspectors/listeners/rake_task_dsl_listener.rb +21 -8
- data/lib/rails_ai_context/introspectors/listeners/route_files_listener.rb +4 -11
- data/lib/rails_ai_context/introspectors/listeners/routes_dsl_listener.rb +461 -65
- data/lib/rails_ai_context/introspectors/listeners/schema_dsl_listener.rb +76 -14
- data/lib/rails_ai_context/introspectors/listeners/scopes_listener.rb +40 -1
- data/lib/rails_ai_context/introspectors/listeners/validations_listener.rb +10 -1
- data/lib/rails_ai_context/introspectors/listeners/view_paths_listener.rb +37 -0
- data/lib/rails_ai_context/introspectors/middleware_introspector.rb +47 -3
- data/lib/rails_ai_context/introspectors/migration_introspector.rb +3 -3
- data/lib/rails_ai_context/introspectors/migration_replay/helpers.rb +4 -3
- data/lib/rails_ai_context/introspectors/migration_replay/statements.rb +72 -12
- data/lib/rails_ai_context/introspectors/migration_replay.rb +110 -14
- data/lib/rails_ai_context/introspectors/model_introspector.rb +708 -214
- data/lib/rails_ai_context/introspectors/module_aliases.rb +11 -3
- data/lib/rails_ai_context/introspectors/multi_database_introspector.rb +9 -57
- data/lib/rails_ai_context/introspectors/observability_introspector.rb +5 -8
- data/lib/rails_ai_context/introspectors/performance_introspector.rb +28 -8
- data/lib/rails_ai_context/introspectors/rake_task_introspector.rb +118 -51
- data/lib/rails_ai_context/introspectors/recurring_schedules.rb +150 -0
- data/lib/rails_ai_context/introspectors/rendered_record.rb +87 -0
- data/lib/rails_ai_context/introspectors/retry_policy.rb +8 -5
- data/lib/rails_ai_context/introspectors/route_introspector.rb +153 -42
- data/lib/rails_ai_context/introspectors/schema_conventions.rb +127 -40
- data/lib/rails_ai_context/introspectors/schema_dump_path.rb +80 -0
- data/lib/rails_ai_context/introspectors/schema_introspector.rb +346 -112
- data/lib/rails_ai_context/introspectors/schema_reader.rb +69 -29
- data/lib/rails_ai_context/introspectors/service_classes.rb +21 -15
- data/lib/rails_ai_context/introspectors/source_calls.rb +1 -1
- data/lib/rails_ai_context/introspectors/source_introspector.rb +17 -2
- data/lib/rails_ai_context/introspectors/source_scan.rb +174 -26
- data/lib/rails_ai_context/introspectors/sqlite_virtual_tables.rb +28 -0
- data/lib/rails_ai_context/introspectors/stimulus_introspector.rb +16 -5
- data/lib/rails_ai_context/introspectors/structure_sql_reader.rb +372 -98
- data/lib/rails_ai_context/introspectors/superclass_chain.rb +14 -15
- data/lib/rails_ai_context/introspectors/table_name.rb +46 -45
- data/lib/rails_ai_context/introspectors/test_introspector.rb +289 -78
- data/lib/rails_ai_context/introspectors/turbo_introspector.rb +10 -5
- data/lib/rails_ai_context/introspectors/view_introspector.rb +5 -40
- data/lib/rails_ai_context/introspectors/view_template_introspector.rb +12 -10
- data/lib/rails_ai_context/method_name.rb +31 -0
- data/lib/rails_ai_context/migration_status.rb +5 -5
- data/lib/rails_ai_context/output_guard.rb +6 -4
- data/lib/rails_ai_context/package_json.rb +96 -5
- data/lib/rails_ai_context/path_resolver.rb +170 -15
- data/lib/rails_ai_context/payload.rb +48 -0
- data/lib/rails_ai_context/pending_migrations.rb +15 -18
- data/lib/rails_ai_context/portable_path.rb +76 -4
- data/lib/rails_ai_context/redaction.rb +8 -2
- data/lib/rails_ai_context/route_coverage.rb +5 -1
- data/lib/rails_ai_context/safe_file.rb +4 -1
- data/lib/rails_ai_context/safe_path.rb +61 -10
- data/lib/rails_ai_context/schema_hint.rb +8 -2
- data/lib/rails_ai_context/schema_version.rb +7 -9
- data/lib/rails_ai_context/serializers/claude_rules_serializer.rb +1 -1
- data/lib/rails_ai_context/serializers/markdown_serializer.rb +13 -4
- data/lib/rails_ai_context/serializers/section_facts.rb +5 -1
- data/lib/rails_ai_context/serializers/stack_overview_helper.rb +2 -2
- data/lib/rails_ai_context/serializers/tool_guide_helper.rb +7 -4
- data/lib/rails_ai_context/server.rb +26 -7
- data/lib/rails_ai_context/test_framework.rb +32 -5
- data/lib/rails_ai_context/tools/analyze_feature.rb +22 -10
- data/lib/rails_ai_context/tools/base_tool.rb +69 -30
- data/lib/rails_ai_context/tools/diagnose.rb +1 -1
- data/lib/rails_ai_context/tools/generate_test.rb +8 -9
- data/lib/rails_ai_context/tools/get_active_support.rb +19 -4
- data/lib/rails_ai_context/tools/get_api.rb +17 -11
- data/lib/rails_ai_context/tools/get_autoload.rb +10 -5
- data/lib/rails_ai_context/tools/get_callbacks.rb +65 -14
- data/lib/rails_ai_context/tools/get_component_catalog.rb +7 -4
- data/lib/rails_ai_context/tools/get_concern.rb +31 -18
- data/lib/rails_ai_context/tools/get_config.rb +52 -18
- data/lib/rails_ai_context/tools/get_controllers.rb +57 -13
- data/lib/rails_ai_context/tools/get_conventions.rb +44 -22
- data/lib/rails_ai_context/tools/get_engines.rb +2 -0
- data/lib/rails_ai_context/tools/get_env.rb +209 -52
- data/lib/rails_ai_context/tools/get_env_config.rb +38 -7
- data/lib/rails_ai_context/tools/get_frontend_stack.rb +56 -7
- data/lib/rails_ai_context/tools/get_helper_methods.rb +136 -26
- data/lib/rails_ai_context/tools/get_job_pattern.rb +143 -87
- data/lib/rails_ai_context/tools/get_mailers.rb +59 -2
- data/lib/rails_ai_context/tools/get_model_details.rb +178 -28
- data/lib/rails_ai_context/tools/get_partial_interface.rb +101 -62
- data/lib/rails_ai_context/tools/get_routes.rb +35 -9
- data/lib/rails_ai_context/tools/get_schema.rb +160 -61
- data/lib/rails_ai_context/tools/get_service_pattern.rb +121 -25
- data/lib/rails_ai_context/tools/get_stimulus.rb +1 -1
- data/lib/rails_ai_context/tools/get_test_info.rb +152 -82
- data/lib/rails_ai_context/tools/get_view.rb +37 -20
- data/lib/rails_ai_context/tools/migration_advisor.rb +64 -24
- data/lib/rails_ai_context/tools/onboard.rb +174 -88
- data/lib/rails_ai_context/tools/performance_check.rb +5 -13
- data/lib/rails_ai_context/tools/query.rb +122 -28
- data/lib/rails_ai_context/tools/read_logs.rb +45 -17
- data/lib/rails_ai_context/tools/runtime_info.rb +1 -1
- data/lib/rails_ai_context/tools/safe_call.rb +20 -15
- data/lib/rails_ai_context/tools/search_code.rb +114 -40
- data/lib/rails_ai_context/tools/search_docs.rb +22 -11
- data/lib/rails_ai_context/tools/security_scan.rb +51 -4
- data/lib/rails_ai_context/tools/validate.rb +21 -4
- data/lib/rails_ai_context/tools/validate_semantics.rb +18 -6
- data/lib/rails_ai_context/version.rb +1 -1
- data/lib/rails_ai_context/vfs.rb +2 -2
- data/lib/rails_ai_context/view_file.rb +53 -3
- data/lib/rails_ai_context.rb +10 -2
- data/server.json +1 -1
- metadata +34 -3
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "pathname"
|
|
4
|
+
require "set"
|
|
5
|
+
|
|
3
6
|
module RailsAiContext
|
|
4
7
|
module Introspectors
|
|
5
8
|
# Discovers controllers and extracts filters, strong params,
|
|
@@ -38,7 +41,7 @@ module RailsAiContext
|
|
|
38
41
|
result[name] = details
|
|
39
42
|
end
|
|
40
43
|
|
|
41
|
-
{ controllers: fill_inherited_actions(result) }
|
|
44
|
+
{ controllers: inherit_class_declarations(fill_inherited_actions(result)) }
|
|
42
45
|
end
|
|
43
46
|
|
|
44
47
|
# Static tier: every controller goes through the source-only extractor;
|
|
@@ -54,10 +57,13 @@ module RailsAiContext
|
|
|
54
57
|
rescue => e
|
|
55
58
|
hash[path_name] = { error: portable_message(e) }
|
|
56
59
|
end
|
|
60
|
+
engine = PathResolver.bundle_engine_root(root)
|
|
61
|
+
unread = Pathname.new(engine).relative_path_from(Pathname.new(File.expand_path(root))).to_s if engine
|
|
57
62
|
{
|
|
58
|
-
controllers: fill_inherited_actions(result),
|
|
59
|
-
note: "Parsed statically from app/controllers (app not booted)"
|
|
60
|
-
|
|
63
|
+
controllers: inherit_class_declarations(fill_inherited_actions(result)),
|
|
64
|
+
note: "Parsed statically from app/controllers (app not booted)#{"; the engine at #{unread} is not read unbooted" if unread}",
|
|
65
|
+
unread_engine: unread
|
|
66
|
+
}.compact
|
|
61
67
|
end
|
|
62
68
|
|
|
63
69
|
# Rails renders a routed action's template when the controller has no method for it,
|
|
@@ -149,12 +155,60 @@ module RailsAiContext
|
|
|
149
155
|
result
|
|
150
156
|
end
|
|
151
157
|
|
|
158
|
+
# Class-level declarations Rails hands down: responders' respond_to copies the formats the
|
|
159
|
+
# parent set before adding its own, and a rate_limit is a before_action every subclass runs.
|
|
160
|
+
# Folded over the listing as it was, with the bases it leaves out read from their files.
|
|
161
|
+
def inherit_class_declarations(result)
|
|
162
|
+
own = @class_declarations || {}
|
|
163
|
+
folded = result.filter_map do |name, info|
|
|
164
|
+
next unless info.is_a?(Hash) && own.key?(name)
|
|
165
|
+
|
|
166
|
+
links, = ControllerSettings.lineage(result, name, app.root.to_s)
|
|
167
|
+
lineage = links.reverse.filter_map do |link, entry, source|
|
|
168
|
+
declared = entry ? own[link] : base_class_declarations(source)
|
|
169
|
+
[ link, declared ] if declared
|
|
170
|
+
end
|
|
171
|
+
formats = lineage.each_with_object(Set.new) { |(_, declared), set| fold_formats(set, declared[:formats]) }
|
|
172
|
+
limits = lineage.flat_map { |link, declared| link == name ? declared[:rate_limits] : declared[:rate_limits].map { |limit| limit.merge(from: link) } }
|
|
173
|
+
[ name, (formats.to_a | own[name][:block_formats]).sort, limits ]
|
|
174
|
+
end
|
|
175
|
+
folded.each do |name, formats, limits|
|
|
176
|
+
result[name][:respond_to_formats] = formats
|
|
177
|
+
limits.any? ? result[name][:rate_limits] = limits : result[name].delete(:rate_limits)
|
|
178
|
+
end
|
|
179
|
+
result
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# `clear_respond_to` empties the class attribute; each `respond_to` adds to it.
|
|
183
|
+
def fold_formats(set, calls)
|
|
184
|
+
calls.each { |formats| formats ? set.merge(formats) : set.clear }
|
|
185
|
+
set
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def own_class_declarations(name, source, walked)
|
|
189
|
+
declared = class_declarations(source, walked).merge(block_formats: block_formats(source, walked))
|
|
190
|
+
(@class_declarations ||= {})[name] = declared
|
|
191
|
+
declared.merge(respond_to_formats: (fold_formats(Set.new, declared[:formats]).to_a | declared[:block_formats]).sort)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# A base the listing leaves out is read once per run however many controllers inherit it.
|
|
195
|
+
def base_class_declarations(source)
|
|
196
|
+
(@base_class_declarations ||= {})[source] ||= class_declarations(source, class_body_walk(source))
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# { formats: [[format...] per respond_to, nil per clear_respond_to], rate_limits: [...] }
|
|
200
|
+
def class_declarations(source, walked)
|
|
201
|
+
calls = walked ? SourceIntrospector.class_level(walked[:respond_to], walked) : []
|
|
202
|
+
formats = calls.map { |call| call[:macro] == :clear_respond_to ? nil : Array(call[:args]).map(&:to_s) }
|
|
203
|
+
{ formats: formats, rate_limits: extract_rate_limits(source, walked) }
|
|
204
|
+
end
|
|
205
|
+
|
|
152
206
|
# What both tiers do with a file: read it, name it by what it declares,
|
|
153
207
|
# and extract. A file it cannot read is an entry saying so, not a gap.
|
|
154
208
|
def detail_for(record, path_name)
|
|
155
209
|
source = SafeFile.read(record.path)
|
|
156
210
|
return [ path_name, { error: "unreadable" } ] unless source
|
|
157
|
-
return nil unless record.path.end_with?("_controller.rb") || subclasses_a_controller?(source)
|
|
211
|
+
return nil unless record.path.end_with?("_controller.rb") || subclasses_a_controller?(source, path_name)
|
|
158
212
|
|
|
159
213
|
name = DeclaredConstant.resolve(source, path_name)
|
|
160
214
|
[ name, extract_details_from_source(record, name, source) ]
|
|
@@ -162,8 +216,8 @@ module RailsAiContext
|
|
|
162
216
|
|
|
163
217
|
# A file not named *_controller.rb is a controller only if it subclasses one: a mixin,
|
|
164
218
|
# a plain helper class or a Grape API is not.
|
|
165
|
-
def subclasses_a_controller?(source)
|
|
166
|
-
DeclaredConstant.declarations(source).any? { |d| d.superclass.to_s.split("::").last.to_s.end_with?("Controller") }
|
|
219
|
+
def subclasses_a_controller?(source, path_name)
|
|
220
|
+
DeclaredConstant.declarations(source, path_name: path_name).any? { |d| d.superclass.to_s.split("::").last.to_s.end_with?("Controller") }
|
|
167
221
|
end
|
|
168
222
|
|
|
169
223
|
def discover_controllers
|
|
@@ -199,27 +253,30 @@ module RailsAiContext
|
|
|
199
253
|
# Carry the file that was read: the declared name does not round-trip
|
|
200
254
|
# back to a path. See CONTEXT.md, "Declared constant".
|
|
201
255
|
relative_file = record.file
|
|
202
|
-
|
|
203
|
-
|
|
256
|
+
declaration = parent_declaration(source, class_name)
|
|
257
|
+
parent = declaration&.superclass || "Unknown"
|
|
204
258
|
filters, unread = ControllerFilters.with_concerns(source, root: app.root.to_s, within: class_name,
|
|
205
259
|
cache: (@concern_cache ||= {}))
|
|
206
260
|
concerns = extract_concerns_from_source(source)
|
|
261
|
+
walked = class_body_walk(source)
|
|
262
|
+
declared = own_class_declarations(class_name, source, walked)
|
|
207
263
|
own = ActionResolver.actions_from_source(source, class_name: class_name, filters: filter_names(filters))
|
|
208
264
|
mixed_in = concern_actions(concerns, class_name, filters) - own
|
|
209
265
|
details = {
|
|
210
266
|
parent_class: parent,
|
|
267
|
+
parent_nesting: odd_nesting(class_name, declaration&.nesting),
|
|
211
268
|
api_controller: parent.include?("API"),
|
|
212
269
|
actions: (own + mixed_in).sort,
|
|
213
270
|
inherited_actions: mixed_in.presence,
|
|
214
271
|
filters: filters,
|
|
215
272
|
concerns: concerns,
|
|
216
273
|
concerns_unread: unread.presence,
|
|
217
|
-
strong_params: extract_strong_params(source),
|
|
218
|
-
respond_to_formats:
|
|
219
|
-
rescue_from: extract_rescue_from(source),
|
|
220
|
-
|
|
221
|
-
rate_limit_parsed: parse_rate_limit(rate_limit),
|
|
274
|
+
strong_params: extract_strong_params(source, walked),
|
|
275
|
+
respond_to_formats: declared[:respond_to_formats],
|
|
276
|
+
rescue_from: extract_rescue_from(source, walked),
|
|
277
|
+
rate_limits: declared[:rate_limits].presence,
|
|
222
278
|
turbo_stream_actions: extract_turbo_stream_actions(source),
|
|
279
|
+
**ControllerSettings.from_source(source, walked, root: app.root.to_s, within: class_name),
|
|
223
280
|
file: relative_file
|
|
224
281
|
}.compact
|
|
225
282
|
details
|
|
@@ -234,27 +291,29 @@ module RailsAiContext
|
|
|
234
291
|
|
|
235
292
|
def extract_controller_details(ctrl)
|
|
236
293
|
source = read_source(ctrl)
|
|
237
|
-
|
|
238
|
-
filters = extract_filters(ctrl, source)
|
|
294
|
+
filters = from_unread_mixins(ctrl, extract_filters(ctrl, source))
|
|
239
295
|
concerns = extract_concerns(ctrl)
|
|
240
296
|
actions = extract_actions(ctrl, source, filters) | concern_actions(concerns, ctrl.name, filters)
|
|
241
297
|
# What the file does not define itself is inherited or mixed in, for
|
|
242
298
|
# the routes to settle as they do for a statically read entry.
|
|
243
299
|
own = source ? ActionResolver.actions_from_source(source, class_name: ctrl.name, filters: filter_names(filters)) : actions
|
|
300
|
+
walked = class_body_walk(source)
|
|
301
|
+
declared = own_class_declarations(ctrl.name, source, walked)
|
|
244
302
|
|
|
245
303
|
{
|
|
246
304
|
parent_class: ctrl.superclass.name,
|
|
305
|
+
parent_nesting: booted_parent_nesting(ctrl),
|
|
247
306
|
api_controller: api_controller?(ctrl),
|
|
248
307
|
actions: actions.sort,
|
|
249
308
|
inherited_actions: (actions - own).presence,
|
|
250
309
|
filters: filters,
|
|
251
310
|
concerns: concerns,
|
|
252
|
-
strong_params: extract_strong_params(source),
|
|
253
|
-
respond_to_formats:
|
|
254
|
-
rescue_from: extract_rescue_from(source),
|
|
255
|
-
|
|
256
|
-
rate_limit_parsed: parse_rate_limit(rate_limit),
|
|
311
|
+
strong_params: extract_strong_params(source, walked),
|
|
312
|
+
respond_to_formats: declared[:respond_to_formats],
|
|
313
|
+
rescue_from: extract_rescue_from(source, walked),
|
|
314
|
+
rate_limits: declared[:rate_limits].presence,
|
|
257
315
|
turbo_stream_actions: extract_turbo_stream_actions(source),
|
|
316
|
+
**ControllerSettings.from_source(source, walked, root: app.root.to_s, within: ctrl.name),
|
|
258
317
|
file: relative_source_path(ctrl)
|
|
259
318
|
}.compact
|
|
260
319
|
end
|
|
@@ -265,7 +324,16 @@ module RailsAiContext
|
|
|
265
324
|
path = source_path(ctrl)
|
|
266
325
|
return nil unless path && File.exist?(path)
|
|
267
326
|
|
|
268
|
-
path
|
|
327
|
+
project_relative(path)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# Under the app root, or `../../app/...` in the engine a test/dummy runs in; nil anywhere else.
|
|
331
|
+
def project_relative(path)
|
|
332
|
+
root = "#{app.root.to_s.chomp("/")}/"
|
|
333
|
+
return path.to_s.delete_prefix(root) if path.to_s.start_with?(root)
|
|
334
|
+
return nil unless PathResolver.project_file?(path, app.root)
|
|
335
|
+
|
|
336
|
+
Pathname.new(File.realpath(path)).relative_path_from(Pathname.new(File.realpath(app.root.to_s))).to_s
|
|
269
337
|
end
|
|
270
338
|
|
|
271
339
|
def api_controller?(ctrl)
|
|
@@ -290,10 +358,10 @@ module RailsAiContext
|
|
|
290
358
|
# would have Rails dispatch to; the routes decide which are actions.
|
|
291
359
|
def concern_actions(concerns, within, filters)
|
|
292
360
|
Array(concerns).flat_map do |concern|
|
|
293
|
-
|
|
294
|
-
next [] unless
|
|
361
|
+
source = ConcernPaths.module_source(app.root.to_s, concern.to_s, prefer: "controller", within: within)
|
|
362
|
+
next [] unless source
|
|
295
363
|
|
|
296
|
-
offered = (@concern_actions ||= {})[
|
|
364
|
+
offered = (@concern_actions ||= {})[source] ||= ActionResolver.module_actions(source)
|
|
297
365
|
ActionResolver.deliverable_actions(offered, filter_names(filters))
|
|
298
366
|
end.uniq
|
|
299
367
|
rescue => e
|
|
@@ -308,60 +376,100 @@ module RailsAiContext
|
|
|
308
376
|
# source parsing from inheritance chain for only/except constraints.
|
|
309
377
|
def extract_filters(ctrl, source = nil)
|
|
310
378
|
if ctrl.respond_to?(:_process_action_callbacks)
|
|
379
|
+
own_file = relative_source_path(ctrl)
|
|
311
380
|
reflection_filters = ctrl._process_action_callbacks.filter_map do |cb|
|
|
312
|
-
|
|
313
|
-
next if excluded_filters.include?(
|
|
314
|
-
{ name:
|
|
381
|
+
name = callback_name(cb.filter, own_file)
|
|
382
|
+
next if name.nil? || excluded_filters.include?(name)
|
|
383
|
+
{ name: name, kind: cb.kind.to_s }
|
|
315
384
|
end
|
|
316
385
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
386
|
+
# Collect only/except constraints from source files in the inheritance chain
|
|
387
|
+
source_constraints, blocks = reflection_filters.any? ? collect_source_constraints(ctrl, source) : [ {}, {} ]
|
|
388
|
+
reflection_filters.each do |f|
|
|
389
|
+
key = [ f[:kind], f[:name] ]
|
|
390
|
+
# Each block is a callback of its own, so blocks with one name pair up in chain order.
|
|
391
|
+
sc = ControllerFilters.block?(f[:name]) && blocks[key]&.any? ? blocks[key].shift : source_constraints[key]
|
|
392
|
+
if sc
|
|
393
|
+
f[:only] = sc[:only] if sc[:only]&.any?
|
|
394
|
+
f[:except] = sc[:except] if sc[:except]&.any?
|
|
395
|
+
f[:unless] = sc[:unless] if sc[:unless]
|
|
396
|
+
f[:if] = sc[:if] if sc[:if]
|
|
397
|
+
f[:condition] = sc[:condition] if sc[:condition]
|
|
327
398
|
end
|
|
399
|
+
end
|
|
328
400
|
|
|
329
|
-
|
|
330
|
-
|
|
401
|
+
# Evaluate known runtime conditions to remove inapplicable filters
|
|
402
|
+
reflection_filters.reject! { |f| filter_excluded_by_condition?(ctrl, f) }
|
|
331
403
|
|
|
332
|
-
|
|
333
|
-
|
|
404
|
+
# An empty chain still has the body's skips to show: what took the filters out.
|
|
405
|
+
return merge_own_source(reflection_filters, source || read_source(ctrl), ctrl)
|
|
334
406
|
end
|
|
335
407
|
|
|
336
408
|
# Fallback to source parsing when reflection is unavailable
|
|
337
|
-
|
|
338
|
-
filters = extract_filters_from_source(source)
|
|
339
|
-
return filters if filters.any?
|
|
340
|
-
end
|
|
341
|
-
|
|
342
|
-
[]
|
|
409
|
+
source ? extract_filters_from_source(source) : []
|
|
343
410
|
rescue => e
|
|
344
411
|
RailsAiContext.debug_fail(e, [], label: "extract_filters")
|
|
345
412
|
end
|
|
346
413
|
|
|
414
|
+
# cancancan adds its callbacks as blocks; the static tier names them for the macro, and so does this.
|
|
415
|
+
def cancan_callback(filter, path)
|
|
416
|
+
if path&.end_with?("cancan/controller_resource.rb")
|
|
417
|
+
filter.binding.local_variable_get(:method).to_s
|
|
418
|
+
elsif path&.end_with?("cancan/controller_additions.rb")
|
|
419
|
+
filter.binding.local_variable_defined?(:options) ? "check_authorization" : "skip_authorization_check"
|
|
420
|
+
end
|
|
421
|
+
rescue StandardError
|
|
422
|
+
nil
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# A block the app wrote is named by its line, as the static tier names it, and the one
|
|
426
|
+
# http_basic_authenticate_with adds by that macro; any other framework's or gem's
|
|
427
|
+
# block (`allow_browser`, `rate_limit`) is not a filter the app wrote.
|
|
428
|
+
def callback_name(filter, own_file = nil)
|
|
429
|
+
case filter
|
|
430
|
+
when Symbol, String then return filter.to_s.start_with?("_") ? nil : filter.to_s
|
|
431
|
+
# An anonymous class is the object `Class.new` made; an anonymous class's instance is
|
|
432
|
+
# named by its nearest named ancestor (`Struct.new(...).new`), as the static tier reads it.
|
|
433
|
+
when Module then return filter.name || "#{filter.class.name} (object)"
|
|
434
|
+
when Proc then nil
|
|
435
|
+
# An object's to_s is its inspect string, with an address that changes every run.
|
|
436
|
+
else return "#{filter.class.ancestors.find { |a| a.is_a?(Class) && a.name }.name} (object)"
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
path, line = filter.source_location
|
|
440
|
+
return "http_basic_authenticate_with" if path&.end_with?("action_controller/metal/http_authentication.rb")
|
|
441
|
+
cancan = cancan_callback(filter, path)
|
|
442
|
+
return cancan if cancan
|
|
443
|
+
|
|
444
|
+
file = path && project_relative(path)
|
|
445
|
+
return if file.nil? || file.start_with?("vendor/") || PortablePath.gem_file?(path, app.root)
|
|
446
|
+
|
|
447
|
+
ControllerFilters.block_name(line, (file unless own_file.nil? || file == own_file), lambda: filter.lambda?)
|
|
448
|
+
end
|
|
449
|
+
|
|
347
450
|
# A compiled callback keeps only:/except: in private ivars, so the
|
|
348
451
|
# constraint has to come from the chain's source. A skip record states
|
|
349
452
|
# the actions on which the filter does NOT run, which is the opposite
|
|
350
453
|
# of what the filter is being asked for, so it never supplies one.
|
|
351
454
|
def collect_source_constraints(ctrl, current_source = nil)
|
|
352
455
|
constraints = {}
|
|
456
|
+
bodies = []
|
|
353
457
|
klass = ctrl
|
|
354
458
|
while klass&.name && !ActionResolver.framework?(klass, kind: :controller)
|
|
355
459
|
src = (klass == ctrl) ? (current_source || read_source(klass)) : read_source(klass)
|
|
356
460
|
if src
|
|
461
|
+
own = own_filters(src, klass.name)
|
|
462
|
+
own = ControllerFilters.in_file(own, relative_source_path(klass)) unless klass == ctrl
|
|
357
463
|
# Within one body the last declaration wins; across the chain the most specific class does.
|
|
358
|
-
effective_declarations(
|
|
464
|
+
effective_declarations(own).each { |key, sf| constraints[key] ||= sf }
|
|
465
|
+
bodies.unshift(own)
|
|
359
466
|
end
|
|
360
467
|
klass = klass.superclass
|
|
361
468
|
end
|
|
362
|
-
|
|
469
|
+
blocks = bodies.flatten.select { |f| !f[:skipped] && ControllerFilters.block?(f[:name]) }.group_by { |f| [ f[:kind], f[:name] ] }
|
|
470
|
+
[ constraints, blocks ]
|
|
363
471
|
rescue => e
|
|
364
|
-
RailsAiContext.debug_fail(e, {}, label: "collect_source_constraints")
|
|
472
|
+
RailsAiContext.debug_fail(e, [ {}, {} ], label: "collect_source_constraints")
|
|
365
473
|
end
|
|
366
474
|
|
|
367
475
|
# Reflection hands every class the whole chain and no skips at all, so
|
|
@@ -382,9 +490,17 @@ module RailsAiContext
|
|
|
382
490
|
# `before_action :audit` is still the ancestor's.
|
|
383
491
|
by_key = filters.group_by { |f| [ f[:kind], f[:name] ] }
|
|
384
492
|
effective_declarations(own).each do |key, declared|
|
|
385
|
-
Array(by_key[key])
|
|
493
|
+
rows = Array(by_key[key])
|
|
494
|
+
# Each block is a callback of its own: the body's are the last ones of that name, in its order.
|
|
495
|
+
pairs = if ControllerFilters.block?(key.last)
|
|
496
|
+
mine = own.select { |f| !f[:skipped] && [ f[:kind], f[:name] ] == key }
|
|
497
|
+
rows.last(mine.size).zip(mine)
|
|
498
|
+
else
|
|
499
|
+
rows.map { |f| [ f, declared ] }
|
|
500
|
+
end
|
|
501
|
+
pairs.each do |f, source_record|
|
|
386
502
|
f[:declared] = true
|
|
387
|
-
|
|
503
|
+
source_record[:from_concern] ? f[:from_concern] = source_record[:from_concern] : f.delete(:from_concern)
|
|
388
504
|
end
|
|
389
505
|
end
|
|
390
506
|
skips = own.select { |f| f[:skipped] }
|
|
@@ -427,7 +543,7 @@ module RailsAiContext
|
|
|
427
543
|
end
|
|
428
544
|
|
|
429
545
|
def extract_filters_from_source(source)
|
|
430
|
-
ControllerFilters.from_source(source)
|
|
546
|
+
ControllerFilters.from_source(source, root: app.root.to_s)
|
|
431
547
|
end
|
|
432
548
|
|
|
433
549
|
# Statically evaluate known runtime conditions to exclude inapplicable filters.
|
|
@@ -453,6 +569,39 @@ module RailsAiContext
|
|
|
453
569
|
RailsAiContext.debug_fail(e, false, label: "devise_controller?")
|
|
454
570
|
end
|
|
455
571
|
|
|
572
|
+
# A filter whose method a module from outside the app defines (`include ActiveStorage::SetBlob`)
|
|
573
|
+
# is declared in no file the walk reads, so it is credited to that module.
|
|
574
|
+
def from_unread_mixins(ctrl, filters)
|
|
575
|
+
unread = mixins_unread(ctrl)
|
|
576
|
+
return filters if unread.empty?
|
|
577
|
+
|
|
578
|
+
filters.map do |f|
|
|
579
|
+
next f if f[:declared] || f[:skipped] || f[:from_concern]
|
|
580
|
+
|
|
581
|
+
owner = begin
|
|
582
|
+
ctrl.instance_method(f[:name]).owner.name
|
|
583
|
+
rescue NameError
|
|
584
|
+
nil
|
|
585
|
+
end
|
|
586
|
+
unread.include?(owner) ? f.merge(from_concern: owner) : f
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
# Modules an app class on the chain mixes in from a file outside the app. A gem's on_load
|
|
591
|
+
# lands above the framework base.
|
|
592
|
+
def mixins_unread(ctrl)
|
|
593
|
+
ancestors = ctrl.ancestors
|
|
594
|
+
base = ancestors.index { |mod| mod.is_a?(Class) && ActionResolver.framework?(mod, kind: :controller) }
|
|
595
|
+
ancestors.first(base || 0).filter_map do |mod|
|
|
596
|
+
next if mod.is_a?(Class) || mod.name.nil?
|
|
597
|
+
|
|
598
|
+
path, = Object.const_source_location(mod.name)
|
|
599
|
+
mod.name unless path && project_relative(path) && !PortablePath.gem_file?(path, app.root)
|
|
600
|
+
end
|
|
601
|
+
rescue => e
|
|
602
|
+
RailsAiContext.debug_fail(e, [], label: "mixins_unread")
|
|
603
|
+
end
|
|
604
|
+
|
|
456
605
|
def extract_concerns(ctrl)
|
|
457
606
|
ConcernMembership.from_ancestors(ctrl)
|
|
458
607
|
rescue => e
|
|
@@ -469,13 +618,17 @@ module RailsAiContext
|
|
|
469
618
|
RailsAiContext.debug_fail(e, [], label: "extract_concerns_from_source AST")
|
|
470
619
|
end
|
|
471
620
|
|
|
472
|
-
def extract_strong_params(source)
|
|
621
|
+
def extract_strong_params(source, walked = class_body_walk(source))
|
|
473
622
|
return [] if source.nil?
|
|
474
623
|
|
|
475
624
|
parse_result = AstCache.parse_string(source)
|
|
476
625
|
param_methods = []
|
|
477
626
|
find_param_methods(parse_result.value, param_methods)
|
|
478
|
-
|
|
627
|
+
nested = nested_ranges(walked)
|
|
628
|
+
param_methods.reject do |entry|
|
|
629
|
+
offset = entry.delete(:offset)
|
|
630
|
+
nested.any? { |range| range.cover?(offset) }
|
|
631
|
+
end
|
|
479
632
|
rescue => e
|
|
480
633
|
RailsAiContext.debug_fail(e, [], label: "extract_strong_params AST")
|
|
481
634
|
end
|
|
@@ -483,8 +636,7 @@ module RailsAiContext
|
|
|
483
636
|
def find_param_methods(node, results)
|
|
484
637
|
return unless node.respond_to?(:child_nodes)
|
|
485
638
|
if node.is_a?(Prism::DefNode) && node.name.to_s.end_with?("_params")
|
|
486
|
-
|
|
487
|
-
results << details
|
|
639
|
+
results << extract_permit_from_def(node).merge(offset: node.location.start_offset)
|
|
488
640
|
end
|
|
489
641
|
node.child_nodes.compact.each { |child| find_param_methods(child, results) }
|
|
490
642
|
end
|
|
@@ -545,6 +697,7 @@ module RailsAiContext
|
|
|
545
697
|
permits = []
|
|
546
698
|
nested = {}
|
|
547
699
|
arrays = []
|
|
700
|
+
hashes = []
|
|
548
701
|
|
|
549
702
|
args = permit_call.arguments&.arguments || []
|
|
550
703
|
args.each do |arg|
|
|
@@ -556,13 +709,9 @@ module RailsAiContext
|
|
|
556
709
|
next unless assoc.is_a?(Prism::AssocNode)
|
|
557
710
|
key = extract_ast_value(assoc.key).to_s
|
|
558
711
|
val = assoc.value
|
|
559
|
-
if val.is_a?(Prism::ArrayNode)
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
nested[key] = inner.reject { |v| v == "" || v == "inferred" }
|
|
563
|
-
else
|
|
564
|
-
arrays << key
|
|
565
|
-
end
|
|
712
|
+
if val.is_a?(Prism::ArrayNode) || val.is_a?(Prism::HashNode)
|
|
713
|
+
fields = permit_value(val)
|
|
714
|
+
fields.any? ? nested[key] = fields : (val.is_a?(Prism::HashNode) ? hashes : arrays) << key
|
|
566
715
|
else
|
|
567
716
|
permits << key
|
|
568
717
|
end
|
|
@@ -576,6 +725,7 @@ module RailsAiContext
|
|
|
576
725
|
result[:permits] = permits if permits.any?
|
|
577
726
|
result[:nested] = nested if nested.any?
|
|
578
727
|
result[:arrays] = arrays if arrays.any?
|
|
728
|
+
result[:hashes] = hashes if hashes.any?
|
|
579
729
|
result
|
|
580
730
|
end
|
|
581
731
|
|
|
@@ -586,6 +736,8 @@ module RailsAiContext
|
|
|
586
736
|
result = {}
|
|
587
737
|
permits = []
|
|
588
738
|
nested = {}
|
|
739
|
+
arrays = []
|
|
740
|
+
hashes = []
|
|
589
741
|
|
|
590
742
|
args = expect_call.arguments&.arguments || []
|
|
591
743
|
args.each do |arg|
|
|
@@ -598,7 +750,7 @@ module RailsAiContext
|
|
|
598
750
|
key = extract_ast_value(assoc.key).to_s
|
|
599
751
|
if assoc.value.is_a?(Prism::ArrayNode)
|
|
600
752
|
result[:requires] ||= key
|
|
601
|
-
collect_expect_array(assoc.value, key, permits, nested)
|
|
753
|
+
collect_expect_array(assoc.value, key, permits, nested, arrays, hashes)
|
|
602
754
|
else
|
|
603
755
|
permits << key
|
|
604
756
|
end
|
|
@@ -608,23 +760,30 @@ module RailsAiContext
|
|
|
608
760
|
|
|
609
761
|
result[:permits] = permits if permits.any?
|
|
610
762
|
result[:nested] = nested if nested.any?
|
|
763
|
+
result[:arrays] = arrays if arrays.any?
|
|
764
|
+
result[:hashes] = hashes if hashes.any?
|
|
611
765
|
result
|
|
612
766
|
end
|
|
613
767
|
|
|
614
|
-
def collect_expect_array(array_node, key, permits, nested)
|
|
768
|
+
def collect_expect_array(array_node, key, permits, nested, arrays, hashes)
|
|
615
769
|
array_node.elements.each do |el|
|
|
616
770
|
case el
|
|
617
771
|
when Prism::SymbolNode
|
|
618
772
|
permits << el.unescaped
|
|
619
773
|
when Prism::ArrayNode
|
|
620
774
|
# Doubly-wrapped array marks an array-of-hashes attribute
|
|
621
|
-
nested[key] =
|
|
775
|
+
nested[key] = [ permit_fields(el) ]
|
|
622
776
|
when Prism::KeywordHashNode, Prism::HashNode
|
|
623
777
|
el.elements.each do |inner|
|
|
624
778
|
next unless inner.is_a?(Prism::AssocNode)
|
|
625
779
|
inner_key = extract_ast_value(inner.key).to_s
|
|
626
|
-
if inner.value.is_a?(Prism::ArrayNode)
|
|
627
|
-
|
|
780
|
+
if inner.value.is_a?(Prism::ArrayNode) && inner.value.elements.empty?
|
|
781
|
+
arrays << inner_key
|
|
782
|
+
elsif inner.value.is_a?(Prism::ArrayNode)
|
|
783
|
+
nested[inner_key] = permit_fields(inner.value)
|
|
784
|
+
elsif inner.value.is_a?(Prism::HashNode)
|
|
785
|
+
fields = permit_value(inner.value)
|
|
786
|
+
fields.any? ? nested[inner_key] = fields : hashes << inner_key
|
|
628
787
|
else
|
|
629
788
|
permits << inner_key
|
|
630
789
|
end
|
|
@@ -633,16 +792,38 @@ module RailsAiContext
|
|
|
633
792
|
end
|
|
634
793
|
end
|
|
635
794
|
|
|
636
|
-
|
|
795
|
+
# What a nested list permits: a scalar by name, `{ key => fields }` for an
|
|
796
|
+
# array (empty for scalars), `{ key => {} }` for any hash,
|
|
797
|
+
# `{ key => { inner => fields } }` for a hash that names its keys, and an
|
|
798
|
+
# inner list as an array, which `expect` reads as an array of hashes.
|
|
799
|
+
def permit_fields(array_node)
|
|
637
800
|
array_node.elements.flat_map do |el|
|
|
638
801
|
case el
|
|
639
|
-
when Prism::SymbolNode then [ el.unescaped ]
|
|
640
|
-
when Prism::ArrayNode then
|
|
802
|
+
when Prism::SymbolNode, Prism::StringNode then [ el.unescaped ]
|
|
803
|
+
when Prism::ArrayNode then [ permit_fields(el) ]
|
|
804
|
+
when Prism::KeywordHashNode, Prism::HashNode
|
|
805
|
+
el.elements.grep(Prism::AssocNode).map do |assoc|
|
|
806
|
+
key = extract_ast_value(assoc.key).to_s
|
|
807
|
+
value = permit_value(assoc.value)
|
|
808
|
+
value.nil? ? key : { key => value }
|
|
809
|
+
end
|
|
641
810
|
else []
|
|
642
811
|
end
|
|
643
812
|
end
|
|
644
813
|
end
|
|
645
814
|
|
|
815
|
+
# Rails reads only an empty `{}` as any hash; a hash with keys permits those keys alone.
|
|
816
|
+
def permit_value(node)
|
|
817
|
+
case node
|
|
818
|
+
when Prism::ArrayNode then permit_fields(node)
|
|
819
|
+
when Prism::HashNode
|
|
820
|
+
node.elements.grep(Prism::AssocNode).each_with_object({}) do |assoc, members|
|
|
821
|
+
value = permit_value(assoc.value)
|
|
822
|
+
members[extract_ast_value(assoc.key).to_s] = value unless value.nil?
|
|
823
|
+
end
|
|
824
|
+
end
|
|
825
|
+
end
|
|
826
|
+
|
|
646
827
|
def extract_ast_value(node)
|
|
647
828
|
case node
|
|
648
829
|
when Prism::SymbolNode then node.unescaped
|
|
@@ -653,27 +834,24 @@ module RailsAiContext
|
|
|
653
834
|
end
|
|
654
835
|
end
|
|
655
836
|
|
|
656
|
-
def
|
|
837
|
+
def block_formats(source, walked = class_body_walk(source))
|
|
657
838
|
return [] if source.nil?
|
|
658
839
|
|
|
659
840
|
parse_result = AstCache.parse_string(source)
|
|
660
|
-
# Only extract format calls inside respond_to blocks
|
|
661
841
|
respond_to_blocks = []
|
|
662
|
-
find_respond_to_blocks(parse_result.value, respond_to_blocks)
|
|
663
|
-
return [] if respond_to_blocks.empty?
|
|
664
|
-
|
|
665
842
|
formats = []
|
|
843
|
+
find_respond_to_blocks(parse_result.value, respond_to_blocks)
|
|
844
|
+
nested = nested_ranges(walked)
|
|
845
|
+
respond_to_blocks.reject! { |block| nested.any? { |range| range.cover?(block.location.start_offset) } }
|
|
666
846
|
respond_to_blocks.each { |block| find_format_calls(block, formats) }
|
|
667
|
-
formats.uniq
|
|
847
|
+
formats.uniq
|
|
668
848
|
rescue => e
|
|
669
|
-
RailsAiContext.debug_fail(e, [], label: "
|
|
849
|
+
RailsAiContext.debug_fail(e, [], label: "block_formats AST")
|
|
670
850
|
end
|
|
671
851
|
|
|
672
852
|
def find_respond_to_blocks(node, blocks)
|
|
673
853
|
return unless node.respond_to?(:child_nodes)
|
|
674
|
-
if node.is_a?(Prism::CallNode) && node.name == :respond_to && node.block
|
|
675
|
-
blocks << node.block
|
|
676
|
-
end
|
|
854
|
+
blocks << node.block if node.is_a?(Prism::CallNode) && node.name == :respond_to && node.block
|
|
677
855
|
node.child_nodes.compact.each { |child| find_respond_to_blocks(child, blocks) }
|
|
678
856
|
end
|
|
679
857
|
|
|
@@ -691,14 +869,10 @@ module RailsAiContext
|
|
|
691
869
|
node.child_nodes.compact.each { |child| find_format_calls(child, formats) }
|
|
692
870
|
end
|
|
693
871
|
|
|
694
|
-
def extract_rescue_from(source)
|
|
695
|
-
return [] if source.nil?
|
|
872
|
+
def extract_rescue_from(source, walked = class_body_walk(source))
|
|
873
|
+
return [] if source.nil? || walked.nil?
|
|
696
874
|
|
|
697
|
-
|
|
698
|
-
rescue_from: -> { Listeners::GenericMacroListener.new(:rescue_from) }
|
|
699
|
-
})
|
|
700
|
-
raw = ast_result[:rescue_from] || []
|
|
701
|
-
raw.flat_map do |entry|
|
|
875
|
+
SourceIntrospector.class_level(walked[:rescue_from], walked).flat_map do |entry|
|
|
702
876
|
handler = entry[:options][:with]&.to_s
|
|
703
877
|
# The exception classes are the listener's positional values; `args`
|
|
704
878
|
# is symbols only, so a constant reaches this line through `values`.
|
|
@@ -710,44 +884,50 @@ module RailsAiContext
|
|
|
710
884
|
RailsAiContext.debug_fail(e, [], label: "extract_rescue_from AST")
|
|
711
885
|
end
|
|
712
886
|
|
|
713
|
-
|
|
887
|
+
# One walk of the class body serves the rate limits, the respond_to formats, rescue_from and the settings.
|
|
888
|
+
def class_body_walk(source)
|
|
714
889
|
return nil if source.nil?
|
|
715
890
|
|
|
716
|
-
|
|
717
|
-
rate_limit: -> { Listeners::GenericMacroListener.new(:rate_limit) }
|
|
718
|
-
|
|
719
|
-
|
|
891
|
+
SourceIntrospector.walk_source(source, ControllerSettings::LISTENERS.merge(
|
|
892
|
+
rate_limit: -> { Listeners::GenericMacroListener.new(:rate_limit) },
|
|
893
|
+
rescue_from: -> { Listeners::GenericMacroListener.new(:rescue_from) },
|
|
894
|
+
respond_to: -> { Listeners::GenericMacroListener.new(:respond_to, :clear_respond_to) }
|
|
895
|
+
))
|
|
720
896
|
rescue => e
|
|
721
|
-
RailsAiContext.debug_fail(e, nil, label: "
|
|
897
|
+
RailsAiContext.debug_fail(e, nil, label: "class_body_walk")
|
|
722
898
|
end
|
|
723
899
|
|
|
724
|
-
|
|
725
|
-
|
|
900
|
+
# A class or module nested in the controller declares and defines only for itself.
|
|
901
|
+
def nested_ranges(walked)
|
|
902
|
+
Array(walked && walked[:nested])
|
|
903
|
+
end
|
|
726
904
|
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
905
|
+
# Each `rate_limit` the class body declares (`name:` lets one controller declare
|
|
906
|
+
# several), as its options read and the literals among them.
|
|
907
|
+
def extract_rate_limits(source, walked = class_body_walk(source))
|
|
908
|
+
return [] if walked.nil?
|
|
730
909
|
|
|
731
|
-
|
|
732
|
-
|
|
910
|
+
SourceIntrospector.class_level(walked[:rate_limit], walked).map do |entry|
|
|
911
|
+
options = entry[:options] || {}
|
|
912
|
+
sources = entry[:option_values] || {}
|
|
913
|
+
text = options.map { |key, value| "#{key}: #{inferred?(value) ? sources[key] : value.inspect}" }.join(", ")
|
|
914
|
+
{ text: text, to: options[:to], within: sources[:within]&.to_s, only: options.key?(:only) ? Array(options[:only]).map(&:to_s) : nil,
|
|
915
|
+
name: options[:name] }.select { |key, value| key == :text || literal?(value) }
|
|
916
|
+
end
|
|
733
917
|
rescue => e
|
|
734
|
-
RailsAiContext.debug_fail(e,
|
|
918
|
+
RailsAiContext.debug_fail(e, [], label: "extract_rate_limits")
|
|
735
919
|
end
|
|
736
920
|
|
|
737
|
-
def
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
parsed[:to] = options[:to] if options[:to].is_a?(Integer)
|
|
745
|
-
parsed[:within] = sources[:within].to_s if sources.key?(:within)
|
|
746
|
-
parsed[:only] = Array(options[:only]).map(&:to_s) if options.key?(:only)
|
|
921
|
+
def inferred?(value)
|
|
922
|
+
case value
|
|
923
|
+
when Array then value.any? { |item| inferred?(item) }
|
|
924
|
+
when Hash then value.values.any? { |item| inferred?(item) }
|
|
925
|
+
else value == Confidence::INFERRED
|
|
926
|
+
end
|
|
927
|
+
end
|
|
747
928
|
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
RailsAiContext.debug_fail(e, nil, label: "parse_rate_limit")
|
|
929
|
+
def literal?(value)
|
|
930
|
+
!value.nil? && !inferred?(value)
|
|
751
931
|
end
|
|
752
932
|
|
|
753
933
|
def extract_turbo_stream_actions(source)
|
|
@@ -789,13 +969,21 @@ module RailsAiContext
|
|
|
789
969
|
|
|
790
970
|
# --- AST helpers ---
|
|
791
971
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
972
|
+
def parent_declaration(source, class_name)
|
|
973
|
+
DeclaredConstant.parent_declaration(source, class_name)
|
|
974
|
+
end
|
|
975
|
+
|
|
976
|
+
# Only a nesting the class name does not already imply is carried: the
|
|
977
|
+
# compact form, or a root-anchored superclass.
|
|
978
|
+
def odd_nesting(class_name, nesting)
|
|
979
|
+
nesting unless nesting.nil? || nesting == SuperclassChain.nesting_of(class_name)
|
|
980
|
+
end
|
|
981
|
+
|
|
982
|
+
# Reflection names the superclass absolutely; reading it back by the
|
|
983
|
+
# class's own namespace would land on a namesake there.
|
|
984
|
+
def booted_parent_nesting(ctrl)
|
|
985
|
+
nearest = SuperclassChain.resolve_in_scope(ctrl.name, ctrl.superclass.name) { |c| c.safe_constantize.presence }
|
|
986
|
+
[] unless nearest.nil? || nearest.equal?(ctrl.superclass)
|
|
799
987
|
end
|
|
800
988
|
|
|
801
989
|
def read_source(ctrl)
|
|
@@ -808,12 +996,10 @@ module RailsAiContext
|
|
|
808
996
|
# agrees when the app registers no inflection. See CONTEXT.md,
|
|
809
997
|
# "Declared constant".
|
|
810
998
|
def source_path(ctrl)
|
|
811
|
-
#
|
|
812
|
-
# spec - is not this app's controller file.
|
|
999
|
+
# The app's own, or the engine's its test/dummy runs in: a constant defined by a gem - or by
|
|
1000
|
+
# a spec - is not this app's controller file.
|
|
813
1001
|
located = Object.const_source_location(ctrl.name)&.first
|
|
814
|
-
if located && File.exist?(located) && located
|
|
815
|
-
return located
|
|
816
|
-
end
|
|
1002
|
+
return located if located && File.exist?(located) && project_relative(located)
|
|
817
1003
|
|
|
818
1004
|
File.join(app.root.to_s, "app", "controllers", "#{ctrl.name.underscore}.rb")
|
|
819
1005
|
rescue StandardError
|