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,442 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# FormResponse for a custom ERB template whose forms are Formtastic-only. The template renders as
|
|
3
|
+
# it does in the classic admin, except that its form builders record their inputs instead of
|
|
4
|
+
# printing them. The first form is the response; html around it is kept in order as html fields,
|
|
5
|
+
# unless the template has more to show (further forms, row actions, results after the form), in
|
|
6
|
+
# which case everything outside the form becomes typed `layout` nodes with the form in place.
|
|
7
|
+
class TemplateForm
|
|
8
|
+
Unsupported = Class.new(StandardError)
|
|
9
|
+
|
|
10
|
+
METHODS = %w[get post put patch delete].freeze
|
|
11
|
+
|
|
12
|
+
# Mixed into the view that renders the template.
|
|
13
|
+
module Recording
|
|
14
|
+
def semantic_form_for(*args, &block)
|
|
15
|
+
state = @_reactiveadmin_template
|
|
16
|
+
return super unless state
|
|
17
|
+
return state.extra_form(self) { semantic_form_for(*args, &block) } if state.extra_form?
|
|
18
|
+
|
|
19
|
+
items = nil
|
|
20
|
+
markup = super do |builder|
|
|
21
|
+
state.objects << builder.object
|
|
22
|
+
items = state.expand(state.capture(self, Recorder.new(builder, state, self), &block))
|
|
23
|
+
"".html_safe
|
|
24
|
+
end
|
|
25
|
+
state.mark([:form, {items:, **Form.target(markup)}])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# AA's version builds an Arbre form around a real builder, which the recorder can't stand in for.
|
|
29
|
+
def active_admin_form_for(resource, options = {}, &block)
|
|
30
|
+
state = @_reactiveadmin_template
|
|
31
|
+
return super unless state
|
|
32
|
+
return state.extra_form(self) { active_admin_form_for(resource, options, &block) } if state.extra_form?
|
|
33
|
+
|
|
34
|
+
state.arbre { semantic_form_for(resource, {builder: AA.form_builder}.merge(options), &block) }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Reads the form tag Rails built: its action and verb, whichever way `url:` and `method:` were given.
|
|
39
|
+
module Form
|
|
40
|
+
module_function
|
|
41
|
+
|
|
42
|
+
def target(markup)
|
|
43
|
+
form = HtmlPolicy.parse(markup.to_s)&.at_css("form")
|
|
44
|
+
verb = form&.at_css("input[name=_method]")&.[]("value") || form&.[]("method")
|
|
45
|
+
{path: form&.[]("action"), method: verb.to_s.downcase}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# A GET form is a client-side navigation: it must re-read the very view that served it, with its
|
|
49
|
+
# fields as the query. Returns that view's classic path, or nil for a form that goes to another page.
|
|
50
|
+
def own_view(url, request)
|
|
51
|
+
uri = URI.parse(url.to_s)
|
|
52
|
+
return if uri.host.present? && !uri.host.casecmp?(request.host)
|
|
53
|
+
return view_path(request) if uri.path.blank?
|
|
54
|
+
|
|
55
|
+
uri.path.chomp("/") if same_route?(uri.path, request)
|
|
56
|
+
rescue URI::InvalidURIError
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# By route rather than by path, so a request that reached the action by another URL (a trailing slash, the
|
|
61
|
+
# API's page endpoint) still counts as the same view.
|
|
62
|
+
def same_route?(path, request)
|
|
63
|
+
current = identify(request.path_parameters)
|
|
64
|
+
identify(Rails.application.routes.recognize_path(path, method: "GET")) == current
|
|
65
|
+
rescue ActionController::RoutingError
|
|
66
|
+
false
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def identify(params)
|
|
70
|
+
params.except(:format).transform_values(&:to_s).tap { it[:action] = "index" if it[:action] == "reactiveadmin_page" }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The classic path of the request being served, which an API request reaches under the mount.
|
|
74
|
+
def view_path(request) = request.path.delete_prefix(ReactiveAdmin.config.mount_path).sub("/_pages/", "/").chomp("/")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The ordered record of what a template printed: each recording call leaves a marker in the output
|
|
78
|
+
# that points at its entry, so inputs and plain html keep the order the template gave them.
|
|
79
|
+
class State
|
|
80
|
+
attr_reader :schema, :entries, :objects, :marker_pattern
|
|
81
|
+
attr_accessor :submit_label, :submit_confirm
|
|
82
|
+
|
|
83
|
+
# With a controller, forms after the first record as inline `form` nodes of the layout.
|
|
84
|
+
def initialize(schema, controller: nil)
|
|
85
|
+
@token = SecureRandom.hex(8)
|
|
86
|
+
@marker_pattern = /<!--ra:#{@token}:(\d+)-->/
|
|
87
|
+
@schema = schema
|
|
88
|
+
@controller = controller
|
|
89
|
+
@entries = []
|
|
90
|
+
@buffers = []
|
|
91
|
+
@objects = []
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def extra_form? = @controller && entries.any? { it.first == :form }
|
|
95
|
+
|
|
96
|
+
def extras? = @extras
|
|
97
|
+
|
|
98
|
+
# A form the recorder can't describe is left to print as it does in the classic admin.
|
|
99
|
+
def extra_form(view, &block)
|
|
100
|
+
@extras = true
|
|
101
|
+
forms.node(view, &block) || forms.suspended { printing(view, &block) }
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def mark(entry)
|
|
105
|
+
@entries << entry
|
|
106
|
+
marker(@entries.size - 1).html_safe.tap { @buffers.last&.push(it) }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# AA's form components record by statement (`f.input` registers itself), where Formtastic
|
|
110
|
+
# templates print with `<%= %>`. Within an arbre form every mark counts, not just the printed ones.
|
|
111
|
+
def arbre
|
|
112
|
+
previous = @arbre
|
|
113
|
+
@arbre = true
|
|
114
|
+
yield
|
|
115
|
+
ensure
|
|
116
|
+
@arbre = previous
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def capture(view, *args, &block)
|
|
120
|
+
return view.capture(*args, &block) unless @arbre
|
|
121
|
+
|
|
122
|
+
buffer = []
|
|
123
|
+
@buffers.push(buffer)
|
|
124
|
+
printed = view.capture(*args, &block).to_s
|
|
125
|
+
(buffer.size > printed.scan(marker_pattern).size) ? buffer.join.html_safe : printed
|
|
126
|
+
ensure
|
|
127
|
+
@buffers.pop if buffer
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def expand(html)
|
|
131
|
+
html.to_s.split(marker_pattern).each_slice(2).flat_map do |text, id|
|
|
132
|
+
[(text unless text.strip.empty?), (@entries[id.to_i] if id)].compact
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# scan_html treats markup too deep to parse as unsupported raw input, so the form isn't submittable.
|
|
137
|
+
def html_field(html)
|
|
138
|
+
schema.scan_html(html)
|
|
139
|
+
return if schema.controls_only?
|
|
140
|
+
|
|
141
|
+
FormField.html(HtmlNodes.sanitize(html).strip, schema.next_html_name) if visible?(html)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The fields among recorded items, in order; plain html between them becomes html fields.
|
|
145
|
+
def fields(items)
|
|
146
|
+
items.flat_map do |item|
|
|
147
|
+
case item
|
|
148
|
+
in String then [html_field(item)].compact
|
|
149
|
+
in [:field, Hash => field] then [field]
|
|
150
|
+
in [:section, Hash => section] then section[:fields]
|
|
151
|
+
in [:group, Array => list] then fields(list)
|
|
152
|
+
else []
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Plain html before, between and after the forms stays as html fields; the first heading is the title.
|
|
158
|
+
def arrange(html) = arrange_items(expand(html).flat_map { |item| (item in [:form, Hash => inner]) ? inner[:items] : [item] })
|
|
159
|
+
|
|
160
|
+
def arrange_items(items)
|
|
161
|
+
title = nil
|
|
162
|
+
sections = []
|
|
163
|
+
loose = nil
|
|
164
|
+
items.each do |item|
|
|
165
|
+
case item
|
|
166
|
+
in String
|
|
167
|
+
title ||= extract_title!(item)
|
|
168
|
+
field = html_field(item) or next
|
|
169
|
+
(loose ||= new_section(sections)) && loose[:fields] << field
|
|
170
|
+
in [:section, section]
|
|
171
|
+
loose = nil
|
|
172
|
+
sections << section
|
|
173
|
+
in [:field | :group, _]
|
|
174
|
+
fields([item]).each { (loose ||= new_section(sections))[:fields] << it }
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
[sections.reject { it[:fields].empty? }, title]
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Whether the template shows more than its form: further forms, controls that act, or content after it.
|
|
181
|
+
def layout?(html, index)
|
|
182
|
+
return false unless html.to_s.include?(marker(index))
|
|
183
|
+
return true if extras? || ActionLinks.controls(html).any? { ActionLinks.control?(it) }
|
|
184
|
+
|
|
185
|
+
visible?(html.to_s.split(marker(index), 2).last)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# The token is random per render, so stored text can't forge a marker for an entry.
|
|
189
|
+
def marker(index) = "<!--ra:#{@token}:#{index}-->"
|
|
190
|
+
|
|
191
|
+
def new_section(sections) = {kind: "inputs", fields: []}.tap { sections << it }
|
|
192
|
+
|
|
193
|
+
def extract_title!(chunk)
|
|
194
|
+
fragment = HtmlPolicy.parse(chunk) or return
|
|
195
|
+
heading = fragment.at_css("h1, h2, h3, h4") or return
|
|
196
|
+
heading.text.strip.presence&.tap do
|
|
197
|
+
heading.remove
|
|
198
|
+
chunk.replace(fragment.to_html)
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
private
|
|
203
|
+
|
|
204
|
+
def forms = (@forms ||= PageForms.new(@controller, options: false))
|
|
205
|
+
|
|
206
|
+
def printing(view)
|
|
207
|
+
previous = view.instance_variable_get(:@_reactiveadmin_template)
|
|
208
|
+
view.instance_variable_set(:@_reactiveadmin_template, nil)
|
|
209
|
+
yield
|
|
210
|
+
ensure
|
|
211
|
+
view.instance_variable_set(:@_reactiveadmin_template, previous)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Too deep to read is still shown, as the too-complex notice.
|
|
215
|
+
def visible?(html)
|
|
216
|
+
fragment = HtmlPolicy.parse(html) or return true
|
|
217
|
+
|
|
218
|
+
fragment.text.strip.present? || fragment.at_css("img, table")
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Stands in for the Formtastic builder inside the template's form block.
|
|
223
|
+
class Recorder
|
|
224
|
+
NOOPS = %i[semantic_errors cancel_link commit_action_with_cancel_link].freeze
|
|
225
|
+
|
|
226
|
+
attr_reader :builder
|
|
227
|
+
|
|
228
|
+
def initialize(builder, state, view, path: nil)
|
|
229
|
+
@builder = builder
|
|
230
|
+
@state = state
|
|
231
|
+
@view = view
|
|
232
|
+
@path = path
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def object = builder.object
|
|
236
|
+
|
|
237
|
+
def input(method, options = {}, &)
|
|
238
|
+
field = field(method, options)
|
|
239
|
+
field ? @state.mark([:field, field]) : "".html_safe
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def inputs(*args, &block)
|
|
243
|
+
options = args.extract_options!
|
|
244
|
+
title = options[:name] || options[:title] || (args.shift if args.first.is_a?(String))
|
|
245
|
+
yielded = block&.arity&.zero? ? [] : [self]
|
|
246
|
+
items = block ? @state.expand(@state.capture(@view, *yielded, &block)) : listed(args, options)
|
|
247
|
+
@state.mark([:section, {title: title&.to_s, kind: "inputs", fields: @state.fields(items)}.compact])
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def semantic_fields_for(name, *args, &block)
|
|
251
|
+
items = []
|
|
252
|
+
builder.semantic_fields_for(name, *args) do |nested|
|
|
253
|
+
items.concat(@state.expand(@state.capture(@view, self.class.new(nested, @state, @view, path: [@path, name].compact.join(".")), &block)))
|
|
254
|
+
"".html_safe
|
|
255
|
+
end
|
|
256
|
+
@state.mark([:group, items])
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def actions(*, &block)
|
|
260
|
+
@state.capture(@view, self, &block) if block
|
|
261
|
+
"".html_safe
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def action(name, options = {})
|
|
265
|
+
return "".html_safe unless name.to_s == "submit"
|
|
266
|
+
|
|
267
|
+
@state.submit_label ||= options[:label].to_s if options[:label]
|
|
268
|
+
confirming(options[:button_html])
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def submit(label = nil, *args)
|
|
272
|
+
@state.submit_label = label.to_s if label.is_a?(String)
|
|
273
|
+
confirming(args.last || label)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
NOOPS.each { |name| define_method(name) { |*, &| "".html_safe } }
|
|
277
|
+
|
|
278
|
+
private
|
|
279
|
+
|
|
280
|
+
# The `data-confirm` a classic browser asks about before posting.
|
|
281
|
+
def confirming(options)
|
|
282
|
+
data = options.is_a?(Hash) ? options.with_indifferent_access : {}
|
|
283
|
+
@state.submit_confirm ||= (data.dig(:data, :confirm) || data["data-confirm"]).presence&.to_s
|
|
284
|
+
"".html_safe
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def field(method, options)
|
|
288
|
+
field = FormField.new(builder, @state.schema, @path).call(method.to_sym, options)
|
|
289
|
+
(field && @path) ? field.merge(name: "#{@path}.#{field[:name]}") : field
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def listed(names, options)
|
|
293
|
+
(names.presence || AA.default_form_columns(builder, except: options[:except])).filter_map { field(it, {}) }.map { [:field, it] }
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
ruby2_keywords def method_missing(name, *args, &block)
|
|
297
|
+
return super unless builder.respond_to?(name)
|
|
298
|
+
|
|
299
|
+
builder.public_send(name, *args, &block).to_s
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def respond_to_missing?(name, include_all = false) = builder.respond_to?(name, include_all) || super
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Form fields need a schema for choices and html field names; a template has no record to build one from.
|
|
306
|
+
class Schema < FormSchema
|
|
307
|
+
def initialize(controller, view, only: nil, controls_only: false)
|
|
308
|
+
@only = only
|
|
309
|
+
@controls_only = controls_only
|
|
310
|
+
@view = view
|
|
311
|
+
@assigns = controller.view_assigns
|
|
312
|
+
@config = AA.config_of(controller)
|
|
313
|
+
@record = nil
|
|
314
|
+
@choices = {}
|
|
315
|
+
@html_fields = 0
|
|
316
|
+
@parent_id = nil
|
|
317
|
+
@params = controller.params
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
attr_writer :sections
|
|
321
|
+
|
|
322
|
+
def sections = (@sections ||= [])
|
|
323
|
+
|
|
324
|
+
def record_id = @params[:id].presence&.to_s
|
|
325
|
+
|
|
326
|
+
def partial? = false
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def initialize(controller, template, only: nil)
|
|
330
|
+
@controller = controller
|
|
331
|
+
@template = template
|
|
332
|
+
@only = only
|
|
333
|
+
@config = AA.config_of(controller)
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
# FormResponse.
|
|
337
|
+
def as_json(*)
|
|
338
|
+
NodeRegistry.with_scope do
|
|
339
|
+
state, html = render
|
|
340
|
+
index = state.entries.index { it.first == :form } or raise Unsupported, "#{@template} renders no Formtastic form"
|
|
341
|
+
form = state.entries[index].last
|
|
342
|
+
raise Unsupported, "#{@template} has a #{form[:method]} form" unless METHODS.include?(form[:method])
|
|
343
|
+
own = Form.own_view(form[:path], @controller.request) if form[:method] == "get"
|
|
344
|
+
raise Unsupported, "#{@template} has a GET form to another page" if form[:method] == "get" && !own
|
|
345
|
+
|
|
346
|
+
RansackGuard.learn(state.entries.filter_map { it.last if it.first == :form })
|
|
347
|
+
|
|
348
|
+
@objects = state.objects
|
|
349
|
+
layout, heading = content_layout(state, html, index)
|
|
350
|
+
sections, title = layout ? state.arrange_items(form[:items]) : state.arrange(html)
|
|
351
|
+
state.schema.sections = sections
|
|
352
|
+
sections.each { |section| section[:fields].each { it[:options_path] &&= options_path(it[:name]) } }
|
|
353
|
+
Degradation.attach({
|
|
354
|
+
resource: key, id: @controller.params[:id].presence&.to_s, title: title || heading || default_title,
|
|
355
|
+
display_name: display_name, actions: [], action: {method: form[:method], path: Paths.api(own || form[:path].to_s)},
|
|
356
|
+
form: {sections:}, layout:, submit_label: state.submit_label, submit_confirm: state.submit_confirm, permissions: permissions,
|
|
357
|
+
submittable: state.schema.submittable?, unsupported_reason: state.schema.unsupported_reason, lock: lock
|
|
358
|
+
}.compact)
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# What the template's forms were built on, once it has rendered: a write that renders it again
|
|
363
|
+
# is rejected when one of them has errors.
|
|
364
|
+
def objects = @objects || []
|
|
365
|
+
|
|
366
|
+
# Choices of one field, for the _options endpoint.
|
|
367
|
+
def choices(field)
|
|
368
|
+
render.first.schema.choices.fetch(field) { raise ActiveRecord::RecordNotFound, "No options for #{field}" }
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# Runs the block (a render) on a view whose Formtastic forms record; returns the state and the html.
|
|
372
|
+
def self.recording(schema, view, controller: nil)
|
|
373
|
+
state = State.new(schema, controller:)
|
|
374
|
+
view.extend(Recording)
|
|
375
|
+
view.instance_variable_set(:@_reactiveadmin_template, state)
|
|
376
|
+
[state, yield]
|
|
377
|
+
ensure
|
|
378
|
+
view.instance_variable_set(:@_reactiveadmin_template, nil)
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
private
|
|
382
|
+
|
|
383
|
+
def render
|
|
384
|
+
@controller.lookup_context.formats = [:html]
|
|
385
|
+
view = @controller.view_context
|
|
386
|
+
self.class.recording(Schema.new(@controller, view, only: @only), view, controller: @controller) { view.render(template: @template, layout: false) }
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
# [nodes, title], or nil when the form and its html fields say it all. The nodes are the template's
|
|
390
|
+
# content, with `{type: "form"}` standing for the response's own form; a heading above the form is
|
|
391
|
+
# the title rather than content.
|
|
392
|
+
def content_layout(state, html, index)
|
|
393
|
+
return unless state.layout?(html, index)
|
|
394
|
+
|
|
395
|
+
fragment = HtmlPolicy.parse(html.to_s.sub(state.marker(index)) { NodeRegistry.marker({type: "form"}) }) or return
|
|
396
|
+
title = heading_above_form(fragment)
|
|
397
|
+
host = @controller.request.host
|
|
398
|
+
links = ActionLinks.new(host:, base_url: @controller.request.base_url, authorizer: Authorizer.for(@controller), subject: record || @config)
|
|
399
|
+
nodes = HtmlNodes.nodes(fragment.to_html, host:, links:, frame_src: classic_url, resource: key)
|
|
400
|
+
[nodes, title] if own_form?(nodes)
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# A form that landed somewhere the walker drops (inside inline text, say) leaves nothing to show it by.
|
|
404
|
+
def own_form?(tree)
|
|
405
|
+
case tree
|
|
406
|
+
when Array then tree.any? { own_form?(it) }
|
|
407
|
+
when Hash then (tree[:type] == "form" && !tree.key?(:form)) || tree.values.any? { own_form?(it) }
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def heading_above_form(fragment)
|
|
412
|
+
above = fragment.element_children.take_while { !(NodeRegistry.marker?(it) || it.at_css(".#{NodeRegistry::CLASS_NAME}")) }
|
|
413
|
+
heading = above.find { it.name.match?(/\Ah[1-4]\z/) } or return
|
|
414
|
+
heading.text.strip.presence&.tap { heading.remove }
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
def classic_url = @controller.request.path.delete_prefix(ReactiveAdmin.config.mount_path)
|
|
418
|
+
|
|
419
|
+
# Options beyond the inline limit are searched by the action that renders the form, which has
|
|
420
|
+
# set up what the template needs; the _options endpoint has not.
|
|
421
|
+
def options_path(field)
|
|
422
|
+
action = @controller.request.path.sub(%r{[^/]+\z}, File.basename(@template))
|
|
423
|
+
"#{action}?#{OptionsPayload.page_query(@controller.request).merge(context: "template", field:).to_query}"
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
def key = AA.page?(@config) ? @config.underscored_resource_name : Registry.route_key(@config)
|
|
427
|
+
|
|
428
|
+
def default_title = AA.t(@controller.action_name, default: @controller.action_name.to_s.titleize)
|
|
429
|
+
|
|
430
|
+
def record = AA.resource_ivar(@controller)
|
|
431
|
+
|
|
432
|
+
def lock = (FormLock.for(record)&.as_json if record&.persisted?)
|
|
433
|
+
|
|
434
|
+
def display_name = (Values.display_name(record, @controller.view_context) if record&.persisted?)
|
|
435
|
+
|
|
436
|
+
def permissions
|
|
437
|
+
return {} unless AA.resource?(@config) && record&.persisted?
|
|
438
|
+
|
|
439
|
+
Permissions.new(@config, Authorizer.for(@controller)).record(record)
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Snapshot and restore for process-wide state. Only specs load this, so the production classes
|
|
3
|
+
# can't be rewound at runtime.
|
|
4
|
+
module TestSupport
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
# Collections are copied so in-place edits (`masked_attributes << ...`) are undone too.
|
|
8
|
+
def snapshot_config(config)
|
|
9
|
+
config.instance_variables.to_h { [it, copy(config.instance_variable_get(it))] }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def restore_config(config, snapshot)
|
|
13
|
+
snapshot.each { |name, value| config.instance_variable_set(name, copy(value)) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def snapshot_sources(sources) = sources.instance_variable_get(:@sources).dup
|
|
17
|
+
|
|
18
|
+
def restore_sources(sources, snapshot) = sources.instance_variable_set(:@sources, snapshot.dup)
|
|
19
|
+
|
|
20
|
+
# Bumps the version so anything cached against the overlay is recomputed.
|
|
21
|
+
def snapshot_overlay(overlay = MenuOverlay) = overlay.entries.dup
|
|
22
|
+
|
|
23
|
+
def restore_overlay(snapshot, overlay = MenuOverlay)
|
|
24
|
+
overlay.instance_variable_set(:@entries, snapshot.dup)
|
|
25
|
+
overlay.instance_variable_set(:@version, overlay.version + 1)
|
|
26
|
+
overlay.instance_variable_set(:@audit, nil)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def copy(value) = value.is_a?(Enumerable) ? value.dup : value
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Wiring for `as: :typeahead` and `source:` in the classic admin's Formtastic inputs.
|
|
3
|
+
module Typeahead
|
|
4
|
+
# Mixed into inputs: a `source:` replaces `collection:` with the source's whole (scoped) list,
|
|
5
|
+
# which keeps the classic markup identical to a hand-built datalist.
|
|
6
|
+
module SourceCollection
|
|
7
|
+
def collection
|
|
8
|
+
return super unless options[:source]
|
|
9
|
+
|
|
10
|
+
Choices.source(options[:source], template, scope: options[:scope], object: scope_object).pairs
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Inside has_many the block receives the parent form object, as the child's own is blank.
|
|
14
|
+
def scope_object = (builder.options[:parent_builder] || builder).object
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.install
|
|
18
|
+
AA.extend_resource(dsl: ResourceDSL, config: ResourceOptions)
|
|
19
|
+
AA.install_typeahead(SourceCollection)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
# Register once, in the host: `ReactiveAdmin.option_sources { source :users, -> { User.all }, label: :title }`.
|
|
25
|
+
def option_sources(&block)
|
|
26
|
+
@option_sources ||= OptionSources.new
|
|
27
|
+
@option_sources.instance_exec(&block) if block
|
|
28
|
+
@option_sources
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def diagnostics = @diagnostics ||= Diagnostics.new
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
module Ui
|
|
3
|
+
# The built client: a Vite production build whose `manifest.json` names the hashed files the shell loads.
|
|
4
|
+
# It comes from `config.ui.client_assets` if set, else the host's own build (`rake reactiveadmin:client:build`)
|
|
5
|
+
# when it has one, so a host that builds a fork keeps it, else the build packaged in the gem. Served by the
|
|
6
|
+
# host's static files or, when it has none, by `Server`.
|
|
7
|
+
module Assets
|
|
8
|
+
ENTRY = "index.html".freeze
|
|
9
|
+
TOGGLE = "src/classic/uiToggle.ts".freeze
|
|
10
|
+
Tags = Data.define(:script, :styles, :preloads)
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def root = configured || host_build || packaged || build_dir
|
|
15
|
+
|
|
16
|
+
# Where `client:build` writes: the configured directory, or public/reactiveadmin.
|
|
17
|
+
def build_dir = configured || Rails.root.join("public/reactiveadmin")
|
|
18
|
+
|
|
19
|
+
def configured
|
|
20
|
+
dir = Ui.settings.client_assets
|
|
21
|
+
dir = dir.call if dir.respond_to?(:call)
|
|
22
|
+
Pathname(dir) if dir
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def host_build = (build_dir if build_dir.join("manifest.json").exist?)
|
|
26
|
+
|
|
27
|
+
def packaged = (PackagedClient.dir if PackagedClient.usable?)
|
|
28
|
+
|
|
29
|
+
def prefix = Ui.settings.asset_prefix.to_s.chomp("/")
|
|
30
|
+
|
|
31
|
+
# Read again when the file changes, so a deploy needs no restart.
|
|
32
|
+
def manifest
|
|
33
|
+
path = root.join("manifest.json")
|
|
34
|
+
stamp = [path, path.mtime]
|
|
35
|
+
return @manifest if @stamp == stamp
|
|
36
|
+
|
|
37
|
+
@stamp = stamp
|
|
38
|
+
@manifest = JSON.parse(path.read)
|
|
39
|
+
rescue SystemCallError, JSON::ParserError
|
|
40
|
+
@stamp = @manifest = nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def ready? = !manifest&.dig(ENTRY, "file").nil?
|
|
44
|
+
|
|
45
|
+
def tags
|
|
46
|
+
manifest = self.manifest or return
|
|
47
|
+
entry = manifest.fetch(ENTRY)
|
|
48
|
+
chunks = chunks(manifest, ENTRY)
|
|
49
|
+
Tags.new(script: url(entry.fetch("file")),
|
|
50
|
+
styles: chunks.flat_map { manifest.dig(it, "css") || [] }.uniq.map { url(it) },
|
|
51
|
+
preloads: (chunks - [ENTRY]).filter_map { manifest.dig(it, "file") }.map { url(it) })
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The classic dashboard's switch: a second entry of the same build.
|
|
55
|
+
def toggle_script
|
|
56
|
+
file = manifest&.dig(TOGGLE, "file")
|
|
57
|
+
url(file) if file
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The entry and every chunk it imports statically, which load before the first render.
|
|
61
|
+
def chunks(manifest, key, seen = [])
|
|
62
|
+
return seen if seen.include?(key) || !manifest.key?(key)
|
|
63
|
+
|
|
64
|
+
seen << key
|
|
65
|
+
manifest[key].fetch("imports", []).each { chunks(manifest, it, seen) }
|
|
66
|
+
seen
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def url(file) = "#{prefix}/#{file}"
|
|
70
|
+
|
|
71
|
+
# Hashed files never change, so they cache for good; everything else revalidates.
|
|
72
|
+
class Server
|
|
73
|
+
IMMUTABLE = {"cache-control" => "public, max-age=31536000, immutable"}.freeze
|
|
74
|
+
|
|
75
|
+
# The packaged build's stamp names the exact gem version, which is nobody's business. Judged on the path as
|
|
76
|
+
# Rack::Files will read it, decoded and cleaned, so an encoded name can't slip past.
|
|
77
|
+
def call(env)
|
|
78
|
+
path = Rack::Utils.unescape_path(env["PATH_INFO"].to_s)
|
|
79
|
+
return not_found unless Rack::Utils.valid_path?(path)
|
|
80
|
+
return not_found if File.basename(Rack::Utils.clean_path_info(path)) == PackagedClient::INFO
|
|
81
|
+
|
|
82
|
+
Rack::Files.new(Assets.root.to_s, env["PATH_INFO"].start_with?("/assets/") ? IMMUTABLE : {}).call(env)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def not_found = [404, {"content-type" => "text/plain"}, ["Not Found"]]
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
module Ui
|
|
3
|
+
# Builds the React client from this repository's checkout (Vite, at deploy time; nothing runs node in
|
|
4
|
+
# production) into the directory the gem serves it from. `rake reactiveadmin:client:build`.
|
|
5
|
+
class ClientBuild
|
|
6
|
+
Error = Class.new(StandardError)
|
|
7
|
+
|
|
8
|
+
SCRIPT = "build:host".freeze
|
|
9
|
+
|
|
10
|
+
def self.source = Pathname(ENV["REACTIVEADMIN_CLIENT_SRC"] || File.expand_path("../../../../apps/standalone", __dir__))
|
|
11
|
+
|
|
12
|
+
def initialize(out: Assets.build_dir, source: self.class.source, root: Rails.root, runner: method(:run))
|
|
13
|
+
@out = Pathname(out).expand_path
|
|
14
|
+
@source = Pathname(source).expand_path
|
|
15
|
+
@root = Pathname(root).expand_path
|
|
16
|
+
@runner = runner
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
attr_reader :out, :source
|
|
20
|
+
|
|
21
|
+
def call
|
|
22
|
+
check!
|
|
23
|
+
workspace = source.parent.parent
|
|
24
|
+
@runner.call({}, %w[pnpm install --frozen-lockfile], workspace)
|
|
25
|
+
@runner.call(env, %W[pnpm --filter standalone #{SCRIPT}], workspace)
|
|
26
|
+
raise Error, "The build wrote no manifest.json to #{out}" unless out.join("manifest.json").exist?
|
|
27
|
+
|
|
28
|
+
out
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def env = {"RA_OUT_DIR" => out.to_s}
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
# The build empties its output directory, so it only ever writes inside the app.
|
|
36
|
+
def check!
|
|
37
|
+
raise Error, "No client source at #{source}. It ships with the ReactiveAdmin repository; set REACTIVEADMIN_CLIENT_SRC." unless source.join("package.json").exist?
|
|
38
|
+
raise Error, "Refusing to build into #{out}: it must be a directory inside #{@root}." unless out.to_s.start_with?("#{@root}/")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def run(env, command, chdir)
|
|
42
|
+
ok = Bundler.with_unbundled_env { system(env, *command, chdir: chdir.to_s) }
|
|
43
|
+
raise Error, "#{command.join(" ")} failed (is node >= 24 with pnpm installed?)" unless ok
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
module Ui
|
|
3
|
+
# View helper for the classic admin: `text_node reactiveadmin_ui_toggle` on an ActiveAdmin page (the
|
|
4
|
+
# dashboard), or `<%= reactiveadmin_ui_toggle %>` in a partial. Renders nothing for users who can't switch,
|
|
5
|
+
# inside the client's frames, or while no client build is installed. The build's `uiToggle` script sets the
|
|
6
|
+
# cookie, and applies `ui.default` to a browser that has none.
|
|
7
|
+
module Helper
|
|
8
|
+
def reactiveadmin_ui_toggle
|
|
9
|
+
user = ReactiveAdmin.current_user(controller)
|
|
10
|
+
script = Ui::Assets.toggle_script
|
|
11
|
+
return "".html_safe unless script && params[:embed] != "1" && Ui.available?(user)
|
|
12
|
+
|
|
13
|
+
returning = params[:classic] == "1"
|
|
14
|
+
tag.div(class: "panel reactiveadmin-ui-toggle", data: {reactiveadmin_ui: Ui.default_mode(user)}) do
|
|
15
|
+
tag.h3(returning ? "The new admin" : "Try the new admin") +
|
|
16
|
+
tag.div(class: "panel_contents") do
|
|
17
|
+
tag.p(returning ? "You're viewing a classic page on request." : "The same pages in a faster, redesigned admin. You can switch back at any time.") +
|
|
18
|
+
tag.button(returning ? "Return to the new admin" : "Try the new admin", type: "button", data: {reactiveadmin_ui_mode: "new"})
|
|
19
|
+
end
|
|
20
|
+
end + tag.script(src: script, type: "module", nonce: content_security_policy_nonce)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|