reactiveadmin 0.2.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.
Files changed (260) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +88 -0
  3. data/LICENSE +21 -0
  4. data/README.md +81 -0
  5. data/app/assets/reactive_admin/bridge.js +89 -0
  6. data/app/controllers/reactive_admin/api_controller.rb +30 -0
  7. data/app/controllers/reactive_admin/client_errors_controller.rb +38 -0
  8. data/app/controllers/reactive_admin/manifest_controller.rb +10 -0
  9. data/app/controllers/reactive_admin/mcp_controller.rb +48 -0
  10. data/app/controllers/reactive_admin/openapi_controller.rb +13 -0
  11. data/app/controllers/reactive_admin/session_controller.rb +11 -0
  12. data/app/views/layouts/reactive_admin/embed.html.erb +30 -0
  13. data/lib/generators/reactiveadmin/install/install_generator.rb +341 -0
  14. data/lib/generators/reactiveadmin/install/templates/menu.rb.tt +25 -0
  15. data/lib/generators/reactiveadmin/install/templates/reactive_admin.rb.tt +53 -0
  16. data/lib/generators/reactiveadmin/install/templates/reactive_admin_annotated.rb.tt +110 -0
  17. data/lib/reactive_admin/aa/v3/controller.rb +76 -0
  18. data/lib/reactive_admin/aa/v3/forms.rb +29 -0
  19. data/lib/reactive_admin/aa/v3/inputs/filters/typeahead_input.rb +51 -0
  20. data/lib/reactive_admin/aa/v3/inputs/typeahead_input.rb +8 -0
  21. data/lib/reactive_admin/aa/v3/install.rb +36 -0
  22. data/lib/reactive_admin/aa/v3/menu.rb +67 -0
  23. data/lib/reactive_admin/aa/v3/presenters.rb +102 -0
  24. data/lib/reactive_admin/aa/v3/registry.rb +78 -0
  25. data/lib/reactive_admin/aa/v3/views.rb +63 -0
  26. data/lib/reactive_admin/aa/v3.rb +23 -0
  27. data/lib/reactive_admin/aa/v4.rb +59 -0
  28. data/lib/reactive_admin/aa.rb +46 -0
  29. data/lib/reactive_admin/action_items.rb +39 -0
  30. data/lib/reactive_admin/action_links.rb +197 -0
  31. data/lib/reactive_admin/action_safety.rb +19 -0
  32. data/lib/reactive_admin/actions.rb +95 -0
  33. data/lib/reactive_admin/api_errors.rb +116 -0
  34. data/lib/reactive_admin/api_fallback.rb +15 -0
  35. data/lib/reactive_admin/api_guards.rb +445 -0
  36. data/lib/reactive_admin/audit.rb +72 -0
  37. data/lib/reactive_admin/auth/guard.rb +68 -0
  38. data/lib/reactive_admin/auth/token_requests.rb +15 -0
  39. data/lib/reactive_admin/auth.rb +135 -0
  40. data/lib/reactive_admin/authorizer.rb +47 -0
  41. data/lib/reactive_admin/background_requests.rb +33 -0
  42. data/lib/reactive_admin/batch_actions.rb +57 -0
  43. data/lib/reactive_admin/choices.rb +289 -0
  44. data/lib/reactive_admin/client_error.rb +70 -0
  45. data/lib/reactive_admin/composite_keys.rb +12 -0
  46. data/lib/reactive_admin/configuration.rb +221 -0
  47. data/lib/reactive_admin/controller_extension.rb +229 -0
  48. data/lib/reactive_admin/csv_injection.rb +26 -0
  49. data/lib/reactive_admin/date_bound.rb +31 -0
  50. data/lib/reactive_admin/degradation.rb +72 -0
  51. data/lib/reactive_admin/diagnostics.rb +16 -0
  52. data/lib/reactive_admin/doctor.rb +466 -0
  53. data/lib/reactive_admin/dry_run.rb +147 -0
  54. data/lib/reactive_admin/dry_run_flag.rb +18 -0
  55. data/lib/reactive_admin/embed.rb +73 -0
  56. data/lib/reactive_admin/engine.rb +47 -0
  57. data/lib/reactive_admin/error_keys.rb +23 -0
  58. data/lib/reactive_admin/exposure.rb +86 -0
  59. data/lib/reactive_admin/filters.rb +165 -0
  60. data/lib/reactive_admin/fixtures/scrubber.rb +1075 -0
  61. data/lib/reactive_admin/fixtures.rb +161 -0
  62. data/lib/reactive_admin/form_content.rb +78 -0
  63. data/lib/reactive_admin/form_field.rb +279 -0
  64. data/lib/reactive_admin/form_has_many.rb +116 -0
  65. data/lib/reactive_admin/form_lock.rb +71 -0
  66. data/lib/reactive_admin/form_payload.rb +57 -0
  67. data/lib/reactive_admin/form_recorder.rb +192 -0
  68. data/lib/reactive_admin/form_schema.rb +160 -0
  69. data/lib/reactive_admin/frame.rb +97 -0
  70. data/lib/reactive_admin/html_nodes.rb +390 -0
  71. data/lib/reactive_admin/html_policy.rb +338 -0
  72. data/lib/reactive_admin/html_rich.rb +186 -0
  73. data/lib/reactive_admin/implicit_defaults.rb +102 -0
  74. data/lib/reactive_admin/index_options.rb +41 -0
  75. data/lib/reactive_admin/index_payload.rb +259 -0
  76. data/lib/reactive_admin/index_recorder.rb +115 -0
  77. data/lib/reactive_admin/index_views.rb +85 -0
  78. data/lib/reactive_admin/inline_style.rb +125 -0
  79. data/lib/reactive_admin/labels_payload.rb +34 -0
  80. data/lib/reactive_admin/ladder.rb +268 -0
  81. data/lib/reactive_admin/layout_classes.rb +167 -0
  82. data/lib/reactive_admin/legacy_inventory.rb +547 -0
  83. data/lib/reactive_admin/location.rb +22 -0
  84. data/lib/reactive_admin/lookup_audit.rb +30 -0
  85. data/lib/reactive_admin/lookup_payload.rb +41 -0
  86. data/lib/reactive_admin/manifest.rb +36 -0
  87. data/lib/reactive_admin/masked_json.rb +62 -0
  88. data/lib/reactive_admin/masking.rb +100 -0
  89. data/lib/reactive_admin/mcp/access.rb +111 -0
  90. data/lib/reactive_admin/mcp/api.rb +123 -0
  91. data/lib/reactive_admin/mcp/count_tool.rb +22 -0
  92. data/lib/reactive_admin/mcp/layout.rb +175 -0
  93. data/lib/reactive_admin/mcp/output.rb +46 -0
  94. data/lib/reactive_admin/mcp/prompts.rb +111 -0
  95. data/lib/reactive_admin/mcp/server.rb +134 -0
  96. data/lib/reactive_admin/mcp/shapes.rb +164 -0
  97. data/lib/reactive_admin/mcp/tools.rb +386 -0
  98. data/lib/reactive_admin/mcp.rb +53 -0
  99. data/lib/reactive_admin/media_nodes.rb +34 -0
  100. data/lib/reactive_admin/menu.rb +90 -0
  101. data/lib/reactive_admin/menu_overlay.rb +291 -0
  102. data/lib/reactive_admin/node_registry.rb +47 -0
  103. data/lib/reactive_admin/open_api/builder.rb +156 -0
  104. data/lib/reactive_admin/open_api/context.rb +46 -0
  105. data/lib/reactive_admin/open_api/core.rb +51 -0
  106. data/lib/reactive_admin/open_api/operation_ids.rb +18 -0
  107. data/lib/reactive_admin/open_api/page_doc.rb +75 -0
  108. data/lib/reactive_admin/open_api/parts.rb +86 -0
  109. data/lib/reactive_admin/open_api/resource_doc.rb +423 -0
  110. data/lib/reactive_admin/open_api/security.rb +55 -0
  111. data/lib/reactive_admin/open_api/write_schema.rb +104 -0
  112. data/lib/reactive_admin/open_api.rb +41 -0
  113. data/lib/reactive_admin/option_source.rb +36 -0
  114. data/lib/reactive_admin/option_sources.rb +14 -0
  115. data/lib/reactive_admin/options_payload.rb +61 -0
  116. data/lib/reactive_admin/page_controller_extension.rb +112 -0
  117. data/lib/reactive_admin/page_forms.rb +181 -0
  118. data/lib/reactive_admin/page_payload.rb +120 -0
  119. data/lib/reactive_admin/param_normalizer.rb +89 -0
  120. data/lib/reactive_admin/paths.rb +63 -0
  121. data/lib/reactive_admin/permissions.rb +31 -0
  122. data/lib/reactive_admin/provenance.rb +57 -0
  123. data/lib/reactive_admin/query_guard.rb +108 -0
  124. data/lib/reactive_admin/ransack_guard.rb +107 -0
  125. data/lib/reactive_admin/rate_limit.rb +59 -0
  126. data/lib/reactive_admin/reactive_dsl.rb +99 -0
  127. data/lib/reactive_admin/readiness.rb +189 -0
  128. data/lib/reactive_admin/registry.rb +69 -0
  129. data/lib/reactive_admin/resource_meta.rb +120 -0
  130. data/lib/reactive_admin/resource_summary.rb +41 -0
  131. data/lib/reactive_admin/routes.rb +60 -0
  132. data/lib/reactive_admin/row_errors.rb +29 -0
  133. data/lib/reactive_admin/rspec.rb +116 -0
  134. data/lib/reactive_admin/scrubbed_error.rb +22 -0
  135. data/lib/reactive_admin/search.rb +79 -0
  136. data/lib/reactive_admin/server_timing.rb +31 -0
  137. data/lib/reactive_admin/show_payload.rb +88 -0
  138. data/lib/reactive_admin/sidebars.rb +34 -0
  139. data/lib/reactive_admin/site.rb +55 -0
  140. data/lib/reactive_admin/style_report.rb +129 -0
  141. data/lib/reactive_admin/style_sources.rb +83 -0
  142. data/lib/reactive_admin/template_actions.rb +126 -0
  143. data/lib/reactive_admin/template_content.rb +49 -0
  144. data/lib/reactive_admin/template_form.rb +442 -0
  145. data/lib/reactive_admin/test_support.rb +31 -0
  146. data/lib/reactive_admin/typeahead.rb +33 -0
  147. data/lib/reactive_admin/ui/assets.rb +91 -0
  148. data/lib/reactive_admin/ui/client_build.rb +47 -0
  149. data/lib/reactive_admin/ui/helper.rb +24 -0
  150. data/lib/reactive_admin/ui/packaged_client.rb +67 -0
  151. data/lib/reactive_admin/ui/shell.rb +81 -0
  152. data/lib/reactive_admin/ui/switch.rb +38 -0
  153. data/lib/reactive_admin/ui.rb +43 -0
  154. data/lib/reactive_admin/value_payload.rb +147 -0
  155. data/lib/reactive_admin/value_token.rb +36 -0
  156. data/lib/reactive_admin/values.rb +43 -0
  157. data/lib/reactive_admin/version.rb +3 -0
  158. data/lib/reactive_admin/write_audit.rb +60 -0
  159. data/lib/reactive_admin/write_guards.rb +142 -0
  160. data/lib/reactive_admin.rb +153 -0
  161. data/lib/reactiveadmin.rb +1 -0
  162. data/lib/tasks/reactiveadmin.rake +36 -0
  163. data/lib/tasks/reactiveadmin_client.rake +11 -0
  164. data/lib/tasks/reactiveadmin_legacy.rake +7 -0
  165. data/lib/tasks/reactiveadmin_styles.rake +6 -0
  166. data/openapi/core.yaml +1643 -0
  167. data/vendor/client/assets/ActionPage-XUYorI8E.js +1 -0
  168. data/vendor/client/assets/ActionView-BsZFcsgp.js +1 -0
  169. data/vendor/client/assets/CommandPalette-C59HT1ip.js +1 -0
  170. data/vendor/client/assets/DatePicker-DuaK0iMg.js +1 -0
  171. data/vendor/client/assets/DateRangeCalendar-B2BDsG-X.js +1 -0
  172. data/vendor/client/assets/DegradedNotice-DiH5n2DO.js +1 -0
  173. data/vendor/client/assets/FormDialog-pmYl8Eov.js +1 -0
  174. data/vendor/client/assets/FormSkeleton-DuSBZO3C.js +1 -0
  175. data/vendor/client/assets/FrameView-2X8cFvUt.css +1 -0
  176. data/vendor/client/assets/FrameView-zmUeDO8d.js +1 -0
  177. data/vendor/client/assets/GroupOverview-DI5T3k5K.js +1 -0
  178. data/vendor/client/assets/InlineForm-CMEIMgeh.js +1 -0
  179. data/vendor/client/assets/JsonDiff-o7JWr7CH.js +3 -0
  180. data/vendor/client/assets/JsonEditor-D4VXdlEx.js +1 -0
  181. data/vendor/client/assets/JsonView-BrJLDmSN.js +1 -0
  182. data/vendor/client/assets/MobileMenu-GAeFOeEb.js +1 -0
  183. data/vendor/client/assets/Node-SQl61V4K.js +2 -0
  184. data/vendor/client/assets/PageView-C-laNEPZ.js +1 -0
  185. data/vendor/client/assets/ResourceForm-BPjsoCY6.js +1 -0
  186. data/vendor/client/assets/ResourceIndex-COezAw5t.js +3 -0
  187. data/vendor/client/assets/ResourceOrPage-CRZGCY84.js +1 -0
  188. data/vendor/client/assets/ResourceShow-DweRXn18.js +1 -0
  189. data/vendor/client/assets/Segmented-BE9TCbph.js +1 -0
  190. data/vendor/client/assets/Sidebars-CsB9rFqX.js +1 -0
  191. data/vendor/client/assets/breadcrumb-DSA3Tlyd.js +1 -0
  192. data/vendor/client/assets/button-CvZ4_DFC.js +1 -0
  193. data/vendor/client/assets/calendar-BoyMpJIf.js +1 -0
  194. data/vendor/client/assets/check-C5A3J16N.js +1 -0
  195. data/vendor/client/assets/command-CMuk0oAt.js +1 -0
  196. data/vendor/client/assets/dist-CqEI82S_.js +1 -0
  197. data/vendor/client/assets/dist-IW4yX942.js +1 -0
  198. data/vendor/client/assets/editor-yqRLzgsJ.js +2 -0
  199. data/vendor/client/assets/external-link-C2aR4slL.js +1 -0
  200. data/vendor/client/assets/frames-C9gPCc3N.js +1 -0
  201. data/vendor/client/assets/humanize-DtU_Kpr4.js +1 -0
  202. data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
  203. data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
  204. data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
  205. data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
  206. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
  207. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
  208. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
  209. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
  210. data/vendor/client/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
  211. data/vendor/client/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
  212. data/vendor/client/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
  213. data/vendor/client/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
  214. data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
  215. data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
  216. data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
  217. data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
  218. data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
  219. data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
  220. data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
  221. data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
  222. data/vendor/client/assets/ibm-plex-sans-cyrillic-ext-wght-normal-d45eAU9y.woff2 +0 -0
  223. data/vendor/client/assets/ibm-plex-sans-cyrillic-wght-normal-BAAhND-U.woff2 +0 -0
  224. data/vendor/client/assets/ibm-plex-sans-greek-wght-normal-CmyJS8uq.woff2 +0 -0
  225. data/vendor/client/assets/ibm-plex-sans-latin-ext-wght-normal-CIII54If.woff2 +0 -0
  226. data/vendor/client/assets/ibm-plex-sans-latin-wght-normal-IvpUvPa2.woff2 +0 -0
  227. data/vendor/client/assets/ibm-plex-sans-vietnamese-wght-normal-Dg1JeJN0.woff2 +0 -0
  228. data/vendor/client/assets/index-B51aUaAY.js +43 -0
  229. data/vendor/client/assets/index-Cs9rCoWM.css +2 -0
  230. data/vendor/client/assets/input-DByFmQPa.js +1 -0
  231. data/vendor/client/assets/json-De3PN-R6.css +1 -0
  232. data/vendor/client/assets/json-oTp7Q-d-.js +5 -0
  233. data/vendor/client/assets/media-D4a_En8R.js +1 -0
  234. data/vendor/client/assets/plus-CfJ9vo8X.js +1 -0
  235. data/vendor/client/assets/popover-CDtI9R8Q.js +1 -0
  236. data/vendor/client/assets/purify-G7QKJyJq.js +1 -0
  237. data/vendor/client/assets/query-CCsZN2fa.js +1 -0
  238. data/vendor/client/assets/recent-BrzLwL0D.js +1 -0
  239. data/vendor/client/assets/rich-BQc_q5T7.js +1 -0
  240. data/vendor/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  241. data/vendor/client/assets/select-C43qDA3k.js +1 -0
  242. data/vendor/client/assets/sheet-BPl2n-4n.js +1 -0
  243. data/vendor/client/assets/textarea-BnOphXry.js +1 -0
  244. data/vendor/client/assets/uiMode-z0ZbRV49.js +1 -0
  245. data/vendor/client/assets/uiToggle-DwnRQTH1.js +1 -0
  246. data/vendor/client/assets/useRecordForm-X_DExEP2.js +1 -0
  247. data/vendor/client/assets/useRecordVisit-BRtfMi-q.js +1 -0
  248. data/vendor/client/assets/utils-DwBGbDkG.js +1 -0
  249. data/vendor/client/assets/vendor-calendar-nTZiq9oY.js +1 -0
  250. data/vendor/client/assets/vendor-datefns-BoC6MCnl.js +1 -0
  251. data/vendor/client/assets/vendor-forms-D0h8hG_S.js +55 -0
  252. data/vendor/client/assets/vendor-purify-BXoPR7WT.js +3 -0
  253. data/vendor/client/assets/vendor-query-DZ6APRNI.js +1 -0
  254. data/vendor/client/assets/vendor-react-C1iE8mnY.js +9 -0
  255. data/vendor/client/assets/vendor-router-CI6Vh4k_.js +1 -0
  256. data/vendor/client/assets/vendor-table-DuQ835by.js +4 -0
  257. data/vendor/client/index.html +24 -0
  258. data/vendor/client/manifest.json +867 -0
  259. data/vendor/client/reactiveadmin-client.json +4 -0
  260. metadata +350 -0
