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,259 @@
1
+ module ReactiveAdmin
2
+ class IndexPayload
3
+ NUMERIC_TYPES = %i[integer float decimal].freeze
4
+ ALIGNMENTS = {"right" => /(\A|[-_])(right|number|numeric|amount|money|currency)\z/, "center" => /(\A|[-_])center\z/, "left" => /(\A|[-_])left\z/}.freeze
5
+ TONES = {"danger" => %w[danger error alert], "warning" => %w[warning warn], "success" => %w[success ok], "info" => %w[info]}.freeze
6
+
7
+ def initialize(controller)
8
+ @controller = controller
9
+ @config = AA.config_of(controller)
10
+ @view = Masking.install(controller.view_context, @config)
11
+ @authorizer = Authorizer.for(controller)
12
+ @permissions = Permissions.new(@config, @authorizer)
13
+ @views = IndexViews.new(@config, as: @controller.params[:as])
14
+ end
15
+
16
+ def as_json(*)
17
+ # Must run first: it applies filtering/scoping/sorting and memoizes the pre-scope chain.
18
+ records = AA.collection(@controller)
19
+ recorder = columns_recorder
20
+ if (sort = unsupported_sort(sorting_recorder(recorder)))
21
+ raise ApiErrors::BadRequest, "Unsupported sort: #{sort}"
22
+ end
23
+ keys = column_keys(recorder, recorder.columns)
24
+
25
+ Degradation.attach({
26
+ resource: Registry.route_key(@config),
27
+ columns: recorder.columns.zip(keys).map { |col, key| column_json(col, key, recorder) },
28
+ rows: records.each_with_index.map { |record, index| row(record, recorder, keys, records.try(:offset_value).to_i + index) },
29
+ meta: meta(records, recorder)
30
+ })
31
+ end
32
+
33
+ # [column json, attribute name or nil] for each column, without reading any rows.
34
+ def declared_columns
35
+ recorder = columns_recorder
36
+ recorder.columns.zip(column_keys(recorder, recorder.columns)).map do |col, key|
37
+ [column_json(col, key, recorder), (col.data.to_s if col.data.is_a?(Symbol))]
38
+ end
39
+ end
40
+
41
+ # Attributes shown as symbol columns: the allowlist for `_value`.
42
+ def symbol_columns = columns_recorder.columns.filter_map { it.data if it.data.is_a?(Symbol) }
43
+
44
+ # What an API export without a `csv` block shows, chosen as the table index chooses whatever view was requested:
45
+ # its block's columns, else secure defaults. Nil when strict defaults block it.
46
+ def export_columns
47
+ if (views = table_with_block)
48
+ @index_failed = false
49
+ recorder = record_columns(views)
50
+ columns = recorder.columns.filter_map { it.data.is_a?(Symbol) ? it.data : (it.title.to_s if recorder.id_column?(it)) }
51
+ return (@index_failed && ImplicitDefaults.strict?) ? ImplicitDefaults.blocked("csv") : columns
52
+ end
53
+ return ImplicitDefaults.blocked("csv") if ImplicitDefaults.strict?
54
+
55
+ key = @config.resource_class.primary_key
56
+ id = key.to_sym if key.is_a?(String) && !ImplicitDefaults.hidden?(@config, key)
57
+ [*id, *ImplicitDefaults.columns(@config, where: "csv")]
58
+ end
59
+
60
+ # The requested `order`'s sort key when the index doesn't offer it. Ordering by a column the
61
+ # page hides would leak how its values compare.
62
+ def unsupported_sort(recorder = sorting_recorder(columns_recorder))
63
+ QueryGuard.order_violation(@config, @controller.params[:order], recorder.columns.select { orderable?(it) }.map(&:sort_key))
64
+ end
65
+
66
+ private
67
+
68
+ def columns_recorder = @views.table? ? record_columns : new_recorder
69
+
70
+ def table_with_block = IndexViews.table_with_block(@config, as: @controller.params[:as])
71
+
72
+ # Other views show no columns, so a sort picked in the declared table (it stays in the URL) is checked against the table's.
73
+ def sorting_recorder(recorder)
74
+ table = IndexViews.new(@config, as: "table")
75
+ (@views.table? || !table.table?) ? recorder : record_columns(table)
76
+ end
77
+
78
+ def record_columns(views = @views)
79
+ block = views.presenter&.block
80
+ return new_recorder.tap { default_columns(it) } unless block
81
+
82
+ new_recorder.tap { it.instance_exec(it, &block) }
83
+ rescue => e
84
+ @index_failed = true
85
+ Rails.logger.warn("[reactiveadmin] #{@config.resource_name} index block fell back to defaults: #{e.class} at #{e.backtrace&.first}")
86
+ Degradation.record(kind: :index_block_failed, where: "index", reason: "Index block raised #{e.class}; showing default columns")
87
+ new_recorder.tap { default_columns(it) }
88
+ end
89
+
90
+ def new_recorder = IndexRecorder.build(@config.resource_class, @controller.view_assigns, @view)
91
+
92
+ def default_columns(recorder)
93
+ return ImplicitDefaults.blocked("index") if ImplicitDefaults.strict?
94
+
95
+ recorder.id_column if @config.resource_class.primary_key
96
+ @implicit_text = ImplicitDefaults.text_columns(@config)
97
+ ImplicitDefaults.columns(@config, where: "index").each { recorder.column(it) }
98
+ recorder.actions
99
+ end
100
+
101
+ def column_keys(recorder, columns)
102
+ seen = Hash.new(0)
103
+ columns.map do |col|
104
+ base = key_base(recorder, col)
105
+ ((seen[base] += 1) == 1) ? base : "#{base}_#{seen[base]}"
106
+ end
107
+ end
108
+
109
+ def key_base(recorder, col)
110
+ return "index" if recorder.index_column?(col)
111
+
112
+ (col.data.is_a?(Symbol) ? col.data : col.title).to_s.parameterize(separator: "_").presence || "column"
113
+ end
114
+
115
+ # pretty_title overwrites the column's symbol title with its label, which sort_key then reads.
116
+ def column_json(col, key, recorder)
117
+ sortable = orderable?(col)
118
+ class_name = recorder.column_class(col).to_s.presence
119
+ {key:, label: col.dup.pretty_title.to_s, sortable:, sort_key: (col.sort_key if sortable)}.merge(
120
+ {kind: ("index" if recorder.index_column?(col)), class_name:, align: align(col, class_name, sortable, recorder)}.compact
121
+ )
122
+ end
123
+
124
+ # A sort key the controller can't parse back, or that AA's order clause would rewrite, would
125
+ # order by something else (or nothing) when the client sent it.
126
+ def orderable?(col)
127
+ return false unless col.sortable?
128
+
129
+ key = col.sort_key
130
+ clause = AA.order_clause(@config, "#{key}_asc")
131
+ clause.valid? && clause.field == key
132
+ end
133
+
134
+ def align(col, class_name, sortable, recorder)
135
+ words = class_name.to_s.split
136
+ ALIGNMENTS.each { |side, pattern| return side if words.any? { pattern.match?(it) } }
137
+ "right" if recorder.index_column?(col) || numeric?(col.data.is_a?(Symbol) ? col.data.to_s : (col.sort_key if sortable))
138
+ end
139
+
140
+ # Ids and enums are labels, not quantities.
141
+ def numeric?(name)
142
+ klass = @config.resource_class
143
+ column = klass.columns_hash[name.to_s] if name
144
+ !!column && NUMERIC_TYPES.include?(column.type) && name != klass.primary_key &&
145
+ !name.end_with?("_id") && !klass.defined_enums.key?(name)
146
+ end
147
+
148
+ def row(record, recorder, keys, position)
149
+ links = ActionLinks.new(host: @controller.request.host, base_url: @controller.request.base_url, authorizer: @authorizer, subject: record)
150
+ row = {id: record.to_param.to_s, display_name: Values.display_name(record, @view), paths: Paths.record(@config, record),
151
+ permissions: @permissions.record(record)}
152
+ return row.merge(cells: {}, layout: item_layout(record, recorder, links, row[:display_name])) unless @views.table?
153
+
154
+ cells = recorder.columns.zip(keys).to_h { |col, key| [key, cell(record, col, recorder, links, position)] }
155
+ actions = row_actions(record, recorder, links)
156
+ row = row.merge(cells:, **row_style(record))
157
+ actions.any? ? row.merge(actions:) : row
158
+ end
159
+
160
+ def item_layout(record, recorder, links, fallback)
161
+ HtmlNodes.nodes(recorder.render_item(record, @views), host: @controller.request.host, links:, resource: @config.resource_name)
162
+ rescue => e
163
+ Rails.logger.warn("[reactiveadmin] #{@config.resource_name} #{@views.name} view failed: #{e.class} at #{e.backtrace&.first}")
164
+ Degradation.record(kind: :item_view_failed, where: "index/view[#{@views.name}]", reason: "Item view raised #{e.class}; showing the display name")
165
+ [{type: "text", text: fallback.to_s}]
166
+ end
167
+
168
+ def row_style(record)
169
+ block = @views.presenter&.[](:row_class)
170
+ class_name = Array(block.call(record)).flatten.compact_blank.join(" ").presence if block
171
+ words = class_name.to_s.downcase.split(/[^a-z0-9]+/)
172
+ tone = TONES.find { |_, names| (names & words).any? }&.first
173
+ {class_name:, tone:}.compact
174
+ rescue => e
175
+ Rails.logger.warn("[reactiveadmin] #{@config.resource_name} row_class failed: #{e.class} at #{e.backtrace&.first}")
176
+ Degradation.record(kind: :row_class_failed, where: "index/row_class", reason: "row_class raised #{e.class}; rows left unstyled")
177
+ {}
178
+ end
179
+
180
+ def row_actions(record, recorder, links)
181
+ return [] unless recorder.row_items?
182
+
183
+ ActionLinks.controls(recorder.render_row_items(record)).filter_map { links.action(it, placement: "row") }
184
+ rescue => e
185
+ Rails.logger.warn("[reactiveadmin] #{@config.resource_name} row items failed: #{e.class} at #{e.backtrace&.first}")
186
+ Degradation.record(kind: :row_actions_failed, where: "index/row_actions", reason: "Row actions raised #{e.class}; rows have no actions")
187
+ []
188
+ end
189
+
190
+ def cell(record, col, recorder, links, position)
191
+ if recorder.index_column?(col)
192
+ {type: "number", value: position + recorder.index_start(col)}
193
+ elsif recorder.id_column?(col) || (col.data.is_a?(Symbol) && col.data.to_s == @config.resource_class.primary_key)
194
+ {type: "ref", label: (col.data.is_a?(Symbol) ? record.public_send(col.data) : record.to_param).to_s, ref: Registry.ref(record)}.compact
195
+ elsif col.data.is_a?(Symbol)
196
+ value = AA.find_value(@view, record, col.data)
197
+ source = ValuePayload::Source.new(config: @config, record:, field: col.data.to_s, controller: @controller, via: "index")
198
+ if value.is_a?(String) && @implicit_text&.include?(col.data.to_s) then ImplicitDefaults.text_node(value, source, inline: false)
199
+ else Masking.node(@config, col.data, value) || Values.for(value, @view, source:)
200
+ end
201
+ else
202
+ HtmlNodes.convert(recorder.render_cell(record, col), host: @controller.request.host, links:, resource: @config.resource_name)
203
+ end
204
+ rescue => e
205
+ Rails.logger.warn("[reactiveadmin] #{@config.resource_name} cell #{col.title} failed: #{e.class} at #{e.backtrace&.first}")
206
+ Degradation.record(kind: :cell_failed, where: "index/column[#{key_base(recorder, col)}]", reason: "Cell raised #{e.class}")
207
+ {type: "error", label: "Couldn't display this value"}
208
+ end
209
+
210
+ def meta(records, recorder)
211
+ counted = ResourceMeta.pagination_total?(@config, @views.presenter)
212
+ {
213
+ page: records.try(:current_page) || 1,
214
+ per_page: records.try(:limit_value) || records.size,
215
+ total_count: (records.try(:total_count) || records.size if counted),
216
+ total_pages: (records.try(:total_pages) || 1 if counted),
217
+ has_next_page: next_page?(records, counted),
218
+ scopes: scopes,
219
+ current_scope: AA.current_scope(@controller)&.id,
220
+ order: @controller.params[:order]
221
+ }.merge(view_meta(recorder))
222
+ end
223
+
224
+ def view_meta(recorder)
225
+ table = @views.table?
226
+ {
227
+ as: @views.name,
228
+ view: @views.kind,
229
+ grid_columns: (@views.grid_columns if @views.kind == "grid"),
230
+ title: @views.title(@controller),
231
+ blank_slate: @views.blank_slate(@view),
232
+ default_row_actions: table && recorder.default_row_actions?,
233
+ row_actions_label: (recorder.row_actions_label if table),
234
+ row_actions_dropdown: (true if table && recorder.dropdown?),
235
+ paginator: (false unless @views.paginator?),
236
+ per_page_options: @views.per_page_options,
237
+ sidebars: Sidebars.new(@controller, "index", src: Paths.collection(@config, parent_id: Paths.parent_id(@config, @controller.params))).as_json.presence
238
+ }.compact
239
+ end
240
+
241
+ # Without totals the answer costs one indexed probe past the page, as in AA's countless paginator.
242
+ def next_page?(records, counted)
243
+ return records.current_page < records.total_pages if counted && records.respond_to?(:total_pages)
244
+ return false unless records.respond_to?(:offset)
245
+
246
+ records.offset(records.current_page * records.limit_value).exists?
247
+ end
248
+
249
+ def scopes
250
+ before_scope = AA.collection_before_scope(@controller)
251
+ counts = @views.presenter&.fetch(:scope_count, true) != false
252
+ visible = AA.scopes(@config).select { AA.scope_visible?(@view, it) }
253
+ IndexViews.scopes_in_render_order(visible).map do |scope|
254
+ count = AA.scope_count(@controller, scope, before_scope) if counts
255
+ {id: scope.id, name: AA.scope_name(@view, scope), count:}.merge(scope.group ? {group: scope.group.to_s} : {})
256
+ end
257
+ end
258
+ end
259
+ end
@@ -0,0 +1,115 @@
1
+ module ReactiveAdmin
2
+ # Stands in for AA's IndexAsTable while the `index do ... end` block runs, capturing columns
3
+ # and rendering cells the way TableFor#build_table_cell does. An Element (not a Context):
4
+ # Contexts forward unknown methods to their html string, which would swallow helpers like
5
+ # `truncate` that column blocks call.
6
+ class IndexRecorder < AA.arbre_element
7
+ Column = AA.table_column_class
8
+
9
+ attr_reader :columns, :row_options
10
+
11
+ def self.build(resource_class, assigns, helpers)
12
+ AA.arbre_context(assigns, helpers).insert_tag(self, resource_class)
13
+ end
14
+
15
+ def build(resource_class)
16
+ @resource_class = resource_class
17
+ @columns = []
18
+ @column_classes = {}.compare_by_identity
19
+ @index_starts = {}.compare_by_identity
20
+ @id_columns = {}.compare_by_identity
21
+ end
22
+
23
+ def column(*args, &block)
24
+ options = {i18n: @resource_class}.merge(args.extract_options!)
25
+ title = args[0]
26
+ explicit_class = options[:class]
27
+ Column.new(title, args[1] || title, @resource_class, options, &block).tap do |col|
28
+ @column_classes[col] = explicit_class
29
+ @columns << col
30
+ end
31
+ end
32
+
33
+ # The `class:` as written; Column keeps only the joined html class.
34
+ def column_class(col) = @column_classes[col]
35
+
36
+ def index_column?(col) = @index_starts.key?(col)
37
+
38
+ def index_start(col) = @index_starts[col]
39
+
40
+ def id_column?(col) = @id_columns.key?(col)
41
+
42
+ # A composite key has no single attribute to read or sort by, so its column shows the joined param.
43
+ def id_column(*args)
44
+ options = args.extract_options!
45
+ pk = @resource_class.primary_key
46
+ return column(args[0].presence || "Id", **options.reverse_merge(sortable: false), &:to_param).tap { @id_columns[it] = true } if pk.is_a?(Array)
47
+
48
+ title = args[0].presence || @resource_class.human_attribute_name(pk)
49
+ column(title, pk.to_sym, options.reverse_merge(sortable: pk))
50
+ end
51
+
52
+ def selectable_column(*) = nil
53
+
54
+ def index_column(start = 1)
55
+ col = column("#", sortable: false)
56
+ @index_starts[col] = start
57
+ end
58
+
59
+ # Only the block's custom items matter: View/Edit/Delete are derived from permissions and paths.
60
+ def actions(*args, &block)
61
+ @row_options = args.extract_options!
62
+ @row_items = block
63
+ nil
64
+ end
65
+
66
+ # Without an `actions` call the classic table has no View, Edit or Delete links either.
67
+ def default_row_actions? = !@row_options.nil? && @row_options[:defaults] != false
68
+
69
+ def dropdown? = !!@row_options&.[](:dropdown)
70
+
71
+ def row_actions_label
72
+ return unless @row_options
73
+
74
+ label = dropdown? ? @row_options[:dropdown_name] || AA.t("dropdown_actions.button_label", default: "Actions") : @row_options[:name]
75
+ label.to_s.presence
76
+ end
77
+
78
+ def row_items? = !@row_items.nil?
79
+
80
+ # Renders a row's custom `item`s the way IndexTableFor#actions does.
81
+ def render_row_items(record)
82
+ items = table_actions do
83
+ result = instance_exec(record, &@row_items)
84
+ text_node result unless AA.arbre_element?(result)
85
+ end
86
+ items.to_s.tap { items.parent&.remove_child(items) }
87
+ end
88
+
89
+ # A grid or block view's item, or a blog or custom view's whole output for one record.
90
+ def render_item(record, views)
91
+ item = if views.custom? || views.kind == "blog"
92
+ insert_tag(views.renderer, views.presenter, [record])
93
+ else
94
+ div { instance_exec(record, &views.presenter.block) }
95
+ end
96
+ html = item.to_s.tap { item.parent&.remove_child(item) }
97
+ strip_selection(html)
98
+ end
99
+
100
+ def strip_selection(html)
101
+ fragment = HtmlPolicy.parse(html) or return HtmlPolicy::TOO_COMPLEX
102
+
103
+ fragment.css(".resource_selection_toggle_panel, .collection_selection").each(&:remove)
104
+ fragment.to_html
105
+ end
106
+
107
+ def render_cell(record, col)
108
+ cell = td do
109
+ html = AA.format_attribute(helpers, record, col.data)
110
+ current_arbre_element << html unless current_arbre_element.children.include?(html)
111
+ end
112
+ cell.children.to_s.tap { cell.parent&.remove_child(cell) }
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,85 @@
1
+ module ReactiveAdmin
2
+ # The index presenter in effect (`?as=` picks one of several) and what it declares beyond its
3
+ # block: view kind, title, blank slate, paging and download links.
4
+ class IndexViews
5
+ DEFAULT_GRID_COLUMNS = 3
6
+
7
+ attr_reader :presenter
8
+
9
+ def self.scopes_in_render_order(scopes) = scopes.group_by(&:group).values.flatten(1)
10
+
11
+ def self.built_in = AA.index_renderers
12
+
13
+ def self.custom?(renderer) = renderer.is_a?(Class) && !built_in.value?(renderer)
14
+
15
+ # The table index with a block that an API export without a `csv` block takes its columns from:
16
+ # the requested view, then the default, then any other.
17
+ def self.table_with_block(config, as: nil)
18
+ names = [as.presence, nil, *AA.index_presenters(config).try(:keys)]
19
+ names.lazy.map { new(config, as: it) }.find { it.table? && it.presenter&.block }
20
+ end
21
+
22
+ def initialize(config, as: nil)
23
+ @config = config
24
+ @presenter = AA.page_presenter(config, :index, as.presence) || AA.page_presenter(config, :index)
25
+ end
26
+
27
+ def renderer
28
+ @renderer ||= AA.index_renderer(presenter&.[](:as))
29
+ end
30
+
31
+ def name = AA.index_name(renderer).to_s
32
+
33
+ # Custom IndexAs classes are rendered per record, so they read as a block view.
34
+ def kind = self.class.built_in.key(renderer) || "block"
35
+
36
+ def table? = kind == "table"
37
+
38
+ def custom? = self.class.custom?(renderer)
39
+
40
+ def grid_columns = (presenter&.[](:columns) || DEFAULT_GRID_COLUMNS).to_i
41
+
42
+ def list
43
+ presenters = AA.index_presenters(@config)
44
+ return unless presenters.is_a?(Hash) && presenters.size > 1
45
+
46
+ presenters.map do |as, item|
47
+ {as: as.to_s, label: AA.t("index_list.#{as}", default: as.to_s.titleize), default: item.equal?(presenters.default)}
48
+ end
49
+ end
50
+
51
+ def title(controller)
52
+ title = presenter&.[](:title)
53
+ title = controller.instance_exec(&title) if title.is_a?(Proc)
54
+ title.to_s.presence
55
+ end
56
+
57
+ # [label, href] of the declared link, [nil, nil] when the resource declared none.
58
+ def blank_slate(view)
59
+ return unless presenter&.has_key?(:blank_slate_link)
60
+
61
+ link = presenter[:blank_slate_link]
62
+ html = view.instance_exec(&link) if link.is_a?(Proc)
63
+ anchor = HtmlPolicy.parse(html.to_s)&.at_css("a[href]")
64
+ {label: anchor&.text&.strip.presence, href: anchor&.[]("href")}
65
+ end
66
+
67
+ def paginator? = presenter&.fetch(:paginator, true) != false
68
+
69
+ # AA offers a page-size selector only for a list; the page size itself stays the resource's.
70
+ def per_page_options
71
+ return unless presenter&.has_key?(:per_page)
72
+
73
+ list = presenter[:per_page]
74
+ list.is_a?(Array) ? list.map(&:to_i) : []
75
+ end
76
+
77
+ # The presenter's `download_links:`, then the resource's own, then the namespace's.
78
+ def download_links
79
+ return presenter[:download_links] if presenter&.has_key?(:download_links)
80
+
81
+ resource = @config.download_links if @config.respond_to?(:download_links)
82
+ resource.nil? ? @config.namespace.download_links : resource
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,125 @@
1
+ module ReactiveAdmin
2
+ # Reads an inline style as the utility classes LayoutClasses understands, one declaration at a
3
+ # time. Only values that land exactly on the vocabulary translate; anything else is returned in
4
+ # `unknown` and never reaches a typed node.
5
+ class InlineStyle
6
+ PX_PER_STEP = 4
7
+ MAX_REPORTED = 200
8
+ SIDES = {"top" => "t", "right" => "r", "bottom" => "b", "left" => "l"}.freeze
9
+ SHARES = {25.0 => "1/4", 33.333 => "1/3", 50.0 => "1/2", 66.667 => "2/3", 75.0 => "3/4", 100.0 => "full"}.freeze
10
+ ALIGN_ITEMS = {"flex-start" => "start", "start" => "start", "center" => "center", "flex-end" => "end", "end" => "end",
11
+ "stretch" => "stretch", "baseline" => "baseline"}.freeze
12
+ JUSTIFY = {"flex-start" => "start", "start" => "start", "center" => "center", "flex-end" => "end", "end" => "end",
13
+ "space-between" => "between"}.freeze
14
+ BOLD = %w[bold bolder 600 700 800 900].freeze
15
+ COLORS = {"#999" => "muted", "#999999" => "muted", "gray" => "muted", "grey" => "muted", "red" => "danger", "green" => "success"}.freeze
16
+
17
+ # What to do with a declaration that has no translation, for the styles report.
18
+ ADVICE = {
19
+ "padding" => "no typed equivalent; wrap in a panel, or use margin on the neighbours",
20
+ "position" => "classic-only layout; the client sizes and scrolls its own panes",
21
+ "overflow" => "classic-only; the client scrolls its own panes",
22
+ "max-height" => "classic-only; drop it",
23
+ "max-width" => "use `max-w-sm|md|lg|xl|2xl`",
24
+ "width" => "use `w-1/4|1/3|1/2|2/3|3/4|full`, or `max-w-*`",
25
+ "color" => "use `text-muted|success|danger|warning`",
26
+ "background" => "use a `flash` or a status_tag for a callout; there is no typed fill",
27
+ "border" => "use a panel or a `flash`; there is no typed border",
28
+ "cursor" => "drop it; the client styles its own controls",
29
+ "font-family" => "drop it, or show the text as a `code` node",
30
+ "font-size" => "drop it; there is no typed text size",
31
+ "display" => "drop it; boxes are blocks (use `flex` for rows)",
32
+ "white-space" => "only `nowrap` has a typed form (`whitespace-nowrap`); show preformatted text as a `code` node",
33
+ "flex-shrink" => "drop it; the client handles shrink",
34
+ "top" => "classic-only layout",
35
+ "bottom" => "classic-only layout",
36
+ "margin" => "use margins on the 4px scale (`mt-2`), no negatives or auto",
37
+ "margin-top" => "use margins on the 4px scale (`mt-2`), no negatives or auto",
38
+ "margin-bottom" => "use margins on the 4px scale (`mb-2`), no negatives or auto",
39
+ "margin-left" => "use margins on the 4px scale (`ml-2`), no negatives or auto",
40
+ "margin-right" => "use margins on the 4px scale (`mr-2`), no negatives or auto"
41
+ }.freeze
42
+
43
+ Result = Struct.new(:tokens, :unknown)
44
+
45
+ def self.parse(style) = new(style).parse
46
+
47
+ # A style a host builds from data can carry some (a url, a width), so reports keep it short and drop urls.
48
+ def self.redact(style) = style.to_s.squish.gsub(/url\([^)]*\)/i, "url(…)").truncate(MAX_REPORTED)
49
+
50
+ def self.advice(declaration) = ADVICE.fetch(declaration.to_s.split(":").first.to_s.strip.downcase, "no typed equivalent")
51
+
52
+ def initialize(style)
53
+ @declarations = style.to_s.split(";").filter_map do |declaration|
54
+ property, _, value = declaration.partition(":")
55
+ [property.strip.downcase, value.sub(/!important\z/i, "").strip.downcase] if property.strip.present? && value.present?
56
+ end
57
+ end
58
+
59
+ def parse
60
+ tokens = []
61
+ unknown = []
62
+ @declarations.each do |property, value|
63
+ translated = translate(property, value)
64
+ translated ? tokens.concat(translated) : unknown << "#{property}: #{value}"
65
+ end
66
+ Result.new(tokens, unknown)
67
+ end
68
+
69
+ private
70
+
71
+ # nil means no translation; an empty list means the declaration says nothing the client needs.
72
+ def translate(property, value)
73
+ case property
74
+ when /\Amargin-(top|right|bottom|left)\z/ then spacing("m#{SIDES[$1]}", value)
75
+ when "margin" then margin(value)
76
+ when "text-align" then %W[text-#{value}] if LayoutClasses::TEXT_ALIGNS.include?(value)
77
+ when "display" then {"flex" => %w[flex], "inline-flex" => %w[inline-flex], "grid" => %w[grid]}[value]
78
+ when "flex-direction" then {"row" => %w[flex-row], "column" => %w[flex-col]}[value]
79
+ when "flex-wrap" then {"wrap" => %w[flex-wrap], "nowrap" => []}[value]
80
+ when "white-space" then {"nowrap" => %w[whitespace-nowrap], "normal" => []}[value]
81
+ when "gap" then steps(value)&.then { ["gap-#{it}"] }
82
+ when "align-items" then ALIGN_ITEMS[value]&.then { ["items-#{it}"] }
83
+ when "justify-content" then JUSTIFY[value]&.then { ["justify-#{it}"] }
84
+ when "width" then width(value)
85
+ when "font-weight" then BOLD.include?(value) ? %w[font-bold] : ([] if %w[normal 400].include?(value))
86
+ when "font-style" then {"italic" => %w[italic], "normal" => []}[value]
87
+ when "color" then COLORS[value]&.then { ["text-#{it}"] }
88
+ when "flex" then ["grow"] if ["1", "1 1 0", "1 1 0%", "1 1 0px"].include?(value)
89
+ when "flex-grow" then %w[grow] if value == "1"
90
+ end
91
+ end
92
+
93
+ # Whole steps of the 4px scale, from px or rem; 0 is a valid step.
94
+ def steps(value)
95
+ amount, unit = value.match(/\A(\d+(?:\.\d+)?)(px|rem)?\z/)&.captures
96
+ return unless amount
97
+
98
+ pixels = (unit == "rem") ? amount.to_f * 16 : amount.to_f
99
+ return 0 if pixels.zero?
100
+ return unless unit && pixels % PX_PER_STEP == 0
101
+
102
+ (pixels / PX_PER_STEP).to_i.then { it if it <= LayoutClasses::MAX_SPACING }
103
+ end
104
+
105
+ def spacing(prefix, value) = steps(value)&.then { ["#{prefix}-#{it}"] }
106
+
107
+ # CSS order: top, right, bottom, left, each repeating from the opposite side when omitted.
108
+ def margin(value)
109
+ parts = value.split
110
+ return unless (1..4).cover?(parts.size)
111
+
112
+ top, right, bottom, left = parts.values_at(0, 1, 2, 3)
113
+ sides = {"t" => top, "r" => right || top, "b" => bottom || top, "l" => left || right || top}
114
+ tokens = sides.map { |side, part| spacing("m#{side}", part) }
115
+ tokens.flatten if tokens.none?(&:nil?)
116
+ end
117
+
118
+ def width(value)
119
+ return unless value.end_with?("%")
120
+
121
+ percent = value.to_f
122
+ SHARES.find { |amount, _| (amount - percent).abs < 0.1 }&.last&.then { ["w-#{it}"] }
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,34 @@
1
+ module ReactiveAdmin
2
+ # Display names for ids the user opened recently (command palette), so recents never fetch whole
3
+ # records. Each record goes through the same finder and READ authorization as `show`; ids that
4
+ # are missing or refused are left out, and nothing but resource, id and name is returned.
5
+ class LabelsPayload
6
+ MAX_IDS = 20
7
+
8
+ def initialize(controller)
9
+ @controller = controller
10
+ @config = AA.config_of(controller)
11
+ end
12
+
13
+ def as_json(*)
14
+ ids.filter_map { label(it) }.tap do
15
+ LookupAudit.record(@controller, model: @config.resource_class, kind: :labels, result_count: it.size)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def ids
22
+ raw = @controller.params.require(:ids)
23
+ Array(raw).flat_map { it.to_s.split(",") }.map(&:strip).compact_blank.uniq.first(MAX_IDS)
24
+ end
25
+
26
+ def label(id)
27
+ @controller.params[:id] = id
28
+ record = AA.find_authorized_resource(@controller)
29
+ {resource: Registry.route_key(@config), id: record.to_param.to_s, display_name: Values.display_label(record, @controller.view_context)}
30
+ rescue ActiveRecord::RecordNotFound, AA::AccessDenied
31
+ nil
32
+ end
33
+ end
34
+ end