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,71 @@
1
+ module ReactiveAdmin
2
+ # Optimistic-lock token of a persisted record: Rails' own `lock_version` when the model has one,
3
+ # else `updated_at`. `updated_at` doesn't move when only nested rows change, so a form with
4
+ # has_many sections adds a digest of those rows' ids and timestamps to the token.
5
+ class FormLock
6
+ DIGEST_SEPARATOR = "~".freeze
7
+ STALE_MESSAGE = "This record changed since you opened it.".freeze
8
+
9
+ attr_reader :field
10
+
11
+ def self.field_for(klass)
12
+ if klass.locking_enabled? then "lock_version"
13
+ elsif klass.column_names.include?("updated_at") then "updated_at"
14
+ end
15
+ end
16
+
17
+ def self.for(record, nested: [])
18
+ field = field_for(record.class) if record.persisted?
19
+ new(record, field, nested:) if field && !record.public_send(field).nil?
20
+ end
21
+
22
+ def initialize(record, field, nested: [])
23
+ @record = record
24
+ @field = field
25
+ @nested = (field == "updated_at") ? nested.to_a : []
26
+ end
27
+
28
+ def value
29
+ current = @record.public_send(field)
30
+ return current.to_s unless field == "updated_at"
31
+
32
+ [current.utc.iso8601(6), (digest if @nested.any?)].compact.join(DIGEST_SEPARATOR)
33
+ end
34
+
35
+ def as_json(*) = {field:, value:}
36
+
37
+ # `supplied` is the `lock` the client sent back: the object it was given, or just its value.
38
+ def matches?(supplied)
39
+ supplied = supplied.to_unsafe_h if supplied.respond_to?(:to_unsafe_h)
40
+ supplied = supplied.stringify_keys if supplied.is_a?(Hash)
41
+ sent_field, sent = supplied.is_a?(Hash) ? supplied.values_at("field", "value") : [nil, supplied]
42
+ return false if sent_field && sent_field.to_s != field
43
+
44
+ same_value?(sent.to_s)
45
+ end
46
+
47
+ private
48
+
49
+ def same_value?(sent)
50
+ return sent == value unless field == "updated_at"
51
+
52
+ time, sent_digest = sent.split(DIGEST_SEPARATOR, 2)
53
+ [Time.iso8601(time).utc.iso8601(6), sent_digest].compact.join(DIGEST_SEPARATOR) == value
54
+ rescue ArgumentError
55
+ false
56
+ end
57
+
58
+ # Read fresh: the association may hold rows loaded before another editor changed them.
59
+ def digest
60
+ rows = @nested.sort.map { |name| [name, nested_rows(@record.public_send(name))] }
61
+ Digest::SHA256.hexdigest(rows.to_json)[0, 16]
62
+ end
63
+
64
+ def nested_rows(relation)
65
+ klass = relation.klass
66
+ stamp = klass.locking_column if klass.locking_enabled?
67
+ stamp ||= "updated_at" if klass.column_names.include?("updated_at")
68
+ relation.reorder(nil).pluck(*Array(klass.primary_key), *stamp).map { Array(it).map { |v| v.respond_to?(:utc) ? v.utc.iso8601(6) : v } }.sort_by(&:to_s)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,57 @@
1
+ module ReactiveAdmin
2
+ # FormResponse for the new and edit actions.
3
+ class FormPayload
4
+ # A custom `new`/`edit` may have already prepared its own record.
5
+ def self.record_for(controller)
6
+ AA.resource_ivar(controller) ||
7
+ (controller.params[:id].present? ? AA.resource(controller) : AA.build_resource(controller))
8
+ end
9
+
10
+ def initialize(controller, only: nil)
11
+ @controller = controller
12
+ @only = only
13
+ @config = AA.config_of(controller)
14
+ @authorizer = Authorizer.for(controller)
15
+ @record = self.class.record_for(controller)
16
+ end
17
+
18
+ def schema = @schema ||= FormSchema.new(@controller, @record, only: @only)
19
+
20
+ def as_json(*)
21
+ Degradation.attach({
22
+ resource: Registry.route_key(@config),
23
+ id: schema.record_id,
24
+ title: title,
25
+ display_name: (Values.display_name(@record, @controller.view_context) if @record.persisted?),
26
+ actions: ActionItems.new(@controller, @record.persisted? ? "edit" : "new", subject: @record).call,
27
+ action: action,
28
+ form: schema.as_json,
29
+ submit_confirm: schema.submit_confirm,
30
+ submittable: schema.submittable?,
31
+ unsupported_reason: schema.unsupported_reason,
32
+ lock: FormLock.for(@record, nested: schema.nested_associations)&.as_json,
33
+ permissions: permissions
34
+ }.compact)
35
+ end
36
+
37
+ private
38
+
39
+ def title
40
+ custom = AA.page_presenter(@config, :form)&.[](:title)
41
+ text = custom ? AA.render_or_call(@controller.view_context, @record, custom) : default_title
42
+ CGI.unescapeHTML(text.to_s)
43
+ end
44
+
45
+ def default_title = AA.resource_text(@config, "#{@record.persisted? ? "edit" : "new"}_model")
46
+
47
+ def action
48
+ path = @record.persisted? ? @config.route_instance_path(@record) : @config.route_collection_path(@controller.params)
49
+ {method: @record.persisted? ? "patch" : "post", path: Paths.api(path)}
50
+ end
51
+
52
+ def permissions
53
+ perms = Permissions.new(@config, @authorizer)
54
+ @record.persisted? ? perms.record(@record) : perms.resource
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,192 @@
1
+ module ReactiveAdmin
2
+ # Stands in for AA's ActiveAdminForm while a `form do |f| ... end` block runs: inputs are
3
+ # recorded instead of rendered, and any other Arbre content (panels, paragraphs) is captured
4
+ # as typed `content` fields between them (see FormContent). An Element for the same reason as IndexRecorder.
5
+ class FormRecorder < AA.arbre_element
6
+ Holder = Class.new(AA.arbre_element)
7
+
8
+ NOOPS = %i[semantic_errors commit_action_with_cancel_link cancel_link].freeze
9
+
10
+ attr_reader :builder, :sections
11
+
12
+ def self.build(builder, schema, prefix: nil)
13
+ AA.arbre_context(schema.assigns, schema.view).insert_tag(self, builder, schema, prefix)
14
+ end
15
+
16
+ def build(builder, schema, prefix)
17
+ @builder = builder
18
+ @schema = schema
19
+ @prefix = prefix
20
+ @sections = []
21
+ @cuts = {}
22
+ end
23
+
24
+ def record(&block)
25
+ within(self) do
26
+ instance_exec(self, &block)
27
+ flush_html
28
+ end
29
+ settle
30
+ self
31
+ end
32
+
33
+ def object = builder.object
34
+
35
+ def form_builder = builder
36
+
37
+ NOOPS.each { |name| define_method(name) { |*, &| nil } }
38
+
39
+ # Only the submit button's confirm is kept; the block runs in a holder that is thrown away.
40
+ def actions(*, &block)
41
+ return unless block
42
+
43
+ holder = insert_tag(Holder) do
44
+ block.arity.zero? ? block.call : block.call(self)
45
+ nil
46
+ end
47
+ holder.parent&.remove_child(holder)
48
+ rescue => e
49
+ Rails.logger.warn("[reactiveadmin] #{@schema.config.resource_name} actions block skipped: #{e.class} at #{e.backtrace&.first}")
50
+ end
51
+
52
+ def action(name, options = {})
53
+ @schema.record_submit_confirm(FormSchema.confirm_of(options[:button_html])) if name.to_s == "submit"
54
+ nil
55
+ end
56
+
57
+ def inputs(*args, &block)
58
+ options = args.extract_options!
59
+ return nested_inputs(args, options, &block) if options[:for]
60
+
61
+ title = options[:name] || options[:title] || (args.shift if args.first.is_a?(String) || (block && args.first.is_a?(Symbol)))
62
+ flush_html
63
+ @section = new_section(title&.to_s)
64
+ if block
65
+ guard(title) { collect { block.arity.zero? ? block.call : block.call(self) } }
66
+ else
67
+ (args.presence || default_columns(options)).each { input(it) }
68
+ end
69
+ flush_html
70
+ @section = nil
71
+ end
72
+
73
+ def input(method, options = {}, &)
74
+ flush_html
75
+ field = FormField.new(builder, @schema, @prefix).call(method.to_sym, options)
76
+ section[:fields] << field if field
77
+ end
78
+
79
+ def has_many(assoc, options = {}, &block)
80
+ return unless @schema.wants_nested?(assoc.to_s)
81
+
82
+ flush_html
83
+ guard((options[:heading] if options[:heading].is_a?(String)) || assoc.to_s.humanize) { @sections << FormHasMany.new(self, @schema, assoc, options).call(&block) }
84
+ @section = nil
85
+ end
86
+
87
+ # has_one and other single nested records: the nested builder's own sections join this form's.
88
+ def semantic_fields_for(*args, &block)
89
+ name = args.first if args.first.is_a?(Symbol) || args.first.is_a?(String)
90
+ path = [@prefix, name].compact.join(".")
91
+ return unless @schema.wants_nested?(path)
92
+
93
+ flush_html
94
+ nested = nil
95
+ builder.fields_for(*args) { nested = it }
96
+ @sections.concat(nested_sections(nested, path.presence, &block)) if nested
97
+ @section = nil
98
+ end
99
+ alias_method :fields_for, :semantic_fields_for
100
+
101
+ def fields = @sections.flat_map { it[:fields] || [] }
102
+
103
+ # Content so far belongs in this spot: a marker says where, and `settle` fills the field once the
104
+ # whole block has run (an element open around an input can't be walked before then).
105
+ def flush_html(element = current_arbre_element)
106
+ return discard(element) unless @schema.content?
107
+
108
+ id = (@cuts.size + 1).to_s
109
+ field = FormField.content(@schema.next_content_name)
110
+ section[:fields] << field
111
+ @cuts[id] = [section, field]
112
+ text_node(FormContent.marker(id))
113
+ end
114
+
115
+ private
116
+
117
+ def nested_inputs(args, options, &block)
118
+ for_args = [options[:for], options[:for_options] || {}].flatten(1)
119
+ inner = options.except(:for, :for_options)
120
+ semantic_fields_for(*for_args) { |nested| nested.inputs(*args, inner, &block) }
121
+ end
122
+
123
+ def nested_sections(nested, path, &block)
124
+ recorder = FormRecorder.build(nested, @schema, prefix: path)
125
+ recorder.record { block.arity.zero? ? block.call : block.call(recorder) }
126
+ sections = recorder.sections.reject { it[:kind] == "inputs" && it[:fields].empty? }
127
+ sections.last[:fields] << FormField.hidden_id(nested, nested.object) if sections.any? && nested.object.try(:persisted?) && !@schema.only && nested_attributes?(nested)
128
+ sections
129
+ end
130
+
131
+ # Rails emits the id of a persisted record only for an association the model takes nested attributes for.
132
+ def nested_attributes?(nested) = nested.object_name.to_s.match?(/_attributes\]/)
133
+
134
+ # A block that can't run here (it may need a parent record or params) costs its own section, not the form.
135
+ def guard(label)
136
+ yield
137
+ rescue => e
138
+ Rails.logger.warn("[reactiveadmin] #{@schema.config.resource_name} #{label} section unavailable: #{e.class} at #{e.backtrace&.first}")
139
+ @schema.unsupported!(:section_failed)
140
+ section[:fields] << FormField.html("<p>This section is unavailable here.</p>", @schema.next_html_name, label: label.to_s)
141
+ Degradation.record(kind: :form_section_failed, where: "form/section[#{@sections.index(section)}]", reason: "Section raised #{e.class}; shown as unavailable")
142
+ end
143
+
144
+ # A write or one field's options only needs to know whether the markup holds inputs.
145
+ def discard(element)
146
+ @schema.scan_html(element.children.to_s)
147
+ element.children.clear
148
+ end
149
+
150
+ # Walks everything the block rendered once, then gives each marker's field the nodes before it. A field
151
+ # left empty (a wrapper around inputs) is dropped.
152
+ def settle
153
+ return if @cuts.empty?
154
+
155
+ raw = children.to_s
156
+ @schema.scan_html(raw)
157
+ parts = @schema.content.split(raw) || [[nil, raw]]
158
+ parts.each do |id, html|
159
+ section, field = @cuts.delete(id) || next
160
+ nodes = @schema.content.nodes(html)
161
+ nodes.empty? ? drop(section, field) : field[:layout] = nodes
162
+ end
163
+ @cuts.each_value { |section, field| drop(section, field) }
164
+ end
165
+
166
+ def drop(section, field) = section[:fields].delete_if { it.equal?(field) }
167
+
168
+ def section = @section ||= new_section(nil)
169
+
170
+ def new_section(title) = {title:, kind: "inputs", fields: []}.tap { @sections << it }
171
+
172
+ # Arbre content built by the block lands in a throwaway holder so it can be flushed in order.
173
+ def collect
174
+ insert_tag(Holder) do
175
+ yield
176
+ flush_html
177
+ nil
178
+ end
179
+ end
180
+
181
+ def default_columns(options) = AA.default_form_columns(builder, except: options[:except])
182
+
183
+ ruby2_keywords def method_missing(name, *args, &block)
184
+ return super unless builder.respond_to?(name)
185
+
186
+ builder.public_send(name, *args, &block)
187
+ nil
188
+ end
189
+
190
+ def respond_to_missing?(name, include_all = false) = builder.respond_to?(name, include_all) || super
191
+ end
192
+ end
@@ -0,0 +1,160 @@
1
+ module ReactiveAdmin
2
+ # The resource's `form do |f| ... end` block run the way ActiveAdmin::Views::Pages::Form
3
+ # does, but against a FormRecorder so the result is data instead of html.
4
+ class FormSchema
5
+ DEFAULT_BLOCK = proc do |f|
6
+ f.semantic_errors
7
+ f.inputs
8
+ f.actions
9
+ end
10
+
11
+ UNSUPPORTED_REASON = "This form can't be edited here yet — open it in the classic admin".freeze
12
+ # Inputs that html fields can't carry: the sanitizer drops them, so the API would post without them.
13
+ RAW_INPUTS = "input, select, textarea, button[type=submit]".freeze
14
+
15
+ attr_reader :config, :view, :assigns, :record, :choices, :parent_id, :only, :content
16
+
17
+ # The `data-confirm` a classic submit button asks about before posting.
18
+ def self.confirm_of(button_html)
19
+ data = button_html.respond_to?(:with_indifferent_access) ? button_html.with_indifferent_access : {}
20
+ (data.dig(:data, :confirm) || data["data-confirm"]).presence&.to_s
21
+ end
22
+
23
+ # `only` is a field key (`name`, or `assoc.name` inside has_many), or a list of them: just those are built.
24
+ # `controls_only` records what write guards and param normalization read (params, controls, disabled,
25
+ # summarized, nesting, unsupported kinds) and skips what only a client shows: options, labels, hints,
26
+ # values, sanitized html.
27
+ def initialize(controller, record, only: nil, controls_only: false)
28
+ @only = only
29
+ @controls_only = controls_only
30
+ @controller = controller
31
+ @view = Masking.install(controller.view_context, AA.config_of(controller))
32
+ @assigns = controller.view_assigns
33
+ @config = AA.config_of(controller)
34
+ @record = record
35
+ @parent_id = Paths.parent_id(@config, controller.params)
36
+ @choices = {}
37
+ @html_fields = 0
38
+ @content_fields = 0
39
+ @content = FormContent.new(controller, record)
40
+ note_prepare_failure(controller.reactiveadmin_prepare_failure)
41
+ end
42
+
43
+ def as_json(*) = {sections:}
44
+
45
+ def submit_confirm = sections && @submit_confirm
46
+
47
+ def record_submit_confirm(text) = (@submit_confirm ||= text)
48
+
49
+ def controls_only? = !!@controls_only
50
+
51
+ def record_id = (record.to_param.to_s if record.persisted?)
52
+
53
+ def next_html_name = "html_#{@html_fields += 1}"
54
+
55
+ def next_content_name = "content_#{@content_fields += 1}"
56
+
57
+ # Whether the content around inputs is carried: a client shows it, a write or one field's options doesn't need it.
58
+ def content? = !controls_only? && only.nil?
59
+
60
+ # False when posting this form through the API would leave out inputs the classic form sends.
61
+ def submittable?
62
+ return @submittable if defined?(@submittable)
63
+
64
+ sections
65
+ @submittable = unsupported.empty?
66
+ report_unsupported unless @submittable
67
+ @submittable
68
+ end
69
+
70
+ def unsupported_reason = (UNSUPPORTED_REASON unless submittable?)
71
+
72
+ def unsupported!(kind) = unsupported << kind
73
+
74
+ def unsupported_kinds = unsupported.to_a
75
+
76
+ # Associations the form edits in has_many sections: the lock has to cover their rows too.
77
+ def nested_associations = sections && nested.to_a
78
+
79
+ def nested!(assoc) = nested << assoc
80
+
81
+ # A partial leans on ivars its controller's `new` set up, so only that action can render it.
82
+ def partial? = AA.page_presenter(@config, :form)&.options&.dig(:partial).present?
83
+
84
+ # Raw html from a form block, before sanitizing removes the inputs it holds. Markup too deep to scan may hold
85
+ # inputs, so it makes the form unsupported rather than input-free.
86
+ def scan_html(raw)
87
+ fragment = HtmlPolicy.parse(raw.to_s)
88
+ unsupported! :raw_inputs if fragment.nil? || fragment.at_css(RAW_INPUTS)
89
+ end
90
+
91
+ def wants?(key) = only.nil? || Array(only).include?(key)
92
+
93
+ def wants_nested?(path) = only.nil? || (path.present? && Array(only).any? { it.start_with?("#{path}.") })
94
+
95
+ def sections
96
+ @sections ||= begin
97
+ presenter = AA.page_presenter(@config, :form)
98
+ options = presenter&.options || {}
99
+ options[:partial] ? partial_sections(options[:partial]) : record_sections(builder_for(options), presenter&.block)
100
+ rescue => e
101
+ Rails.logger.warn("[reactiveadmin] #{@config.resource_name} form block fell back to defaults: #{e.class} at #{e.backtrace&.first}")
102
+ Degradation.record(kind: :form_block_failed, where: "form", reason: "Form block raised #{e.class}; showing default fields")
103
+ unsupported! :block_failed
104
+ record_sections(builder_for({}), nil)
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def unsupported = (@unsupported ||= Set.new)
111
+
112
+ # The host's `new`/`edit` block raised before the form was built; the collector wasn't open yet.
113
+ def note_prepare_failure(error_class)
114
+ return unless error_class
115
+
116
+ unsupported! :prepare_failed
117
+ Degradation.record(kind: :prepare_block_failed, where: "form", reason: "The action's block raised #{error_class}; showing the record as built")
118
+ end
119
+
120
+ def nested = (@nested ||= Set.new)
121
+
122
+ def report_unsupported
123
+ resource = @config.resource_name.to_s
124
+ ReactiveAdmin.diagnostics.record(:form_unsupported, resource:, reason: unsupported.to_a.join(","))
125
+ Degradation.record(kind: partial? ? :form_partial : :form_unsupported, where: "form", reason: UNSUPPORTED_REASON)
126
+ end
127
+
128
+ def builder_for(options)
129
+ options = {url: "#", as: @config.param_key, builder: AA.form_builder}.merge(options.except(:partial, :title))
130
+ builder = nil
131
+ @view.semantic_form_for(@record, options) { builder = it }
132
+ builder
133
+ end
134
+
135
+ def record_sections(builder, block)
136
+ @choices.clear
137
+ @submit_confirm = nil
138
+ nested.clear
139
+ @html_fields = @content_fields = 0
140
+ NodeRegistry.with_scope do
141
+ FormRecorder.build(builder, self).record(&block || DEFAULT_BLOCK).sections.reject do |section|
142
+ section[:kind] == "inputs" && section[:fields].empty?
143
+ end
144
+ end
145
+ end
146
+
147
+ # The partial renders as a custom template does: its Formtastic inputs are recorded, the rest is html.
148
+ def partial_sections(partial)
149
+ @choices.clear
150
+ @html_fields = 0
151
+ state, html = TemplateForm.recording(self, @view) { @view.render(partial:) }
152
+ state.arrange(html).first
153
+ rescue => e
154
+ Rails.logger.warn("[reactiveadmin] #{@config.resource_name} form partial #{partial} unavailable: #{e.class} at #{e.backtrace&.first}")
155
+ Degradation.record(kind: :form_partial_failed, where: "form", reason: "Form partial raised #{e.class}; shown empty")
156
+ unsupported! :partial_failed
157
+ [{kind: "inputs", fields: [FormField.html("", next_html_name)]}]
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,97 @@
1
+ module ReactiveAdmin
2
+ # Requests the client makes to classic URLs with X-Reactiveadmin-Frame: the same action and
3
+ # authorization as the classic admin, answered without AA's layout. Writes are answered with
4
+ # data the client can act on: a redirect becomes JSON, anything else stays html.
5
+ module Frame
6
+ HEADER = "X-Reactiveadmin-Frame".freeze
7
+
8
+ # Scripts and handlers never reach the client; the client sanitizes again. A full AA page
9
+ # (a standard action rendered by its own Arbre view) is cut down to its main content.
10
+ def self.sanitize(html, host: nil) = HtmlPolicy.frame_body(content(html), host:)
11
+
12
+ def self.content(html)
13
+ return html unless html.match?(/<body\b/i)
14
+
15
+ doc = Nokogiri::HTML5(html)
16
+ (doc.at_css(AA.frame_content_selector) || doc.at_css("body")).inner_html
17
+ rescue ArgumentError
18
+ # Too deep for the document parser: frame_body parses the page as a fragment, degrading it under its own caps.
19
+ html
20
+ end
21
+
22
+ def redirect_to(options = {}, response_options = {})
23
+ return super unless frame_request?
24
+
25
+ location = url_for(options)
26
+ return render_api_error(:unauthorized, "Authentication required", 401, location:) if @frame_authenticating
27
+
28
+ notes = flash.to_hash.merge(response_options.fetch(:flash, {}).to_h.stringify_keys, response_options.slice(:notice, :alert, :error).stringify_keys)
29
+ flash.clear
30
+ render json: {redirect: Location.for(location, host: request.host), flash: notes.compact_blank.transform_values(&:to_s)},
31
+ content_type: "application/json"
32
+ end
33
+
34
+ def rescue_with_handler(exception, **)
35
+ return super unless frame_request? && exception.is_a?(ActionController::InvalidAuthenticityToken)
36
+
37
+ render_api_error(:invalid_csrf, "Invalid CSRF token", 422)
38
+ true
39
+ end
40
+
41
+ def render(*args, &)
42
+ frame_request? ? super(*Layouts.replace(args, false), &) : super
43
+ end
44
+
45
+ private
46
+
47
+ def frame_request?
48
+ return @frame_request if defined?(@frame_request)
49
+
50
+ @frame_request = request.headers[HEADER].to_s == "1" && !reactiveadmin_request?
51
+ end
52
+
53
+ # Classic actions answer by format, and a client's Accept must not turn them into record dumps.
54
+ # A format in the URL (a csv export) is the client's explicit choice and passes through.
55
+ def process_action(*)
56
+ request.format = :html if frame_request? && params[:format].blank?
57
+ super
58
+ sanitize_frame_response if frame_request?
59
+ end
60
+
61
+ def sanitize_frame_response
62
+ return unless response.media_type == "text/html" && response.body.present? && response.headers["Content-Disposition"].blank?
63
+
64
+ response.body = Frame.sanitize(response.body, host: request.host)
65
+ end
66
+
67
+ define_method(AA.layout_hook) { frame_request? ? false : super() }
68
+ private AA.layout_hook
69
+
70
+ def authenticate_active_admin_user
71
+ return super unless frame_request?
72
+
73
+ @frame_authenticating = true
74
+ super
75
+ @frame_authenticating = false
76
+ end
77
+
78
+ def dispatch_active_admin_access_denied(exception)
79
+ return super unless frame_request?
80
+
81
+ render_api_error(:forbidden, "Not authorized", 403)
82
+ end
83
+ end
84
+
85
+ # Swaps the layout a custom render names explicitly (`render "x", layout: "active_admin"`), which
86
+ # takes precedence over the controller's own layout.
87
+ module Layouts
88
+ module_function
89
+
90
+ def replace(args, layout)
91
+ options = args.last
92
+ return args unless options.is_a?(Hash) && options[:layout]
93
+
94
+ [*args[0...-1], options.merge(layout:)]
95
+ end
96
+ end
97
+ end