@@ -0,0 +1,291 @@
1
+ module ReactiveAdmin
2
+ # Menu placement declared in one place, on top of ActiveAdmin's own menu (which stays the
3
+ # fallback for anything not placed):
4
+ #
5
+ # ReactiveAdmin.menu do
6
+ # group "Setup", priority: 90, icon: "settings" do
7
+ # group "Billing" do
8
+ # resources :billing_plans, :invoices
9
+ # page :batch_operations
10
+ # link "Sidekiq", url: "/sidekiq", if: -> { current_active_admin_user.superadmin? }
11
+ # end
12
+ # end
13
+ # hide :legacy_reports, :batch_operations
14
+ # end
15
+ #
16
+ # Precedence: overlay > `reactive { menu path: [...] }` on the resource > AA's `menu parent:`.
17
+ # Nodes the user may not see stay out, and groups left empty are dropped. Placing a resource or page
18
+ # that AA hides (`menu false`) puts it in this menu only; AA's own menu is untouched.
19
+ class MenuOverlay
20
+ MAX_DEPTH = 3
21
+ DEFAULT_PRIORITY = 10
22
+ UNSEQUENCED = 1_000_000
23
+
24
+ class DepthError < StandardError; end
25
+
26
+ Entry = Struct.new(:kind, :key, :label, :id, :priority, :description, :icon, :url, :display_if, :verb, :children)
27
+
28
+ class Builder
29
+ attr_reader :entries
30
+
31
+ def initialize(depth)
32
+ @depth = depth
33
+ @entries = []
34
+ end
35
+
36
+ # Groups hold items one level below, so a group may not sit at the last level.
37
+ def group(label, priority: nil, description: nil, icon: nil, id: nil, &block)
38
+ MenuOverlay.check_depth!(@depth + 1, "group #{label.inspect}")
39
+ child = Builder.new(@depth + 1).tap { it.instance_exec(&block) if block }
40
+ @entries << Entry.new(kind: :group, label:, id:, priority:, description:, icon:, children: child.entries)
41
+ end
42
+
43
+ def resource(key, label: nil, priority: nil, description: nil, icon: nil)
44
+ @entries << Entry.new(kind: :resource, key: key.to_s, label:, priority:, description:, icon:)
45
+ end
46
+
47
+ def resources(*keys) = keys.each { resource(it) }
48
+
49
+ def page(key, label: nil, priority: nil, description: nil, icon: nil)
50
+ @entries << Entry.new(kind: :page, key: key.to_s, label:, priority:, description:, icon:)
51
+ end
52
+
53
+ # Resources and pages intentionally left out of the React menu: not placed, not reported as unplaced.
54
+ def hide(*keys) = keys.each { @entries << Entry.new(kind: :hide, key: it.to_s) }
55
+
56
+ # `if:` and a proc `url:` run in the view like AA's menu options, so `authorized?` and
57
+ # `current_active_admin_user` are available.
58
+ def link(label, url:, priority: nil, description: nil, icon: nil, id: nil, method: nil, if: nil)
59
+ @entries << Entry.new(kind: :link, label:, url:, id:, priority:, description:, icon:, verb: method, display_if: binding.local_variable_get(:if))
60
+ end
61
+ end
62
+
63
+ class << self
64
+ def define(&block)
65
+ @entries = [*entries, *Builder.new(1).tap { it.instance_exec(&block) }.entries]
66
+ @version = version + 1
67
+ end
68
+
69
+ def entries = @entries ||= []
70
+
71
+ def declared? = entries.any?
72
+
73
+ def reset!
74
+ @entries = []
75
+ @version = version + 1
76
+ @audit = nil
77
+ end
78
+
79
+ def version = @version ||= 0
80
+
81
+ # Depth beyond what the client lays out is a mistake worth failing on early.
82
+ def check_depth!(depth, what)
83
+ return if depth <= MAX_DEPTH
84
+
85
+ message = "reactiveadmin menu: #{what} is nested #{depth} levels deep; the maximum is #{MAX_DEPTH}"
86
+ raise DepthError, message if Rails.env.local?
87
+
88
+ Rails.logger.warn(message)
89
+ ReactiveAdmin.diagnostics.record(:menu_too_deep, item: what.to_s, depth:)
90
+ end
91
+
92
+ # Explicit `link` entries: the labels leading to each and the id its menu node gets.
93
+ def links(list = entries, path = [])
94
+ list.flat_map do |entry|
95
+ case entry.kind
96
+ when :group then links(entry.children, [*path, entry.label])
97
+ when :link then [{id: link_id(entry, path), path: [*path, entry.label], url: (entry.url if entry.url.is_a?(String))}]
98
+ else []
99
+ end
100
+ end
101
+ end
102
+
103
+ def link_id(entry, path) = entry.id || [*path.map { slug(it) }, slug(entry.label)].join("/")
104
+
105
+ def slug(value) = value.to_s.downcase.tr(" ", "_")
106
+
107
+ def keys(kind, list = entries)
108
+ list.flat_map { (it.kind == :group) ? keys(kind, it.children) : (it.key if it.kind == kind) }.compact
109
+ end
110
+
111
+ # Mismatches between the overlay and what is registered; resource keys only, never data.
112
+ def audit
113
+ stamp = [version, Registry.index.object_id]
114
+ return @audit[:entries] if @audit && @audit[:stamp] == stamp
115
+
116
+ found = compute_audit
117
+ @audit = {stamp:, entries: found}
118
+ found
119
+ end
120
+
121
+ def record_audit = audit.each { ReactiveAdmin.diagnostics.record(it[:kind], **it.except(:kind)) }
122
+
123
+ # Whether the overlay can give the config a top-level entry: a resource needs an index, and one that
124
+ # only exists under its parent has no top-level URL (AA lists it in the parent's own menu).
125
+ def placeable?(config)
126
+ (AA.in_menu?(config) || !Paths.nested?(config)) && (AA.page?(config) || Registry.actions(config).include?(:index))
127
+ end
128
+
129
+ private
130
+
131
+ def compute_audit
132
+ return [] unless declared?
133
+
134
+ resources = keys(:resource)
135
+ pages = keys(:page)
136
+ hidden = keys(:hide)
137
+ placed_resources = resources + hidden + Registry.resources.filter_map { Registry.route_key(it) if it.reactive_options[:menu]&.key?(:path) }
138
+ registered = [*Registry.resources.map { Registry.route_key(it) }, *menu_pages.map { Menu.page_key(it) }]
139
+ [
140
+ *(hidden - registered).uniq.map { {kind: :menu_unknown, item: "hide", key: it} },
141
+ *duplicates(:resource, resources), *duplicates(:page, pages),
142
+ *unknown(:resource, resources, Registry.resources.to_h { [Registry.route_key(it), it] }),
143
+ *unknown(:page, pages, menu_pages.to_h { [Menu.page_key(it), it] }),
144
+ *unplaced(:resource, Registry.resources.select { Registry.actions(it).include?(:index) }.to_h { [Registry.route_key(it), it] }, placed_resources),
145
+ *unplaced(:page, menu_pages.to_h { [Menu.page_key(it), it] }, pages + hidden)
146
+ ]
147
+ end
148
+
149
+ def menu_pages = Registry.pages
150
+
151
+ def duplicates(kind, keys) = keys.tally.select { |_, n| n > 1 }.keys.map { {kind: :menu_duplicate, item: kind.to_s, key: it} }
152
+
153
+ def unknown(kind, keys, registered)
154
+ keys.uniq.filter_map do |key|
155
+ config = registered[key]
156
+ if config.nil?
157
+ {kind: :menu_unknown, item: kind.to_s, key:}
158
+ elsif !placeable?(config) || !Exposure.allowed?(config)
159
+ {kind: :menu_hidden, item: kind.to_s, key:}
160
+ end
161
+ end
162
+ end
163
+
164
+ def unplaced(kind, registered, placed)
165
+ registered.filter_map { |key, config| {kind: :menu_unplaced, item: kind.to_s, key:} if AA.in_menu?(config) && !placed.include?(key) }
166
+ end
167
+ end
168
+
169
+ # Rearranges evaluated menu nodes (hashes with :children) per the per-resource hints and the
170
+ # overlay. Nodes are moved, never copied, so each resource appears once.
171
+ class Layout
172
+ def initialize(roots, evaluate:, build:)
173
+ @roots = roots
174
+ @evaluate = evaluate
175
+ @build = build
176
+ @overlay_placed = Set.new
177
+ end
178
+
179
+ def call
180
+ place_declared_resources
181
+ place(@roots, MenuOverlay.entries, [])
182
+ MenuOverlay.keys(:hide).each { take(:resource, it) || take(:page, it) }
183
+ @roots
184
+ end
185
+
186
+ private
187
+
188
+ def place_declared_resources
189
+ Registry.resources.each do |config|
190
+ hint = config.reactive_options[:menu] or next
191
+ key = Registry.route_key(config)
192
+ node = hint.key?(:path) ? take(:resource, key) : find(@roots, :resource, key)&.first
193
+ next unless node
194
+
195
+ style(node, hint)
196
+ attach(group_for(hint[:path]), node) if hint.key?(:path)
197
+ end
198
+ end
199
+
200
+ def place(container, entries, path)
201
+ entries.each_with_index do |entry, seq|
202
+ case entry.kind
203
+ when :group
204
+ group = find_or_create_group(container, entry, path)
205
+ group[:seq] = seq
206
+ style(group, entry.to_h)
207
+ place(group[:children], entry.children, [*path, entry.label])
208
+ when :resource, :page
209
+ next unless @overlay_placed.add?([entry.kind, entry.key])
210
+
211
+ node = take(entry.kind, entry.key) || build_hidden(entry.kind, entry.key) or next
212
+ attach(container, style(node.merge!(priority: DEFAULT_PRIORITY, seq:), entry.to_h))
213
+ when :link
214
+ next unless entry.display_if.nil? || @evaluate.call(entry.display_if)
215
+
216
+ node = link_node(entry, path, seq)
217
+ drop_inherited_link(node[:id])
218
+ attach(container, node)
219
+ end
220
+ end
221
+ end
222
+
223
+ # A host-declared link replaces the AA menu item it redeclares, so it isn't listed twice.
224
+ def drop_inherited_link(id, nodes = @roots)
225
+ nodes.reject! { it[:id] == id && it[:kind].to_s == "link" }
226
+ nodes.each { drop_inherited_link(id, it[:children]) if it[:children] }
227
+ end
228
+
229
+ # A resource AA keeps out of its menu has no node to move; the overlay's placement is the decision to show it.
230
+ def build_hidden(kind, key)
231
+ config = (kind == :page) ? Registry.find_page(key) : Registry.find(key)
232
+ return unless config && !AA.in_menu?(config) && MenuOverlay.placeable?(config)
233
+
234
+ node = @build.call(config)
235
+ node if node && (node[:resource] || node[:page])
236
+ end
237
+
238
+ def style(node, options)
239
+ node[:label] = options[:label] if options[:label]
240
+ node[:priority] = options[:priority] if options[:priority]
241
+ node[:description] = options[:description] if options[:description]
242
+ node[:icon] = options[:icon] if options[:icon]
243
+ node
244
+ end
245
+
246
+ def attach(container, node) = container << node
247
+
248
+ def link_node(entry, path, seq)
249
+ {id: MenuOverlay.link_id(entry, path), label: entry.label, kind: "link",
250
+ url: @evaluate.call(entry.url).to_s, method: entry.verb&.to_s, priority: entry.priority || DEFAULT_PRIORITY,
251
+ description: entry.description, icon: entry.icon, seq:, children: []}.compact.merge(resource: nil)
252
+ end
253
+
254
+ def group_for(path, container = @roots, trail = [])
255
+ path.to_a.each do |label|
256
+ group = container.find { group?(it, label) } || new_group(container, label, trail)
257
+ container = group[:children]
258
+ trail += [label]
259
+ end
260
+ container
261
+ end
262
+
263
+ def find_or_create_group(container, entry, path)
264
+ container.find { group?(it, entry.label) } || new_group(container, entry.label, path, id: entry.id)
265
+ end
266
+
267
+ def new_group(container, label, path, id: nil)
268
+ {id: id || [*path.map { slug(it) }, slug(label)].join("/"), label:, kind: "group", priority: DEFAULT_PRIORITY, children: []}.tap { container << it }
269
+ end
270
+
271
+ def group?(node, label) = node[:kind] == "group" && slug(node[:label]) == slug(label)
272
+
273
+ def slug(value) = MenuOverlay.slug(value)
274
+
275
+ def find(nodes, kind, key)
276
+ nodes.each do |node|
277
+ return [node, nodes] if node[kind] == key
278
+ found = find(node[:children], kind, key)
279
+ return found if found
280
+ end
281
+ nil
282
+ end
283
+
284
+ def take(kind, key)
285
+ node, siblings = find(@roots, kind, key)
286
+ siblings&.delete(node)
287
+ node
288
+ end
289
+ end
290
+ end
291
+ end
@@ -0,0 +1,47 @@
1
+ module ReactiveAdmin
2
+ # Typed nodes the gem itself emits into rendered HTML (show-row JSON values) travel as opaque
3
+ # ids into a per-request table. Markup can't carry a node: host-rendered HTML may hold user
4
+ # text, and anything it says about a node (a `data-node` attribute, a guessed id) resolves to
5
+ # nothing. Ids are random and die with the scope, so they never resolve in another request.
6
+ module NodeRegistry
7
+ CLASS_NAME = "reactiveadmin-node".freeze
8
+ LEGACY_CLASS_NAME = "reactiveadmin-json".freeze
9
+ ALLOWED_TYPES = %w[json text empty form].freeze
10
+ KEY = :reactiveadmin_node_registry
11
+ STATE = ActiveSupport::IsolatedExecutionState
12
+
13
+ module_function
14
+
15
+ def with_scope
16
+ previous = STATE[KEY]
17
+ STATE[KEY] = {}
18
+ yield
19
+ ensure
20
+ STATE[KEY] = previous
21
+ end
22
+
23
+ # Outside a scope the id resolves to nothing, which is right for renders whose HTML is discarded.
24
+ def register(node)
25
+ type = node[:type].to_s
26
+ raise ArgumentError, "node type #{type.inspect} can't travel through markup" unless ALLOWED_TYPES.include?(type)
27
+
28
+ SecureRandom.hex(16).tap { |id| STATE[KEY]&.store(id, node.freeze) }
29
+ end
30
+
31
+ def resolve(id) = STATE[KEY]&.[](id.to_s)
32
+
33
+ def marker(node) = %(<span class="#{CLASS_NAME}" data-node-id="#{register(node)}"></span>).html_safe
34
+
35
+ # The node a marker element stands for, or nil with a degradation when this request never
36
+ # issued it.
37
+ def from_html(el)
38
+ node = resolve(el["data-node-id"]) if el.classes.include?(CLASS_NAME)
39
+ return node if node
40
+
41
+ Degradation.record(kind: :forged_node_marker, where: Degradation.where, reason: "Markup claimed to be a typed node; dropped")
42
+ nil
43
+ end
44
+
45
+ def marker?(el) = (el.classes & [CLASS_NAME, LEGACY_CLASS_NAME]).any?
46
+ end
47
+ end
@@ -0,0 +1,156 @@
1
+ module ReactiveAdmin
2
+ module OpenApi
3
+ # Assembles the document: the resources and pages the user may open, the metadata endpoints, and
4
+ # only those of core's component schemas the paths reach. Keys and operations come out in a fixed
5
+ # order, so two builds of the same code and permissions are equal and a diff shows real changes.
6
+ class Builder
7
+ include Parts
8
+
9
+ META_TAG = "meta".freeze
10
+ DEFAULT_INFO = {title: "Admin API"}.freeze
11
+ REF = %r{\A#/components/(?<kind>\w+)/(?<name>.+)\z}
12
+
13
+ def initialize(user, controller:, full:, channel: nil)
14
+ @ctx = Context.new(user, controller:, full:)
15
+ @channel = channel || :session
16
+ end
17
+
18
+ def call = ApiGuards.with_channel(@channel) { document }
19
+
20
+ private
21
+
22
+ def document
23
+ resources = documents(@ctx.resources.sort_by { it[:key] }) { ResourceDoc.new(@ctx, it) }
24
+ pages = documents(readable_pages.sort_by { Menu.page_key(it) }) { PageDoc.new(@ctx, it) }
25
+ tags = [*resources.pluck(:tag), *pages.pluck(:tag), meta_tag]
26
+ paths = ordered([meta_paths, *resources.pluck(:paths), *pages.pluck(:paths)].reduce({}, :merge)).deep_stringify_keys
27
+ {
28
+ openapi: SPEC_VERSION,
29
+ info:, servers: [{url: "/"}],
30
+ tags: arrange(tags, resources, pages), "x-tagGroups": groups(resources, pages), security: Security.requirement(write: false)
31
+ }.deep_stringify_keys.merge("paths" => paths, "components" => components(paths, resources))
32
+ end
33
+
34
+ def info = DEFAULT_INFO.merge(version: VERSION).merge(ReactiveAdmin.config.openapi.info.to_h.deep_symbolize_keys)
35
+
36
+ # A resource or page whose document can't be built is left out and reported, not a failed document.
37
+ def documents(items)
38
+ items.filter_map do |item|
39
+ yield(item).call
40
+ rescue => e
41
+ Degradation.record(kind: :openapi_document_dropped, where: "openapi/#{item.try(:name) || item[:key]}", reason: "Document raised #{e.class}; left out")
42
+ nil
43
+ end
44
+ end
45
+
46
+ def readable_pages
47
+ Registry.pages.select { Exposure.allowed?(it) && !ApiGuards.denies?(it, "index") && @ctx.authorized?(AA::Auth::READ, it, it) }
48
+ end
49
+
50
+ def ordered(paths) = paths.transform_values { |item| item.sort_by { |verb, _| VERBS.index(verb.to_s) }.to_h }
51
+
52
+ # Metadata
53
+
54
+ def meta_tag
55
+ {name: META_TAG, "x-displayName": "Session and metadata", description: "The manifest, the session and CSRF token, and this document."}
56
+ end
57
+
58
+ def meta_paths
59
+ ns = "/#{ReactiveAdmin.config.namespace}"
60
+ {
61
+ Paths.api("#{ns}/_manifest") => {get: meta_operation("getManifest", "Manifest of served resources and the menu", success("The manifest", schema_ref("Manifest")), [403])},
62
+ Paths.api("#{ns}/_session") => {get: meta_operation("getSession", "Current user and CSRF token", success("The session", schema_ref("Session")), [])},
63
+ Paths.api("#{ns}/_openapi") => {get: meta_operation("getOpenApi", "This OpenAPI document", openapi_response, [403])}
64
+ }.merge(mcp_paths(ns))
65
+ end
66
+
67
+ # Only where a token can call it: a bearer adapter is configured and MCP is on for this user.
68
+ def mcp_paths(ns)
69
+ mcp = ReactiveAdmin.config.mcp
70
+ return {} unless Auth.bearer? && (@ctx.full? ? mcp.possibly_enabled? : mcp.enabled_for?(@ctx.user))
71
+
72
+ operation = Core.operation("/admin/_mcp", "post").merge("tags" => [META_TAG], "security" => Security.token_requirement)
73
+ operation["operationId"] = @ctx.ids.claim(operation["operationId"])
74
+ {Paths.api("#{ns}/_mcp") => {post: operation}}
75
+ end
76
+
77
+ def meta_operation(id, summary, response, errors)
78
+ operation(id: @ctx.ids.claim(id), summary:, tags: [META_TAG], responses: {200 => response}, errors:)
79
+ end
80
+
81
+ def openapi_response
82
+ success("An OpenAPI 3.1 document of what the user may do, as plain JSON", {type: "object", additionalProperties: true})
83
+ end
84
+
85
+ # Tags in the order of their groups, so a viewer's sidebar follows the admin's own menu.
86
+
87
+ def arrange(tags, resources, pages)
88
+ order = group_names(resources, pages).values.flatten
89
+ by_name = tags.index_by { it[:name] }
90
+ [*order.filter_map { by_name[it] }, by_name[META_TAG]]
91
+ end
92
+
93
+ def groups(resources, pages)
94
+ group_names(resources, pages).map { |name, tags| {name:, tags:} } + [{name: "Meta", tags: [META_TAG]}]
95
+ end
96
+
97
+ def group_names(resources, pages)
98
+ names = resources.map { it.dig(:tag, :name) } + pages.map { it.dig(:tag, :name) }
99
+ placed = menu_groups.transform_values { |tags| tags & names }.reject { |_, tags| tags.empty? }
100
+ rest = names - placed.values.flatten
101
+ rest.empty? ? placed : placed.merge("Other" => rest)
102
+ end
103
+
104
+ # Group name (the top-level menu group) to the tags of the resources and pages placed in it.
105
+ def menu_groups
106
+ walk(@ctx.manifest.menu(@ctx.resources).as_json, nil, {})
107
+ rescue => e
108
+ Degradation.record(kind: :openapi_degraded, where: "openapi/menu", reason: "Menu raised #{e.class}; tags are not grouped")
109
+ {}
110
+ end
111
+
112
+ def walk(nodes, group, found)
113
+ nodes.each do |node|
114
+ tag = node[:resource] || (PageDoc.tag_name(Registry.find_page(node[:page])) if node[:page])
115
+ (found[group || (node[:page] ? "Pages" : "Resources")] ||= []) << tag if tag
116
+ walk(node[:children], group || (node[:label] if node[:kind] == "group"), found)
117
+ end
118
+ found.transform_values(&:uniq)
119
+ end
120
+
121
+ # Components: core's, plus the generated ones, kept only where a path reaches them.
122
+
123
+ def components(paths, resources)
124
+ pool = Core::KINDS.to_h { [it, Core.components(it).deep_dup] }
125
+ pool["schemas"] = pool["schemas"].merge(resources.pluck(:schemas).reduce({}, :merge).deep_stringify_keys) { |name, *| raise "reactiveadmin: generated schema #{name} clashes with core's" }
126
+ pool["parameters"] = pool["parameters"].merge(resources.pluck(:parameters).reduce({}, :merge))
127
+ pool["responses"] = pool["responses"].merge(RESPONSES.deep_stringify_keys)
128
+ used = reachable(paths, pool)
129
+ used.transform_values { |entries| entries.sort.to_h }.merge("securitySchemes" => Security.schemes).sort.to_h
130
+ end
131
+
132
+ def reachable(paths, pool)
133
+ used = Hash.new { |hash, kind| hash[kind] = {} }
134
+ queue = refs_in(paths)
135
+ until queue.empty?
136
+ match = REF.match(queue.shift) or next
137
+ kind = match[:kind]
138
+ name = match[:name]
139
+ next if used[kind].key?(name) || !pool.dig(kind, name)
140
+
141
+ used[kind][name] = pool.dig(kind, name)
142
+ queue.concat(refs_in(used[kind][name]))
143
+ end
144
+ used
145
+ end
146
+
147
+ def refs_in(node)
148
+ case node
149
+ when Hash then node.flat_map { |key, value| (key == "$ref") ? [value] : refs_in(value) }
150
+ when Array then node.flat_map { refs_in(it) }
151
+ else []
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,46 @@
1
+ module ReactiveAdmin
2
+ module OpenApi
3
+ # Who the document is for and how each resource is probed. Probes are controllers of the resource,
4
+ # outside a request, bound to the user, so AA's procs (`if:`, scopes, filters) answer as they would for them.
5
+ class Context
6
+ Host = Struct.new(:controller)
7
+
8
+ attr_reader :user, :ids
9
+
10
+ # `controller` is the request's own when there is one (the endpoint); otherwise a stand-in.
11
+ def initialize(user, controller:, full:)
12
+ @user = user
13
+ @full = full
14
+ @probes = {}
15
+ @ids = OperationIds.new
16
+ @controller = controller || Doctor::Sample.new(Host.new(ManifestController), view_user).controller
17
+ end
18
+
19
+ def full? = @full
20
+
21
+ def authorizer = @authorizer ||= @full ? Authorizer::Permissive.new : Authorizer.for(@controller)
22
+
23
+ def manifest = @manifest ||= Manifest.new(@controller, authorizer:)
24
+
25
+ # The summaries of the resources the user may open (what `_manifest` lists), built once.
26
+ def resources = @resources ||= manifest.resources
27
+
28
+ def probe(config) = (@probes[config] ||= Doctor::Sample.new(config, view_user).tap { permit_everything(it.controller) if @full })
29
+
30
+ # Whether the user may do `action` to the resource or page; anything that raises is denied.
31
+ def authorized?(action, subject, config)
32
+ authorizer.authorized?(action, subject, config:)
33
+ rescue
34
+ false
35
+ end
36
+
37
+ private
38
+
39
+ # Views ask their controller before showing a batch action or a link; a full document shows them all.
40
+ def permit_everything(controller) = controller.define_singleton_method(:authorized?) { |*| true }
41
+
42
+ # A full document has no user; probes still need one for procs that read it.
43
+ def view_user = @full ? ReactiveAdmin.config.doctor_user&.call : @user
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ require "yaml"
2
+
3
+ module ReactiveAdmin
4
+ module OpenApi
5
+ # openapi/core.yaml (OpenAPI 3.0, packaged with the gem) as a source of 3.1 components and operation templates.
6
+ module Core
7
+ PATH = File.expand_path("../../../openapi/core.yaml", __dir__)
8
+ KINDS = %w[parameters headers requestBodies responses schemas].freeze
9
+
10
+ module_function
11
+
12
+ def document = @document ||= begin
13
+ raise "reactiveadmin: #{PATH} is missing; the OpenAPI document embeds its schemas" unless File.exist?(PATH)
14
+
15
+ convert(YAML.safe_load_file(PATH, aliases: true)).freeze
16
+ end
17
+
18
+ def components(kind) = document.dig("components", kind) || {}
19
+
20
+ # A copy of one of core's operations, for the caller to specialize.
21
+ def operation(path, verb) = document.dig("paths", path, verb).deep_dup
22
+
23
+ # `nullable` and `example` became type arrays and `examples` in JSON Schema 2020-12.
24
+ def convert(node, names: false)
25
+ case node
26
+ when Array then node.map { convert(it) }
27
+ when Hash then names ? node.transform_values { convert(it) } : schema(node)
28
+ else node
29
+ end
30
+ end
31
+
32
+ def schema(node)
33
+ node = node.dup
34
+ nullable = node.delete("nullable")
35
+ node["examples"] = [node.delete("example")] if node.key?("example")
36
+ converted = node.to_h { |key, value| [key, convert(value, names: key == "properties")] }
37
+ nullable ? allow_null(converted) : converted
38
+ end
39
+
40
+ def allow_null(node)
41
+ node["enum"] = [*node["enum"], nil].uniq if node["enum"]
42
+ case (type = node["type"])
43
+ when String then node.merge("type" => [type, "null"])
44
+ when Array then node.merge("type" => [*type, "null"].uniq)
45
+ when nil then (node.key?("$ref") || node.key?("oneOf") || node.key?("allOf")) ? {"anyOf" => [node.except("description"), {"type" => "null"}], **node.slice("description")} : node
46
+ else node
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,18 @@
1
+ module ReactiveAdmin
2
+ module OpenApi
3
+ # operationIds are unique across the document; a clash gets a counter, in build order, so ids stay stable.
4
+ class OperationIds
5
+ def initialize
6
+ @taken = Set.new
7
+ end
8
+
9
+ def claim(base)
10
+ id = base
11
+ count = 1
12
+ id = "#{base}#{count += 1}" while @taken.include?(id)
13
+ @taken << id
14
+ id
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,75 @@
1
+ module ReactiveAdmin
2
+ module OpenApi
3
+ # The paths and tag of one `register_page`: the page itself and its page actions. The fields of a form
4
+ # in the page's content come with the page response (`forms`); rendering the content here would read data.
5
+ class PageDoc
6
+ include Parts
7
+
8
+ attr_reader :paths
9
+
10
+ def self.tag_name(page) = "page:#{Menu.page_key(page)}"
11
+
12
+ def initialize(context, page)
13
+ @ctx = context
14
+ @page = page
15
+ @key = Menu.page_key(page)
16
+ @label = page.name.to_s
17
+ @nested = Paths.nested?(page)
18
+ @paths = {}
19
+ end
20
+
21
+ def call
22
+ read
23
+ actions
24
+ {tag: {name: self.class.tag_name(@page), "x-displayName": @label, description: "The #{@label} page and its page actions."}, paths:}
25
+ end
26
+
27
+ private
28
+
29
+ def tags = [self.class.tag_name(@page)]
30
+
31
+ def prefix
32
+ return "/#{ReactiveAdmin.config.namespace}/" unless @nested
33
+
34
+ parent = @page.belongs_to_config
35
+ "/#{ReactiveAdmin.config.namespace}/#{Registry.route_key(parent.target)}/{#{parent.to_param}}/"
36
+ end
37
+
38
+ def parameters = (@nested ? [path_param(@page.belongs_to_config.to_param.to_s, "Id of the parent record")] : [])
39
+
40
+ def add(path, verb, operation, names: nil) = ((@paths[path] ||= {})[verb] = names ? operation.merge(guard_extensions(@page, names)) : operation)
41
+
42
+ def id(name) = @ctx.ids.claim(name)
43
+
44
+ def read
45
+ add(Paths.api("#{prefix}_pages/#{@key}"), "get", operation(
46
+ id: id("get#{camel(@key)}Page"), summary: "#{@label} page", tags:, parameters: [*parameters, *([form_query_param] if Ladder.page(@page).get_form)],
47
+ description: "The page's content as typed nodes, its action items and the GET page actions that render something.",
48
+ responses: {200 => success("The page", schema_ref("PageResponse"), degraded: true)}, errors: [403, 404]
49
+ ), names: %w[index])
50
+ end
51
+
52
+ def actions
53
+ @page.page_actions.sort_by { it.name.to_s }.each do |declaration|
54
+ name = declaration.name.to_s
55
+ next unless @ctx.authorized?(name.to_sym, @page, @page)
56
+
57
+ verbs = verbs_of(declaration)
58
+ verbs.each { action(name, it, suffix: (camel(it) if verbs.size > 1)) unless ApiGuards.denies?(@page, name, verb: it) }
59
+ end
60
+ end
61
+
62
+ def action(name, verb, suffix:)
63
+ get = verb == "get"
64
+ add(Paths.api("#{prefix}#{@key}/#{name}"), verb, operation(
65
+ id: id("#{camel(@key).camelize(:lower)}#{camel(name)}#{suffix}"), summary: "#{name.humanize} (page action)", tags:, write: !get,
66
+ parameters: [*parameters, *([form_query_param] if get && Ladder.action(@page, name)&.get_form)],
67
+ description: get ? "Answers the action's template as a form." : "Page action `#{name}`, usually the target of a form in the page's content. The redirect and flash become the result; an alert answers 422.",
68
+ request_body: (ref("requestBodies", "ActionParams") unless get),
69
+ responses: {200 => get ? shared("FormOk") : ref("responses", "ActionResult")},
70
+ errors: [403, *(422 unless get)]
71
+ ), names: [name])
72
+ end
73
+ end
74
+ end
75
+ end