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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +88 -0
- data/LICENSE +21 -0
- data/README.md +81 -0
- data/app/assets/reactive_admin/bridge.js +89 -0
- data/app/controllers/reactive_admin/api_controller.rb +30 -0
- data/app/controllers/reactive_admin/client_errors_controller.rb +38 -0
- data/app/controllers/reactive_admin/manifest_controller.rb +10 -0
- data/app/controllers/reactive_admin/mcp_controller.rb +48 -0
- data/app/controllers/reactive_admin/openapi_controller.rb +13 -0
- data/app/controllers/reactive_admin/session_controller.rb +11 -0
- data/app/views/layouts/reactive_admin/embed.html.erb +30 -0
- data/lib/generators/reactiveadmin/install/install_generator.rb +341 -0
- data/lib/generators/reactiveadmin/install/templates/menu.rb.tt +25 -0
- data/lib/generators/reactiveadmin/install/templates/reactive_admin.rb.tt +53 -0
- data/lib/generators/reactiveadmin/install/templates/reactive_admin_annotated.rb.tt +110 -0
- data/lib/reactive_admin/aa/v3/controller.rb +76 -0
- data/lib/reactive_admin/aa/v3/forms.rb +29 -0
- data/lib/reactive_admin/aa/v3/inputs/filters/typeahead_input.rb +51 -0
- data/lib/reactive_admin/aa/v3/inputs/typeahead_input.rb +8 -0
- data/lib/reactive_admin/aa/v3/install.rb +36 -0
- data/lib/reactive_admin/aa/v3/menu.rb +67 -0
- data/lib/reactive_admin/aa/v3/presenters.rb +102 -0
- data/lib/reactive_admin/aa/v3/registry.rb +78 -0
- data/lib/reactive_admin/aa/v3/views.rb +63 -0
- data/lib/reactive_admin/aa/v3.rb +23 -0
- data/lib/reactive_admin/aa/v4.rb +59 -0
- data/lib/reactive_admin/aa.rb +46 -0
- data/lib/reactive_admin/action_items.rb +39 -0
- data/lib/reactive_admin/action_links.rb +197 -0
- data/lib/reactive_admin/action_safety.rb +19 -0
- data/lib/reactive_admin/actions.rb +95 -0
- data/lib/reactive_admin/api_errors.rb +116 -0
- data/lib/reactive_admin/api_fallback.rb +15 -0
- data/lib/reactive_admin/api_guards.rb +445 -0
- data/lib/reactive_admin/audit.rb +72 -0
- data/lib/reactive_admin/auth/guard.rb +68 -0
- data/lib/reactive_admin/auth/token_requests.rb +15 -0
- data/lib/reactive_admin/auth.rb +135 -0
- data/lib/reactive_admin/authorizer.rb +47 -0
- data/lib/reactive_admin/background_requests.rb +33 -0
- data/lib/reactive_admin/batch_actions.rb +57 -0
- data/lib/reactive_admin/choices.rb +289 -0
- data/lib/reactive_admin/client_error.rb +70 -0
- data/lib/reactive_admin/composite_keys.rb +12 -0
- data/lib/reactive_admin/configuration.rb +221 -0
- data/lib/reactive_admin/controller_extension.rb +229 -0
- data/lib/reactive_admin/csv_injection.rb +26 -0
- data/lib/reactive_admin/date_bound.rb +31 -0
- data/lib/reactive_admin/degradation.rb +72 -0
- data/lib/reactive_admin/diagnostics.rb +16 -0
- data/lib/reactive_admin/doctor.rb +466 -0
- data/lib/reactive_admin/dry_run.rb +147 -0
- data/lib/reactive_admin/dry_run_flag.rb +18 -0
- data/lib/reactive_admin/embed.rb +73 -0
- data/lib/reactive_admin/engine.rb +47 -0
- data/lib/reactive_admin/error_keys.rb +23 -0
- data/lib/reactive_admin/exposure.rb +86 -0
- data/lib/reactive_admin/filters.rb +165 -0
- data/lib/reactive_admin/fixtures/scrubber.rb +1075 -0
- data/lib/reactive_admin/fixtures.rb +161 -0
- data/lib/reactive_admin/form_content.rb +78 -0
- data/lib/reactive_admin/form_field.rb +279 -0
- data/lib/reactive_admin/form_has_many.rb +116 -0
- data/lib/reactive_admin/form_lock.rb +71 -0
- data/lib/reactive_admin/form_payload.rb +57 -0
- data/lib/reactive_admin/form_recorder.rb +192 -0
- data/lib/reactive_admin/form_schema.rb +160 -0
- data/lib/reactive_admin/frame.rb +97 -0
- data/lib/reactive_admin/html_nodes.rb +390 -0
- data/lib/reactive_admin/html_policy.rb +338 -0
- data/lib/reactive_admin/html_rich.rb +186 -0
- data/lib/reactive_admin/implicit_defaults.rb +102 -0
- data/lib/reactive_admin/index_options.rb +41 -0
- data/lib/reactive_admin/index_payload.rb +259 -0
- data/lib/reactive_admin/index_recorder.rb +115 -0
- data/lib/reactive_admin/index_views.rb +85 -0
- data/lib/reactive_admin/inline_style.rb +125 -0
- data/lib/reactive_admin/labels_payload.rb +34 -0
- data/lib/reactive_admin/ladder.rb +268 -0
- data/lib/reactive_admin/layout_classes.rb +167 -0
- data/lib/reactive_admin/legacy_inventory.rb +547 -0
- data/lib/reactive_admin/location.rb +22 -0
- data/lib/reactive_admin/lookup_audit.rb +30 -0
- data/lib/reactive_admin/lookup_payload.rb +41 -0
- data/lib/reactive_admin/manifest.rb +36 -0
- data/lib/reactive_admin/masked_json.rb +62 -0
- data/lib/reactive_admin/masking.rb +100 -0
- data/lib/reactive_admin/mcp/access.rb +111 -0
- data/lib/reactive_admin/mcp/api.rb +123 -0
- data/lib/reactive_admin/mcp/count_tool.rb +22 -0
- data/lib/reactive_admin/mcp/layout.rb +175 -0
- data/lib/reactive_admin/mcp/output.rb +46 -0
- data/lib/reactive_admin/mcp/prompts.rb +111 -0
- data/lib/reactive_admin/mcp/server.rb +134 -0
- data/lib/reactive_admin/mcp/shapes.rb +164 -0
- data/lib/reactive_admin/mcp/tools.rb +386 -0
- data/lib/reactive_admin/mcp.rb +53 -0
- data/lib/reactive_admin/media_nodes.rb +34 -0
- data/lib/reactive_admin/menu.rb +90 -0
- data/lib/reactive_admin/menu_overlay.rb +291 -0
- data/lib/reactive_admin/node_registry.rb +47 -0
- data/lib/reactive_admin/open_api/builder.rb +156 -0
- data/lib/reactive_admin/open_api/context.rb +46 -0
- data/lib/reactive_admin/open_api/core.rb +51 -0
- data/lib/reactive_admin/open_api/operation_ids.rb +18 -0
- data/lib/reactive_admin/open_api/page_doc.rb +75 -0
- data/lib/reactive_admin/open_api/parts.rb +86 -0
- data/lib/reactive_admin/open_api/resource_doc.rb +423 -0
- data/lib/reactive_admin/open_api/security.rb +55 -0
- data/lib/reactive_admin/open_api/write_schema.rb +104 -0
- data/lib/reactive_admin/open_api.rb +41 -0
- data/lib/reactive_admin/option_source.rb +36 -0
- data/lib/reactive_admin/option_sources.rb +14 -0
- data/lib/reactive_admin/options_payload.rb +61 -0
- data/lib/reactive_admin/page_controller_extension.rb +112 -0
- data/lib/reactive_admin/page_forms.rb +181 -0
- data/lib/reactive_admin/page_payload.rb +120 -0
- data/lib/reactive_admin/param_normalizer.rb +89 -0
- data/lib/reactive_admin/paths.rb +63 -0
- data/lib/reactive_admin/permissions.rb +31 -0
- data/lib/reactive_admin/provenance.rb +57 -0
- data/lib/reactive_admin/query_guard.rb +108 -0
- data/lib/reactive_admin/ransack_guard.rb +107 -0
- data/lib/reactive_admin/rate_limit.rb +59 -0
- data/lib/reactive_admin/reactive_dsl.rb +99 -0
- data/lib/reactive_admin/readiness.rb +189 -0
- data/lib/reactive_admin/registry.rb +69 -0
- data/lib/reactive_admin/resource_meta.rb +120 -0
- data/lib/reactive_admin/resource_summary.rb +41 -0
- data/lib/reactive_admin/routes.rb +60 -0
- data/lib/reactive_admin/row_errors.rb +29 -0
- data/lib/reactive_admin/rspec.rb +116 -0
- data/lib/reactive_admin/scrubbed_error.rb +22 -0
- data/lib/reactive_admin/search.rb +79 -0
- data/lib/reactive_admin/server_timing.rb +31 -0
- data/lib/reactive_admin/show_payload.rb +88 -0
- data/lib/reactive_admin/sidebars.rb +34 -0
- data/lib/reactive_admin/site.rb +55 -0
- data/lib/reactive_admin/style_report.rb +129 -0
- data/lib/reactive_admin/style_sources.rb +83 -0
- data/lib/reactive_admin/template_actions.rb +126 -0
- data/lib/reactive_admin/template_content.rb +49 -0
- data/lib/reactive_admin/template_form.rb +442 -0
- data/lib/reactive_admin/test_support.rb +31 -0
- data/lib/reactive_admin/typeahead.rb +33 -0
- data/lib/reactive_admin/ui/assets.rb +91 -0
- data/lib/reactive_admin/ui/client_build.rb +47 -0
- data/lib/reactive_admin/ui/helper.rb +24 -0
- data/lib/reactive_admin/ui/packaged_client.rb +67 -0
- data/lib/reactive_admin/ui/shell.rb +81 -0
- data/lib/reactive_admin/ui/switch.rb +38 -0
- data/lib/reactive_admin/ui.rb +43 -0
- data/lib/reactive_admin/value_payload.rb +147 -0
- data/lib/reactive_admin/value_token.rb +36 -0
- data/lib/reactive_admin/values.rb +43 -0
- data/lib/reactive_admin/version.rb +3 -0
- data/lib/reactive_admin/write_audit.rb +60 -0
- data/lib/reactive_admin/write_guards.rb +142 -0
- data/lib/reactive_admin.rb +153 -0
- data/lib/reactiveadmin.rb +1 -0
- data/lib/tasks/reactiveadmin.rake +36 -0
- data/lib/tasks/reactiveadmin_client.rake +11 -0
- data/lib/tasks/reactiveadmin_legacy.rake +7 -0
- data/lib/tasks/reactiveadmin_styles.rake +6 -0
- data/openapi/core.yaml +1643 -0
- data/vendor/client/assets/ActionPage-XUYorI8E.js +1 -0
- data/vendor/client/assets/ActionView-BsZFcsgp.js +1 -0
- data/vendor/client/assets/CommandPalette-C59HT1ip.js +1 -0
- data/vendor/client/assets/DatePicker-DuaK0iMg.js +1 -0
- data/vendor/client/assets/DateRangeCalendar-B2BDsG-X.js +1 -0
- data/vendor/client/assets/DegradedNotice-DiH5n2DO.js +1 -0
- data/vendor/client/assets/FormDialog-pmYl8Eov.js +1 -0
- data/vendor/client/assets/FormSkeleton-DuSBZO3C.js +1 -0
- data/vendor/client/assets/FrameView-2X8cFvUt.css +1 -0
- data/vendor/client/assets/FrameView-zmUeDO8d.js +1 -0
- data/vendor/client/assets/GroupOverview-DI5T3k5K.js +1 -0
- data/vendor/client/assets/InlineForm-CMEIMgeh.js +1 -0
- data/vendor/client/assets/JsonDiff-o7JWr7CH.js +3 -0
- data/vendor/client/assets/JsonEditor-D4VXdlEx.js +1 -0
- data/vendor/client/assets/JsonView-BrJLDmSN.js +1 -0
- data/vendor/client/assets/MobileMenu-GAeFOeEb.js +1 -0
- data/vendor/client/assets/Node-SQl61V4K.js +2 -0
- data/vendor/client/assets/PageView-C-laNEPZ.js +1 -0
- data/vendor/client/assets/ResourceForm-BPjsoCY6.js +1 -0
- data/vendor/client/assets/ResourceIndex-COezAw5t.js +3 -0
- data/vendor/client/assets/ResourceOrPage-CRZGCY84.js +1 -0
- data/vendor/client/assets/ResourceShow-DweRXn18.js +1 -0
- data/vendor/client/assets/Segmented-BE9TCbph.js +1 -0
- data/vendor/client/assets/Sidebars-CsB9rFqX.js +1 -0
- data/vendor/client/assets/breadcrumb-DSA3Tlyd.js +1 -0
- data/vendor/client/assets/button-CvZ4_DFC.js +1 -0
- data/vendor/client/assets/calendar-BoyMpJIf.js +1 -0
- data/vendor/client/assets/check-C5A3J16N.js +1 -0
- data/vendor/client/assets/command-CMuk0oAt.js +1 -0
- data/vendor/client/assets/dist-CqEI82S_.js +1 -0
- data/vendor/client/assets/dist-IW4yX942.js +1 -0
- data/vendor/client/assets/editor-yqRLzgsJ.js +2 -0
- data/vendor/client/assets/external-link-C2aR4slL.js +1 -0
- data/vendor/client/assets/frames-C9gPCc3N.js +1 -0
- data/vendor/client/assets/humanize-DtU_Kpr4.js +1 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-cyrillic-ext-wght-normal-d45eAU9y.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-cyrillic-wght-normal-BAAhND-U.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-greek-wght-normal-CmyJS8uq.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-latin-ext-wght-normal-CIII54If.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-latin-wght-normal-IvpUvPa2.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-vietnamese-wght-normal-Dg1JeJN0.woff2 +0 -0
- data/vendor/client/assets/index-B51aUaAY.js +43 -0
- data/vendor/client/assets/index-Cs9rCoWM.css +2 -0
- data/vendor/client/assets/input-DByFmQPa.js +1 -0
- data/vendor/client/assets/json-De3PN-R6.css +1 -0
- data/vendor/client/assets/json-oTp7Q-d-.js +5 -0
- data/vendor/client/assets/media-D4a_En8R.js +1 -0
- data/vendor/client/assets/plus-CfJ9vo8X.js +1 -0
- data/vendor/client/assets/popover-CDtI9R8Q.js +1 -0
- data/vendor/client/assets/purify-G7QKJyJq.js +1 -0
- data/vendor/client/assets/query-CCsZN2fa.js +1 -0
- data/vendor/client/assets/recent-BrzLwL0D.js +1 -0
- data/vendor/client/assets/rich-BQc_q5T7.js +1 -0
- data/vendor/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- data/vendor/client/assets/select-C43qDA3k.js +1 -0
- data/vendor/client/assets/sheet-BPl2n-4n.js +1 -0
- data/vendor/client/assets/textarea-BnOphXry.js +1 -0
- data/vendor/client/assets/uiMode-z0ZbRV49.js +1 -0
- data/vendor/client/assets/uiToggle-DwnRQTH1.js +1 -0
- data/vendor/client/assets/useRecordForm-X_DExEP2.js +1 -0
- data/vendor/client/assets/useRecordVisit-BRtfMi-q.js +1 -0
- data/vendor/client/assets/utils-DwBGbDkG.js +1 -0
- data/vendor/client/assets/vendor-calendar-nTZiq9oY.js +1 -0
- data/vendor/client/assets/vendor-datefns-BoC6MCnl.js +1 -0
- data/vendor/client/assets/vendor-forms-D0h8hG_S.js +55 -0
- data/vendor/client/assets/vendor-purify-BXoPR7WT.js +3 -0
- data/vendor/client/assets/vendor-query-DZ6APRNI.js +1 -0
- data/vendor/client/assets/vendor-react-C1iE8mnY.js +9 -0
- data/vendor/client/assets/vendor-router-CI6Vh4k_.js +1 -0
- data/vendor/client/assets/vendor-table-DuQ835by.js +4 -0
- data/vendor/client/index.html +24 -0
- data/vendor/client/manifest.json +867 -0
- data/vendor/client/reactiveadmin-client.json +4 -0
- metadata +350 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module ReactiveAdmin
|
|
4
|
+
# Behind `rake reactiveadmin:readiness`: per resource and page, how it renders in the new admin (the doctor's
|
|
5
|
+
# view of it) and a checklist of signals for leaving classic. It knows what the doctor can measure and what
|
|
6
|
+
# the host records through `config.event_counts`; who still uses classic, and whether anyone is satisfied, it
|
|
7
|
+
# can't see, so a check with no data reads `unknown` rather than passing.
|
|
8
|
+
module Readiness
|
|
9
|
+
SECTIONS = Doctor::SECTIONS
|
|
10
|
+
FALLBACK_MODES = %w[frame iframe].freeze
|
|
11
|
+
EVENTS = {degraded: Degradation::EVENT, client_errors: ClientError::EVENT}.freeze
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# `report` is a Doctor report. Rows are resources, then pages.
|
|
16
|
+
def build(report, counts: ReactiveAdmin.config.event_counts)
|
|
17
|
+
rows = [*report[:resources].map { resource_row(it, counts, report[:sampled]) }, *report[:pages].map { page_row(it, counts, report[:sampled]) }]
|
|
18
|
+
{sampled: report[:sampled], counted: !counts.nil?, summary: summary(rows), rows:}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def resource_row(row, counts, sampled)
|
|
22
|
+
failures = [*row[:failures], *("form is not submittable" if row.dig(:form, :submittable) == false), *render_errors(row)]
|
|
23
|
+
fallbacks = [*row[:actions].select { FALLBACK_MODES.include?(it[:mode]) }.map { it[:name] }, *framed(row)]
|
|
24
|
+
html = SECTIONS.filter_map { |section| "#{section} #{row.dig(section, :html)}" if row.dig(section, :html).to_i.positive? }
|
|
25
|
+
mode = mode_of(fallback: failures.any? || fallbacks.any?, degraded: html.any? || row[:filters][:skipped].any?)
|
|
26
|
+
unsampled = SECTIONS.reject { inspected?(it, row[it]) }
|
|
27
|
+
rendered = unsampled.size < SECTIONS.size
|
|
28
|
+
checks = [
|
|
29
|
+
check("typed views", fallbacks.empty?, "#{fallbacks.join(", ")} through frame or iframe"),
|
|
30
|
+
sample_check(sampled, rendered || failures.any?, failures, unsampled),
|
|
31
|
+
html_check(sampled, rendered, html, unsampled),
|
|
32
|
+
check("all filters built", row[:filters][:skipped].empty?, "skipped #{row[:filters][:skipped].join(", ")}"),
|
|
33
|
+
*event_checks(counts, row[:key], Registry.find(row[:key]))
|
|
34
|
+
]
|
|
35
|
+
{key: row[:key], kind: "resource", mode:, checks:, ready: ready?(checks)}
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def page_row(row, counts, sampled)
|
|
39
|
+
html = row.dig(:nodes, :html).to_i
|
|
40
|
+
views = [("page through #{row[:mode]}" if FALLBACK_MODES.include?(row[:mode])),
|
|
41
|
+
*row[:actions].to_a.select { FALLBACK_MODES.include?(it[:mode]) }.map { "#{it[:name]} through #{it[:mode]}" },
|
|
42
|
+
*framed(nodes: row[:nodes]).map { "#{it} through frame or iframe" }].compact
|
|
43
|
+
failures = [row[:failure], *render_errors(nodes: row[:nodes])].compact
|
|
44
|
+
mode = mode_of(fallback: failures.any? || views.any?, degraded: html.positive?)
|
|
45
|
+
rendered = inspected?(:page, row[:nodes])
|
|
46
|
+
checks = [
|
|
47
|
+
check("typed views", views.empty?, views.join(", ")),
|
|
48
|
+
sample_check(sampled, rendered || failures.any?, failures),
|
|
49
|
+
html_check(sampled, rendered, ["html nodes #{html}"].reject { html.zero? }),
|
|
50
|
+
*event_checks(counts, row[:key], Registry.pages.find { it.underscored_resource_name == row[:key] })
|
|
51
|
+
]
|
|
52
|
+
{key: row[:key], kind: "page", mode:, checks:, ready: ready?(checks)}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# An empty tally (an index with no rows, a layout with no nodes) rendered, but showed nothing to judge.
|
|
56
|
+
def inspected?(section, tally)
|
|
57
|
+
return false unless tally.is_a?(Hash) && !tally.key?(:skipped)
|
|
58
|
+
|
|
59
|
+
(tally[:typed].to_i + tally[:html].to_i + tally[:errors].to_i + tally[:frames].to_i).positive? || (section == :index && tally[:rows].to_i.positive?)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Sections whose sample embedded a classic page (content that runs its own JavaScript).
|
|
63
|
+
def framed(row)
|
|
64
|
+
row.filter_map { |section, tally| section.to_s if tally.is_a?(Hash) && tally[:frames].to_i.positive? }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Values that failed inside a sample that answered 200, so no request failure names them.
|
|
68
|
+
def render_errors(row)
|
|
69
|
+
row.filter_map { |section, tally| "#{section} #{tally[:errors]} failed to render" if tally.is_a?(Hash) && tally[:errors].to_i.positive? }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def mode_of(fallback:, degraded:)
|
|
73
|
+
return "fallback" if fallback
|
|
74
|
+
|
|
75
|
+
degraded ? "degraded" : "native"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def summary(rows)
|
|
79
|
+
modes = rows.map { it[:mode] }.tally
|
|
80
|
+
{total: rows.size, native: modes.fetch("native", 0), degraded: modes.fetch("degraded", 0), fallback: modes.fetch("fallback", 0), ready: rows.count { it[:ready] }}
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def ready?(checks) = checks.none? { it[:status] == :fail }
|
|
84
|
+
|
|
85
|
+
def check(name, ok, detail) = {name:, status: ok ? :pass : :fail, detail: ok ? nil : detail}
|
|
86
|
+
|
|
87
|
+
# The doctor samples a screen only with a sample user, and skips nested resources, guarded sections and
|
|
88
|
+
# pages that need a record; without a sample there is nothing to read.
|
|
89
|
+
def sample_check(sampled, evidence, failures, unsampled = [])
|
|
90
|
+
return unmeasured("samples render", sampled, unsampled) unless evidence
|
|
91
|
+
|
|
92
|
+
partial(check("samples render", failures.empty?, failures.join("; ")), unsampled)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def html_check(sampled, evidence, html, unsampled = [])
|
|
96
|
+
return unmeasured("no html leaves", sampled, unsampled) unless evidence
|
|
97
|
+
|
|
98
|
+
partial(check("no html leaves", html.empty?, html.join(", ")), unsampled)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def unmeasured(name, sampled, unsampled = [])
|
|
102
|
+
detail = sampled ? "not sampled for this screen" : "not sampled; set config.doctor_user"
|
|
103
|
+
{name:, status: :unknown, detail: [detail, *(unsampled_detail(unsampled) if sampled)].join("; ")}
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def partial(check, unsampled) = (check[:status] == :pass && unsampled.any?) ? check.merge(detail: unsampled_detail(unsampled)) : check
|
|
107
|
+
|
|
108
|
+
def unsampled_detail(unsampled) = ("nothing to inspect in #{unsampled.join(", ")}" if unsampled.any?)
|
|
109
|
+
|
|
110
|
+
def event_checks(counts, key, config)
|
|
111
|
+
EVENTS.map do |name, event|
|
|
112
|
+
count = recent(counts, event, event_resource(event, key, config))
|
|
113
|
+
label = "no #{name.to_s.tr("_", " ")}"
|
|
114
|
+
next {name: label, status: :unknown, detail: "not recorded; set config.event_counts"} unless count
|
|
115
|
+
|
|
116
|
+
check(label, count.zero?, "#{count} recent")
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Each event publishes `resource` its own way: `degraded` as the model's name, the rest as the route key.
|
|
121
|
+
def event_resource(event, key, config) = (event == Degradation::EVENT) ? (config&.resource_name&.to_s || key) : key
|
|
122
|
+
|
|
123
|
+
def recent(counts, event, resource)
|
|
124
|
+
Integer(counts&.call(event, resource), exception: false)
|
|
125
|
+
rescue => e
|
|
126
|
+
Rails.logger.warn("[reactiveadmin] event_counts raised #{e.class}; counts left unknown")
|
|
127
|
+
nil
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Text rendering of a build.
|
|
131
|
+
class Table
|
|
132
|
+
CANT_SEE = [
|
|
133
|
+
"who still uses classic (log the `reactiveadmin_ui` cookie, or `ui.default` changes)",
|
|
134
|
+
"whether operators can do their work in the new admin: ask them",
|
|
135
|
+
"screens the doctor did not sample: nested resources and actions that need a record"
|
|
136
|
+
].freeze
|
|
137
|
+
|
|
138
|
+
def initialize(result)
|
|
139
|
+
@result = result
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def to_s
|
|
143
|
+
[header, rows, blockers, "Not visible to the gem\n#{CANT_SEE.map { " - #{it}" }.join("\n")}"].compact.join("\n\n")
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
private
|
|
147
|
+
|
|
148
|
+
attr_reader :result
|
|
149
|
+
|
|
150
|
+
def header
|
|
151
|
+
s = result[:summary]
|
|
152
|
+
lines = ["reactiveadmin readiness#{" (static: no sample user)" unless result[:sampled]}",
|
|
153
|
+
" #{s[:total]} resources and pages: native #{s[:native]}, degraded #{s[:degraded]}, fallback #{s[:fallback]}; ready to leave classic #{s[:ready]}",
|
|
154
|
+
(" event counts are not recorded; see `config.event_counts`" unless result[:counted])]
|
|
155
|
+
lines.compact.join("\n")
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def rows
|
|
159
|
+
width = result[:rows].map { label(it).size }.max || 0
|
|
160
|
+
lines = result[:rows].map do |row|
|
|
161
|
+
name = label(row)
|
|
162
|
+
unknown = row[:checks].count { it[:status] == :unknown }
|
|
163
|
+
" #{name.ljust(width)} #{row[:mode].ljust(8)} #{row[:ready] ? "ready" : "not ready"}#{" (#{unknown} unknown)" if unknown.positive?}"
|
|
164
|
+
end
|
|
165
|
+
"Resources and pages\n#{lines.join("\n")}"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def label(row) = (row[:kind] == "page") ? "page #{row[:key]}" : row[:key]
|
|
169
|
+
|
|
170
|
+
def blockers
|
|
171
|
+
lines = result[:rows].flat_map do |row|
|
|
172
|
+
row[:checks].select { it[:status] == :fail }.map { " #{label(row)}: #{it[:name]} (#{it[:detail]})" }
|
|
173
|
+
end
|
|
174
|
+
("Blocking\n#{lines.join("\n")}" if lines.any?)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# The rake task's driver; options come from the environment so they pass through `rake`.
|
|
179
|
+
module CLI
|
|
180
|
+
module_function
|
|
181
|
+
|
|
182
|
+
def run(env = ENV, out: $stdout)
|
|
183
|
+
result = Readiness.build(Doctor.run(sample: env["SAMPLE"] != "0"))
|
|
184
|
+
out.puts((env["FORMAT"] == "json") ? JSON.pretty_generate(result) : Table.new(result))
|
|
185
|
+
result
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Single access point to ActiveAdmin's registrations.
|
|
3
|
+
module Registry
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
def namespace = AA.namespace(ReactiveAdmin.config.namespace)
|
|
7
|
+
|
|
8
|
+
def resources = index[:resources]
|
|
9
|
+
|
|
10
|
+
def route_key(config) = config.resource_name.route_key
|
|
11
|
+
|
|
12
|
+
def find(key) = index[:by_key][key]
|
|
13
|
+
|
|
14
|
+
def for_class(klass)
|
|
15
|
+
return unless klass.is_a?(Class)
|
|
16
|
+
|
|
17
|
+
by_class = index[:by_class]
|
|
18
|
+
config = by_class[klass.to_s] || (by_class[klass.base_class.to_s] if klass.respond_to?(:base_class))
|
|
19
|
+
config if config && actions(config).include?(:show) && Exposure.allowed?(config) && !ApiGuards.denies?(config, "show")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# AA's own `defined_actions` lists the controller's instance methods on every call.
|
|
23
|
+
def actions(config) = (index[:actions][config] ||= AA.defined_actions(config))
|
|
24
|
+
|
|
25
|
+
# What the client may offer at the resource's top level, where a `new_needs_parent` resource can't build.
|
|
26
|
+
def top_level_actions(config)
|
|
27
|
+
config.reactive_options[:new_needs_parent] ? actions(config) - %i[new create] : actions(config)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def for_controller_path(path) = index[:by_controller_path][path]
|
|
31
|
+
|
|
32
|
+
def pages = index[:pages]
|
|
33
|
+
|
|
34
|
+
def find_page(key) = index[:pages_by_key][key]
|
|
35
|
+
|
|
36
|
+
def page_for_controller_path(path) = index[:pages_by_controller_path][path]
|
|
37
|
+
|
|
38
|
+
# Records of nested-only resources also carry their paths: `resource` + `id` don't locate them.
|
|
39
|
+
def ref(record)
|
|
40
|
+
config = for_class(record.class) or return
|
|
41
|
+
ref = {resource: route_key(config), id: record.to_param.to_s}
|
|
42
|
+
return ref unless Paths.nested?(config)
|
|
43
|
+
|
|
44
|
+
paths = Paths.record(config, record)
|
|
45
|
+
ref.merge(path: paths[:show], api_path: paths[:api_show]).compact
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Lookups run per menu item and per cell, and AA's own are linear scans that constantize
|
|
49
|
+
# controllers. Registrations only change on (re)load, which swaps or grows the collection.
|
|
50
|
+
def index
|
|
51
|
+
collection = AA.registrations(namespace)
|
|
52
|
+
stamp = [collection.object_id, collection.size]
|
|
53
|
+
return @index if @index && @index_stamp == stamp
|
|
54
|
+
|
|
55
|
+
resources = collection.select { AA.resource?(it) }
|
|
56
|
+
pages = collection.select { AA.page?(it) }
|
|
57
|
+
@index = {
|
|
58
|
+
resources:,
|
|
59
|
+
by_key: resources.index_by { route_key(it) },
|
|
60
|
+
by_class: resources.index_by { it.resource_class.to_s },
|
|
61
|
+
by_controller_path: resources.index_by { it.controller.controller_path },
|
|
62
|
+
pages:,
|
|
63
|
+
pages_by_key: pages.index_by(&:underscored_resource_name),
|
|
64
|
+
pages_by_controller_path: pages.index_by { it.controller.controller_path },
|
|
65
|
+
actions: {}
|
|
66
|
+
}.tap { @index_stamp = stamp }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Everything a client needs to render and drive one resource's index: filters, scopes, batch
|
|
3
|
+
# actions, header buttons. Built inside the resource's own controller so AA's procs reach its
|
|
4
|
+
# helpers. Only what changes with code and permissions is cached; anything that can read
|
|
5
|
+
# params or the request (`if:` procs, option lists, action items) is built per request.
|
|
6
|
+
class ResourceMeta
|
|
7
|
+
BOOT_ID = SecureRandom.hex(4)
|
|
8
|
+
REVISION_VARS = %w[GIT_SHA GIT_COMMIT SOURCE_VERSION REVISION].freeze
|
|
9
|
+
EXPORTABLE = %w[csv].freeze
|
|
10
|
+
|
|
11
|
+
def self.fetch(controller) = new(controller).as_json
|
|
12
|
+
|
|
13
|
+
# Without a configured version, code is assumed to change with the process (and with every
|
|
14
|
+
# reload in development), which is safe but not shared between processes. `seed` identifies
|
|
15
|
+
# what reloads: the resource's config, or whatever a document is built from.
|
|
16
|
+
def self.code_version(seed)
|
|
17
|
+
configured = ReactiveAdmin.config.meta_cache_version
|
|
18
|
+
return configured.call if configured
|
|
19
|
+
return seed.object_id if Rails.application.config.enable_reloading
|
|
20
|
+
|
|
21
|
+
ENV.values_at(*REVISION_VARS).compact.first || BOOT_ID
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.pagination_total?(config, presenter = AA.page_presenter(config, :index))
|
|
25
|
+
config.paginate && presenter&.fetch(:pagination_total, true) != false
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(controller)
|
|
29
|
+
@controller = controller
|
|
30
|
+
@config = AA.config_of(controller)
|
|
31
|
+
@view = controller.view_context
|
|
32
|
+
@authorizer = Authorizer.for(controller)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def cache_key
|
|
36
|
+
["reactiveadmin", "meta", Registry.route_key(@config), version, I18n.locale, ReactiveAdmin.user_fingerprint(@controller), ApiGuards.channel]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def as_json(*)
|
|
40
|
+
static.merge(dynamic)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def static
|
|
44
|
+
Rails.cache.fetch(cache_key, expires_in: ReactiveAdmin.config.meta_cache_ttl) do
|
|
45
|
+
views = IndexViews.new(@config)
|
|
46
|
+
ResourceSummary.new(@config, @authorizer).as_json.merge(
|
|
47
|
+
per_page_options: views.per_page_options || Array(@config.per_page).map(&:to_i),
|
|
48
|
+
default_per_page: Array(@config.per_page).first&.to_i || AA.default_per_page,
|
|
49
|
+
default_order: @config.sort_order,
|
|
50
|
+
pagination_total: self.class.pagination_total?(@config),
|
|
51
|
+
**{index_views: views.list}.compact
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def dynamic
|
|
57
|
+
declared(options: true).merge(action_items:)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# What the resource declares, without action items. `options: false` also leaves out option lists,
|
|
61
|
+
# so nothing is read from records.
|
|
62
|
+
def declared(options: false)
|
|
63
|
+
scopes = visible_scopes
|
|
64
|
+
|
|
65
|
+
{
|
|
66
|
+
filters: AA.filters_enabled?(@config) ? Filters.new(@config, @view, options:).call : [],
|
|
67
|
+
batch_actions: BatchActions.new(@config, @view).as_json,
|
|
68
|
+
scopes: IndexViews.scopes_in_render_order(scopes).map { scope_json(it) },
|
|
69
|
+
default_scope: scopes.find { default_scope?(it) }&.id,
|
|
70
|
+
download_formats:
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def download_formats
|
|
75
|
+
links = IndexViews.new(@config, as: @controller.params[:as]).download_links
|
|
76
|
+
links = safely(false, "download_links") { @view.instance_exec(&links) } if links.is_a?(Proc)
|
|
77
|
+
formats = (links.is_a?(Array) && links.any?) ? links : (AA.default_download_formats(@config) unless links == false)
|
|
78
|
+
Array(formats).map(&:to_s) & EXPORTABLE
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def version = self.class.code_version(@config)
|
|
84
|
+
|
|
85
|
+
def action_items
|
|
86
|
+
items = ActionItems.new(@controller, "index").call(generated_new)
|
|
87
|
+
[*items, *Actions.new(@config, @authorizer, view: @view).exposed(items)]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The client derives its New button from permissions; AA's own item would duplicate it.
|
|
91
|
+
def generated_new
|
|
92
|
+
return [] unless Registry.actions(@config).include?(:new)
|
|
93
|
+
|
|
94
|
+
[{method: "get", href: File.join(@config.route_collection_path, "new")}]
|
|
95
|
+
rescue ActionController::UrlGenerationError
|
|
96
|
+
[]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def scope_json(scope)
|
|
100
|
+
{id: scope.id, name: scope_name(scope), count: nil}.merge(scope.group ? {group: scope.group.to_s} : {})
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Named block parameter: a bare `it` inside `safely`'s block would be that block's own argument.
|
|
104
|
+
def visible_scopes = AA.scopes(@config).select { |scope| safely(true, "scope[#{scope.id}]") { AA.scope_visible?(@view, scope) } }
|
|
105
|
+
|
|
106
|
+
def default_scope?(scope)
|
|
107
|
+
block = scope.default_block
|
|
108
|
+
safely(false, "scope[#{scope.id}]") { block.is_a?(Proc) ? @view.instance_exec(&block) : block }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def scope_name(scope) = safely(scope.id.humanize, "scope[#{scope.id}]") { AA.scope_name(@view, scope) }
|
|
112
|
+
|
|
113
|
+
def safely(fallback, what)
|
|
114
|
+
yield
|
|
115
|
+
rescue => e
|
|
116
|
+
Degradation.record(kind: :meta_block_failed, where: "index/#{what}", reason: "Block raised #{e.class}; used a default")
|
|
117
|
+
fallback
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# What the shell needs of a resource: enough to build the menu and routes, cheap to compute
|
|
3
|
+
# for every registered resource.
|
|
4
|
+
class ResourceSummary
|
|
5
|
+
def initialize(config, authorizer)
|
|
6
|
+
@config = config
|
|
7
|
+
@authorizer = authorizer
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def permissions = @permissions ||= Permissions.new(@config, @authorizer).resource
|
|
11
|
+
|
|
12
|
+
def as_json(*)
|
|
13
|
+
placement = AA.menu_placement(@config)
|
|
14
|
+
template = Paths.index_template(@config)
|
|
15
|
+
|
|
16
|
+
{
|
|
17
|
+
key: Registry.route_key(@config),
|
|
18
|
+
label: @config.resource_label.to_s,
|
|
19
|
+
plural_label: @config.plural_resource_label.to_s,
|
|
20
|
+
menu_parent: placement&.[](:parent),
|
|
21
|
+
menu_priority: placement&.[](:priority),
|
|
22
|
+
meta_path: Paths.api(Paths.meta(@config)),
|
|
23
|
+
belongs_to:,
|
|
24
|
+
index_path_template: template,
|
|
25
|
+
api_index_path_template: (Paths.api(template) if template),
|
|
26
|
+
actions: Registry.top_level_actions(@config).map(&:to_s),
|
|
27
|
+
permissions:,
|
|
28
|
+
api_denied: ApiGuards.summary(@config)
|
|
29
|
+
}.compact # a lean manifest: 100+ resources, so absent beats null
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def belongs_to
|
|
35
|
+
return unless @config.belongs_to?
|
|
36
|
+
|
|
37
|
+
parent = @config.belongs_to_config
|
|
38
|
+
{parent: Registry.route_key(parent.target), param: parent.to_param.to_s, optional: !!parent.optional?}
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Re-draws AA's routes under mount_path with the ReactiveAdmin format so AA's own
|
|
3
|
+
# controllers serve the JSON API.
|
|
4
|
+
def self.routes(router)
|
|
5
|
+
ns = config.namespace
|
|
6
|
+
router.scope(config.mount_path, as: :reactiveadmin, defaults: {format: :reactiveadmin}, constraints: method(:api_format?)) do
|
|
7
|
+
router.get "#{ns}/_manifest", to: "reactive_admin/manifest#show"
|
|
8
|
+
router.get "#{ns}/_session", to: "reactive_admin/session#show"
|
|
9
|
+
router.get "#{ns}/_openapi", to: "reactive_admin/openapi#show"
|
|
10
|
+
router.post "#{ns}/_client_errors", to: "reactive_admin/client_errors#create"
|
|
11
|
+
router.post "#{ns}/_mcp", to: "reactive_admin/mcp#create"
|
|
12
|
+
router.match "#{ns}/_mcp", to: "reactive_admin/mcp#unsupported", via: %i[get delete]
|
|
13
|
+
# Must precede AA's `/:id` routes, which would otherwise swallow `_options` and `_meta`.
|
|
14
|
+
AA.load_registrations!
|
|
15
|
+
Registry.resources.each do |config|
|
|
16
|
+
controller = config.controller.controller_path
|
|
17
|
+
router.get(Paths.meta(config), to: "#{controller}#reactiveadmin_meta")
|
|
18
|
+
router.get(Paths.labels(config), to: "#{controller}#reactiveadmin_labels") unless Paths.nested?(config)
|
|
19
|
+
to = "#{controller}#reactiveadmin_options"
|
|
20
|
+
router.get(Paths.options(config), to:)
|
|
21
|
+
lookup = "#{controller}#reactiveadmin_lookup"
|
|
22
|
+
router.get(Paths.lookup(config), to: lookup)
|
|
23
|
+
value = "#{controller}#reactiveadmin_value"
|
|
24
|
+
router.get(Paths.value(config), to: value) unless Paths.nested?(config)
|
|
25
|
+
router.get(Paths.value(config, parent_id: ":#{config.belongs_to_config.to_param}"), to: value) if config.belongs_to?
|
|
26
|
+
# Form options of a nested resource are built from a record under its parent.
|
|
27
|
+
if config.belongs_to?
|
|
28
|
+
parent_id = ":#{config.belongs_to_config.to_param}"
|
|
29
|
+
router.get(Paths.options(config, parent_id:), to:)
|
|
30
|
+
router.get(Paths.lookup(config, parent_id:), to: lookup)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
Registry.pages.each do |page|
|
|
34
|
+
# A page under a required parent is only routed under it, as its classic route is.
|
|
35
|
+
parent = "#{Registry.route_key(page.belongs_to_config.target)}/:#{page.belongs_to_config.to_param}/" if Paths.nested?(page)
|
|
36
|
+
router.get "#{ns}/#{parent}_pages/#{page.underscored_resource_name}", to: "#{page.controller.controller_path}#reactiveadmin_page"
|
|
37
|
+
end
|
|
38
|
+
AA.draw_routes(router)
|
|
39
|
+
# Last: anything unrouted answers in JSON instead of the host's html 404 page.
|
|
40
|
+
router.match "#{ns}/*path", to: NOT_FOUND, via: :all, format: false, as: :not_found
|
|
41
|
+
end
|
|
42
|
+
ui_routes(router)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The built client's files, for hosts whose static file server doesn't serve them.
|
|
46
|
+
def self.ui_routes(router)
|
|
47
|
+
prefix = config.ui.asset_prefix.to_s
|
|
48
|
+
router.mount Ui::Assets::Server.new, at: prefix, as: :reactiveadmin_assets if prefix.start_with?("/")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
NOT_FOUND = ->(_env) { [404, {"Content-Type" => "application/json"}, ['{"code":"not_found","message":"Not found"}']] }
|
|
52
|
+
|
|
53
|
+
# AA's responders would serialize whole records for .json and .xml, so the mount only answers
|
|
54
|
+
# in its own format, plus CSV on index (AA's streamed export, which honours `csv do` blocks).
|
|
55
|
+
def self.api_format?(request)
|
|
56
|
+
params = request.path_parameters
|
|
57
|
+
format = params[:format].to_s
|
|
58
|
+
format == "reactiveadmin" || (format == "csv" && params[:action] == "index")
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Rails keys a has_many row's errors by `assoc.attribute` alone, so a list can't tell which row failed.
|
|
3
|
+
# This names the rows: a saved one by its id, a new one by its place among the new rows, which is the order
|
|
4
|
+
# the client sent them in.
|
|
5
|
+
class RowErrors
|
|
6
|
+
def self.call(record, associations) = new(record).call(associations.to_a).presence
|
|
7
|
+
|
|
8
|
+
def initialize(record)
|
|
9
|
+
@record = record
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call(associations)
|
|
13
|
+
associations.to_h { [it.to_s, rows(it)] }.reject { _2.empty? }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def rows(assoc)
|
|
19
|
+
fresh = 0
|
|
20
|
+
@record.association(assoc).target.filter_map do |child|
|
|
21
|
+
position = fresh unless child.persisted?
|
|
22
|
+
fresh += 1 unless child.persisted?
|
|
23
|
+
next if child.marked_for_destruction? || child.errors.empty?
|
|
24
|
+
|
|
25
|
+
{id: (child.to_param.to_s if child.persisted?), new: position, errors: child.errors.to_hash}.compact
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require "erb"
|
|
2
|
+
require "json"
|
|
3
|
+
require "yaml"
|
|
4
|
+
require "json_schemer"
|
|
5
|
+
require "reactive_admin"
|
|
6
|
+
|
|
7
|
+
module ReactiveAdmin
|
|
8
|
+
# Contract checks for request specs. Needs json_schemer in the host's test group.
|
|
9
|
+
module RSpec
|
|
10
|
+
# openapi/core.yaml with every object schema that lists properties closed, so an undeclared key
|
|
11
|
+
# fails here instead of reaching the client untyped.
|
|
12
|
+
module Contract
|
|
13
|
+
PATH = OpenApi::Core::PATH
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def document = @document ||= YAML.safe_load_file(PATH, aliases: true)
|
|
18
|
+
|
|
19
|
+
def openapi = @openapi ||= JSONSchemer.openapi(closed(document.deep_dup))
|
|
20
|
+
|
|
21
|
+
def closed(node)
|
|
22
|
+
case node
|
|
23
|
+
when Array then node.each { closed(it) }
|
|
24
|
+
when Hash
|
|
25
|
+
node["additionalProperties"] = false if node.key?("properties") && !node.key?("additionalProperties")
|
|
26
|
+
node.each_value { closed(it) }
|
|
27
|
+
end
|
|
28
|
+
node
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# `schema` is a component name (`IndexResponse`) or a JSON pointer into core.yaml.
|
|
32
|
+
def errors(schema, data)
|
|
33
|
+
pointer = schema.start_with?("#") ? schema : "#/components/schemas/#{schema}"
|
|
34
|
+
openapi.ref(pointer).validate(JSON.parse(data.to_json)).map { it["error"] }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def valid?(schema, data) = errors(schema, data).empty?
|
|
38
|
+
|
|
39
|
+
# What core.yaml declares for `status` on the operation that serves `path`; nil when it declares nothing.
|
|
40
|
+
def response_schema(verb, path, status)
|
|
41
|
+
keys = ["paths", template(path), verb.to_s.downcase, "responses", status.to_s]
|
|
42
|
+
ref = document.dig(*keys, "$ref")
|
|
43
|
+
keys = ref.delete_prefix("#/").split("/") if ref
|
|
44
|
+
keys += ["content", "application/json", "schema"]
|
|
45
|
+
schema = document.dig(*keys) or return
|
|
46
|
+
schema["$ref"]&.delete_prefix("#/components/schemas/") || pointer(keys)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def pointer(keys) = "#/#{keys.map { ERB::Util.url_encode(it.gsub("~", "~0").gsub("/", "~1")) }.join("/")}"
|
|
50
|
+
|
|
51
|
+
# Most literal first, so `/admin/{resource}/new` wins over `/admin/{resource}/{id}`.
|
|
52
|
+
def template(path)
|
|
53
|
+
route = canonical(path.split("?").first.delete_prefix(ReactiveAdmin.config.mount_path))
|
|
54
|
+
templates.find { |_, pattern| pattern.match?(route) }&.first
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# core.yaml says `/admin/{resource}/...`: map a custom namespace to it, and a route nested under
|
|
58
|
+
# its parent (`/posts/42/comments/new`) to the resource's own.
|
|
59
|
+
def canonical(route)
|
|
60
|
+
_, namespace, *parts = route.split("/", -1)
|
|
61
|
+
return route unless namespace == ReactiveAdmin.config.namespace.to_s
|
|
62
|
+
|
|
63
|
+
parts.slice!(0, 2) if nested_under_parent?(parts)
|
|
64
|
+
["", "admin", *parts].join("/")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def nested_under_parent?(parts)
|
|
68
|
+
child = (parts[2] == "_pages") ? Registry.find_page(parts[3]) : Registry.find(parts[2])
|
|
69
|
+
child&.belongs_to? && Registry.route_key(child.belongs_to_config.target) == parts[0]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def templates
|
|
73
|
+
@templates ||= document["paths"].keys
|
|
74
|
+
.sort_by { |path| -path.split("/").count { !it.include?("{") } }
|
|
75
|
+
.map { |path| [path, /\A#{path.split(/\{\w+\}/, -1).map { Regexp.escape(it) }.join("[^/]+")}\z/] }
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
HEADERS = {"ACCEPT" => "application/json"}.freeze
|
|
80
|
+
|
|
81
|
+
module_function
|
|
82
|
+
|
|
83
|
+
# Adds an example per registration to a request spec group: its meta, index and first listed
|
|
84
|
+
# record answer as core.yaml says. Sign in, as a user who can read everything, in a `before`.
|
|
85
|
+
# Resources that only exist under a parent are left out.
|
|
86
|
+
def contract_examples(group)
|
|
87
|
+
Rails.application.reload_routes_unless_loaded
|
|
88
|
+
Registry.resources.reject { Paths.nested?(it) }.each do |config|
|
|
89
|
+
key = Registry.route_key(config)
|
|
90
|
+
group.it "serves #{key} as the contract describes" do
|
|
91
|
+
check = lambda do |path|
|
|
92
|
+
get(path, headers: HEADERS)
|
|
93
|
+
expect(response.status).to be < 500, "GET #{path} answered #{response.status}"
|
|
94
|
+
schema = (response.status >= 400) ? "Error" : Contract.response_schema("get", path, response.status)
|
|
95
|
+
body = response.parsed_body if response.media_type == "application/json"
|
|
96
|
+
expect(body).not_to be_nil, "GET #{path} answered #{response.status} #{response.media_type}"
|
|
97
|
+
errors = schema ? Contract.errors(schema, body) : []
|
|
98
|
+
expect(errors).to be_empty, "GET #{path} (#{response.status}) doesn't match #{schema}:\n #{errors.first(8).join("\n ")}"
|
|
99
|
+
body
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
check.call(Paths.api(Paths.meta(config)))
|
|
103
|
+
index = check.call(Paths.api(Paths.collection(config)))
|
|
104
|
+
show = response.successful? && index["rows"].first&.dig("paths", "api_show")
|
|
105
|
+
check.call(show) if show
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
RSpec::Matchers.define :match_contract do |schema|
|
|
113
|
+
match { |data| ReactiveAdmin::RSpec::Contract.valid?(schema, data) }
|
|
114
|
+
|
|
115
|
+
failure_message { |data| "expected payload to match #{schema}:\n #{ReactiveAdmin::RSpec::Contract.errors(schema, data).first(8).join("\n ")}" }
|
|
116
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# What error reporters see in place of an unexpected exception: the original's message can carry
|
|
3
|
+
# record data (a failed SQL statement, a validation value), so only its class, where it was raised
|
|
4
|
+
# and the request it came from are passed on.
|
|
5
|
+
class ScrubbedError < StandardError
|
|
6
|
+
attr_reader :original_class
|
|
7
|
+
|
|
8
|
+
def self.report(exception, resource:, action:, request_id:)
|
|
9
|
+
error = new(exception, resource:, action:)
|
|
10
|
+
context = {resource:, action:, request_id:, error_class: error.original_class}
|
|
11
|
+
ReactiveAdmin.config.error_reporter.call(error, context)
|
|
12
|
+
rescue => e
|
|
13
|
+
Rails.logger.error("[reactiveadmin] error reporter failed: #{e.class}")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(original, resource: nil, action: nil)
|
|
17
|
+
@original_class = original.class.name
|
|
18
|
+
super("#{original_class} in #{resource}##{action}")
|
|
19
|
+
set_backtrace(original.backtrace)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|