rails_admin_next 1.2.0 → 1.3.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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -4
  3. data/app/assets/stylesheets/rails_admin/skin.css +4 -0
  4. data/app/controllers/rails_admin_next/main_controller.rb +64 -7
  5. data/app/helpers/rails_admin_next/form_builder.rb +23 -4
  6. data/app/helpers/rails_admin_next/main_helper.rb +12 -1
  7. data/app/views/layouts/rails_admin_next/_head.html.erb +1 -1
  8. data/app/views/layouts/rails_admin_next/_navigation.html.erb +1 -1
  9. data/app/views/rails_admin_next/main/_form_nested_many.html.erb +9 -4
  10. data/app/views/rails_admin_next/main/_form_nested_one.html.erb +8 -3
  11. data/app/views/rails_admin_next/main/bulk_delete.html.erb +1 -1
  12. data/app/views/rails_admin_next/main/dashboard.html.erb +52 -50
  13. data/app/views/rails_admin_next/main/index.html.erb +3 -2
  14. data/docs/UPGRADING.md +160 -2
  15. data/docs/security.md +5 -0
  16. data/docs/upstream-parity.md +47 -3
  17. data/lib/rails_admin_next/abstract_model.rb +12 -4
  18. data/lib/rails_admin_next/adapters/active_record.rb +47 -3
  19. data/lib/rails_admin_next/config/actions/bulk_delete.rb +4 -4
  20. data/lib/rails_admin_next/config/actions/export.rb +2 -1
  21. data/lib/rails_admin_next/config/actions/index.rb +5 -21
  22. data/lib/rails_admin_next/config/actions/show.rb +1 -1
  23. data/lib/rails_admin_next/config/actions.rb +5 -12
  24. data/lib/rails_admin_next/config/fields/association.rb +1 -1
  25. data/lib/rails_admin_next/config/fields/types/enum.rb +8 -2
  26. data/lib/rails_admin_next/config/fields/types/serialized.rb +17 -2
  27. data/lib/rails_admin_next/config/fields/types/timestamp.rb +1 -0
  28. data/lib/rails_admin_next/config/has_fields.rb +1 -1
  29. data/lib/rails_admin_next/config.rb +4 -4
  30. data/lib/rails_admin_next/extensions/paper_trail/auditing_adapter.rb +1 -1
  31. data/lib/rails_admin_next/extensions/pundit/authorization_adapter.rb +4 -2
  32. data/lib/rails_admin_next/support/composite_keys_serializer.rb +8 -2
  33. data/lib/rails_admin_next/support/csv_converter.rb +45 -5
  34. data/lib/rails_admin_next/support/serialization_schema.rb +74 -0
  35. data/lib/rails_admin_next/version.rb +1 -1
  36. data/lib/rails_admin_next.rb +5 -16
  37. data/src/rails_admin/controllers/nested_form_controller.js +1 -1
  38. metadata +3 -2
data/docs/UPGRADING.md CHANGED
@@ -398,6 +398,19 @@ end
398
398
 
399
399
  Note the block: a bare `inverse_of nil` reads the option rather than setting it.
400
400
 
401
+ ## Nested forms that can reach themselves stop at three levels
402
+
403
+ A nested form for an association that leads back to its own model, such as a self-referential
404
+ `has_many :children` with `accepts_nested_attributes_for`, used to render without end and fail
405
+ with `SystemStackError`, as did saved records that point back at each other. It now nests the
406
+ same association at most three times along one chain; past that, the association's rows, blank
407
+ template and add control are left out of the form, so a deeper tree is edited from a lower
408
+ record. Chains of distinct associations are not capped.
409
+
410
+ The blank row's child-index placeholder is tagged by depth, `new_<depth>_<association>` (for
411
+ example `new_1_comments`), rather than `new_<association>`. Host JavaScript that looked for the
412
+ old token in a nested form's `<template>` needs the new one.
413
+
401
414
  ## The show action answers HTML by default
402
415
 
403
416
  `show` used to declare its JSON format before its HTML one, so a request carrying no
@@ -416,8 +429,149 @@ curl -H 'Accept: application/json' https://example.com/admin/player/1
416
429
  curl https://example.com/admin/player/1.json
417
430
  ```
418
431
 
419
- Note that the JSON branch renders the whole record, including columns the model config
420
- hides from the show view.
432
+ The JSON branch renders only the fields the show view displays (see below).
433
+
434
+ ## JSON and XML serialize only configured fields
435
+
436
+ The index action's `.json`/`.xml`, the show action's `.json`, and the export action's JSON/XML
437
+ used to serialize every attribute of the record, and export passed the submitted `schema` straight
438
+ to `to_json`/`to_xml` — so any `methods` or `include` name in it was called on every record.
439
+
440
+ They now serialize only what the matching section shows: `list` for index, `show` for show,
441
+ `export` for export. Index and show also keep the foreign keys of the `belongs_to` associations
442
+ they display. A submitted export `schema` is intersected with the export fields, and `except` is
443
+ ignored.
444
+
445
+ If a client relied on a column the section doesn't show, add that field to the section.
446
+
447
+ ## Malformed filter params are ignored
448
+
449
+ The index page used to answer a hand-written or stale `f[...]` query string with a 500: a
450
+ filter that wasn't the `field => index => {o:, v:}` shape the filter box submits, one naming
451
+ a field that isn't filterable (`is not currently filterable`), or a value the field couldn't
452
+ parse, such as an impossible date or malformed JSON. Such a filter is now dropped and the
453
+ rest of the list renders. A value that fails to parse counts as no value, so a bookmarked
454
+ link to a field that has since stopped being filterable lists every record rather than
455
+ erroring.
456
+
457
+ ## Pundit: a model with no policy Scope lists nothing
458
+
459
+ A model whose policy defines no `Scope` used to list every record. It now lists none: the
460
+ list, export, bulk delete and dashboard count all return an empty set. Bulk delete deletes
461
+ whatever the scope returns, with no per-record check, so "no Scope" can't mean "everything".
462
+
463
+ Define a `Scope` for each model the admin exposes:
464
+
465
+ ```ruby
466
+ class PlayerPolicy < ApplicationPolicy
467
+ class Scope < ApplicationPolicy::Scope
468
+ def resolve
469
+ scope.all
470
+ end
471
+ end
472
+ end
473
+ ```
474
+
475
+ CanCanCan hosts are unaffected.
476
+
477
+ ## An `:enum` field with no option source offers no options
478
+
479
+ An `:enum` field takes its options from `enum_method`, which defaults to `<name>_enum` on the
480
+ model class or the record. When neither defines it, the field used to call `<name>` itself and
481
+ treat the record's current value as the option list: the index filter crashed, and the form
482
+ offered that value instead of choices. It now offers no options.
483
+
484
+ If a field's own method returns its option list, name it explicitly:
485
+
486
+ ```ruby
487
+ field :colors, :enum do
488
+ enum_method :colors
489
+ end
490
+ ```
491
+
492
+ ## A redeclared field keeps its `visible` block
493
+
494
+ Declaring a field again in the same section used to discard a `visible { ... }` block set by an
495
+ earlier declaration and show the field to everyone. The block now survives:
496
+
497
+ ```ruby
498
+ field :salary do
499
+ visible { bindings[:view].current_user.admin? }
500
+ end
501
+ field :salary # still hidden from non-admins
502
+ ```
503
+
504
+ If a field you hide conditionally started showing for everyone after a later `field` call, it is
505
+ hidden again. A field hidden with a plain `visible false` is still shown again by a later
506
+ declaration, as before.
507
+
508
+ ## Serialized fields load only plain YAML
509
+
510
+ A `serialized` field (the default for a `store` or `serialize` attribute) now shows its value
511
+ as plain YAML, without `!ruby/...` class tags, and loads the submitted YAML with only `Symbol`,
512
+ `Date` and `Time` permitted. A store's `HashWithIndifferentAccess` comes back as a plain `Hash`,
513
+ which the store wraps again on assignment. YAML naming any other class is rejected.
514
+
515
+ The `safe_yaml` gem is no longer consulted, even when the host bundles it.
516
+ `RailsAdminNext.yaml_load` and `RailsAdminNext.yaml_dump` are deprecated: call
517
+ `YAML.safe_load` and `YAML.dump` directly.
518
+
519
+ ## `config.model "::Order"` configures `Order`
520
+
521
+ A model named as a string or symbol with a leading `::` used to register a second, separate
522
+ entry: its block never reached `Order`, and `Order` appeared twice in the navigation. The `::`
523
+ is now dropped, so the block applies to `Order`. If such a block was silently doing nothing,
524
+ check that what it configures is what you want before upgrading. `included_models` and
525
+ `excluded_models` drop a leading `::` the same way.
526
+
527
+ ## Scope tabs apply to export and bulk delete
528
+
529
+ The tab selected on a list (`list { scopes [...] }`) now also narrows export and bulk
530
+ delete: "Export found" streams only the tab's records, and bulk delete lists and destroys
531
+ only the tab's records, whatever ids are submitted. The bulk form and the confirmation
532
+ form carry `scope` so the tab survives the round trip.
533
+
534
+ A tabbed collection stays inside the authorization scope, so a tab that drops conditions,
535
+ such as `unscoped`, cannot list, export or delete records the user may not see. It still
536
+ drops the model's own configured `scope`. The authorization scope is built on the model's
537
+ `default_scope`, so an `unscoped` tab no longer reaches records `default_scope` hides: a
538
+ soft-delete "trash" tab built on `unscoped` comes up empty. Give such a tab a scope the
539
+ authorization relation also reaches.
540
+
541
+ The `scopes` block is now evaluated with `bindings[:controller]`, `bindings[:view]` and
542
+ `bindings[:object]`, so tabs can depend on the current user:
543
+
544
+ ```ruby
545
+ config.model "Order" do
546
+ list do
547
+ scopes { bindings[:controller].current_user.admin? ? [nil, :refunded] : [nil] }
548
+ end
549
+ end
550
+ ```
551
+
552
+ `list_entries` keeps its four positional arguments, but applies a tab only for the page's
553
+ own model, its `additional_scope` defaults to `nil`, and it no longer reads the
554
+ `associated_collection` picker params. Custom actions that listed picker records through
555
+ it should call `association_entries(model_config, collection_scope)` instead.
556
+
557
+ ## `associated_collection_scope` runs after the list sort
558
+
559
+ Association pickers apply `associated_collection_scope` after the picked model's list sort,
560
+ so a `reorder` in it now decides the order. A plain `order` adds a secondary sort key after
561
+ the list sort. Build on the relation the proc receives: returning a fresh relation discards
562
+ the authorization scope, the picker's search query, sort and eager loads as well. Pickers
563
+ never apply the picked model's scope tabs.
564
+
565
+ ## CSV export follows the list sort
566
+
567
+ CSV export used to walk records with `find_each`, which discards the list sort, so every file
568
+ came out in primary-key ascending order. It now writes rows in the order the list shows them:
569
+ the sort the export request carries, or the list's default sort. For most models that default is
570
+ the primary key descending, so an export with no sort chosen is now newest first.
571
+
572
+ Rows are read in batches of 1000 instead of loading every record up front. An export with no
573
+ matching records now contains the header row instead of an empty line.
574
+
421
575
  ## Removed: options deprecated before RailsAdmin 3.0
422
576
 
423
577
  These six were deprecated upstream between 2019-07 and 2022-01 and had no effect for
@@ -461,3 +615,7 @@ These are intentionally gone — migrate off them before upgrading:
461
615
 
462
616
  The gem owns **no** history table. Auditing remains host/PaperTrail territory, so there is
463
617
  no gem migration to run — your existing `rails_admin_histories` table (if any) is unaffected.
618
+
619
+ A record's history page now honours its column sort, and with no sort it lists newest
620
+ first, like the model history page. It used to list a record's versions oldest first
621
+ whatever the sort.
data/docs/security.md CHANGED
@@ -11,6 +11,11 @@ destructive-action controls itself; a few things are left for the host to opt in
11
11
  - **Clickjacking.** Every admin response carries `X-Frame-Options: SAMEORIGIN`.
12
12
  - **Export.** The CSV/JSON/XML data stream requires a non-GET, CSRF-protected request; `GET` only
13
13
  renders the export form, so the table can't be exfiltrated via a crafted link.
14
+ - **Serialization.** JSON and XML output is limited to the fields the section shows (`list`,
15
+ `show` or `export`). A submitted export `schema` is intersected with the export fields, so it
16
+ can't name a hidden column, an association the export doesn't show, or a method to call.
17
+ - **Pundit scopes fail closed.** A model whose policy has no `Scope` lists no records, rather
18
+ than all of them.
14
19
  - **Destructive actions.** `delete` and `bulk_delete` render a confirmation page and additionally
15
20
  carry `data-turbo-confirm` on the destructive submit (Turbo's confirmation, not `@rails/ujs`).
16
21
  - **Mass assignment.** Submitted attributes are sliced to the model config's `visible_fields`
@@ -1,8 +1,8 @@
1
1
  # Upstream parity
2
2
 
3
- Assessed through upstream `897ffc13` (2026-09-13). Fork point: `d8e0809e` (2025-09-14).
3
+ Assessed through upstream `2dfd05f1` (2026-10-30). Fork point: `d8e0809e` (2025-09-14).
4
4
 
5
- Divergence from the fork point: 787 files, +20,899 / −73,565 lines.
5
+ Divergence from the fork point: 792 files, +21,203 / −73,569 lines.
6
6
 
7
7
  ## How this fork tracks upstream
8
8
 
@@ -18,6 +18,50 @@ Commits are listed newest first, as `git log` reports them.
18
18
 
19
19
  | upstream | subject | verdict | ours |
20
20
  |---|---|---|---|
21
+ | `2dfd05f1` | Test ferrum 0.19.2 with flatten mode on JRuby | n/a — JRuby leg over Appraisal gemfiles; neither exists here | — |
22
+ | `bf8f645c` | Replace nested_form with RailsAdmin's own nested form implementation | partly relevant — per-controller `<template>` and button controls already here; unsaved-row remove, self-reference recursion, placeholder collision and the dummy belongs_to still apply | [#73](https://github.com/6temes/rails_admin_next/issues/73) |
23
+ | `6e27f4d3` | Let the adapter narrow a scope, instead of merging it here | n/a — one AR adapter; the controller's merge is already its vocabulary, no behaviour change | — |
24
+ | `20f8f29a` | Narrow a listing by the scope it selects before it is paginated | partly adapted — pre-pagination done; export and bulk delete ignore the tab, and remote pickers get it | [#60](https://github.com/6temes/rails_admin_next/issues/60) |
25
+ | `7e5a7d67` | Keep the browser driver off a version the suite hangs on | partly relevant — the pin is JRuby-only (n/a); the missing job timeout matches this fork's apt-step hangs | [#74](https://github.com/6temes/rails_admin_next/issues/74) |
26
+ | `1e0bd4fd` | Say what an upload needs from an embedded document | n/a — embedded documents are Mongoid; no upload docs here | — |
27
+ | `6651f64d` | Say which records associated_collection_scope governs | n/a — the option isn't documented here, so nothing to correct | — |
28
+ | `51d7321a` | Look up a registered visible block by the option's own name | relevant — measured: redeclaring a field drops its visible block; `admin?` raises NameError | [#64](https://github.com/6temes/rails_admin_next/issues/64) |
29
+ | `e67edd00` | Escape a field name before it stands in for a selector | n/a — the filter container is found by `getElementById`; other selectors use `CSS.escape` or quoted values | — |
30
+ | `11cd3e53` | Leave out a column the queried term could never fit | relevant — measured: PG raises NumericValueOutOfRange for a 10-digit term on int4 | [#56](https://github.com/6temes/rails_admin_next/issues/56) |
31
+ | `2d9f951f` | Have no enumeration rather than crash when there's no _enum method | relevant — measured: enum without a source crashes filter_operators; native PG enums hit it unconfigured | [#58](https://github.com/6temes/rails_admin_next/issues/58) |
32
+ | `c4b64ede` | Document sort_by's array and hash forms | n/a — sort_by isn't documented here; the forms are supported | — |
33
+ | `cbed4c42` | Fall back to the guessed encoding when a CSV export leaves it blank | n/a — measured: blank does what the form says here (leaves encoding untouched); the fallback is a no-op on the connection encoding | — |
34
+ | `8748af05` | Bind the controller and view before reading a model's list scopes | relevant — measured: both reads are unbound | [#62](https://github.com/6temes/rails_admin_next/issues/62) |
35
+ | `3e26f685` | Keep a serialized value's wrapper class out of the text an admin edits | relevant — measured: every AR `store` round-trip raises DisallowedClass | [#63](https://github.com/6temes/rails_admin_next/issues/63) |
36
+ | `8b7f686b` | Let a requested sort win over has_paper_trail's own version order | relevant — `order` appended after paper_trail's association order | [#69](https://github.com/6temes/rails_admin_next/issues/69) |
37
+ | `e9e13ebf` | Recognize a native timestamptz column as a timestamp field | relevant — measured: Rails 8.1.4 PG reports `:timestamptz`, which the registry rejects | [#55](https://github.com/6temes/rails_admin_next/issues/55) |
38
+ | `0c6b8e9d` | Version 4.0.0.beta2 | n/a — upstream release chore | — |
39
+ | `d96845da` | [Security] Fail closed when Pundit policy scope is undefined | relevant — the adapter rescued a missing Scope with `model.all`; fixed with plain AR `none` (no repository indirection needed) | [#77](https://github.com/6temes/rails_admin_next/pull/77) |
40
+ | `a6cf6bb0` | [Security] Limit JSON/XML serialization to the configured fields | partly adapted — the GET gate was already here; methods/hidden columns via `schema` and full-row index/show output fixed; the default never calls virtual fields | [#77](https://github.com/6temes/rails_admin_next/pull/77) |
41
+ | `fca081f2` | Version 4.0.0.beta | n/a — upstream release chore | — |
42
+ | `00ddcd06` | Drop the dummy app's tooling for asset modes that no longer exist | n/a — none of those files are tracked here | — |
43
+ | `d593bcaf` | Serve the dummy app's own JavaScript through every asset pipeline | n/a — one pipeline; the host entry is an ESM module importing Turbo | — |
44
+ | `72bdd18d` | Let any callable serve as asset_source | n/a — `asset_source=` is a deprecated no-op here | — |
45
+ | `812109cf` | Put the :external stylesheet where a JS build will not eat it | n/a — the installer writes only an initializer | — |
46
+ | `9ed33f69` | Keep the dummy app's test hooks out of development | n/a — the only test hook here is invisible | — |
47
+ | `7cc5e292` | Keep the dummy app's cookies readable, and cover CSRF both ways | partly relevant — the json pin is moot on Rails 8.1.4; the CSRF acceptance coverage gap applies | [#76](https://github.com/6temes/rails_admin_next/issues/76) |
48
+ | `fd0bf316` | Drop the docs left behind by the asset rework | n/a — docs here mention those pipelines only as removed | — |
49
+ | `3af6946c` | Flush rich-text editors before the modal submits | n/a — Trix writes its input on every change; no submit-time editor remains | — |
50
+ | `a16b425f` | Test carrierwave 3.x and 4.x within the existing CI matrix | n/a — CarrierWave removed | — |
51
+ | `8302cc29` | Lower a search term with String#downcase | adapted — already `downcase`; no `mb_chars` left | — |
52
+ | `63ebb9ac` | Ask whether an enum is null, not whether it equals '' | relevant — measured: PG rejects `= ''` on an integer enum | [#57](https://github.com/6temes/rails_admin_next/issues/57) |
53
+ | `749cbe75` | Let associated_collection_scope decide the order it asks for | relevant — scope applied before the reorder; the swap must follow #60 | [#61](https://github.com/6temes/rails_admin_next/issues/61) |
54
+ | `47da8e99` | Refresh SimpleMDE editors when their tab is shown | n/a — no layout-measuring editor ships here | — |
55
+ | `4443ab35` | Ship a prebuilt asset bundle; collapse config.asset_source | n/a — already a single zero-build importmap + Propshaft path | — |
56
+ | `478484fb` | CI: install ImageMagick against a refreshed package list | n/a — the step here already runs `apt-get update` and `-y`; the failure here is a stall (#74) | — |
57
+ | `a6f4c5ad` | Fix the viewport meta tag's name attribute | relevant — the identical malformed tag is in `_head.html.erb` | [#72](https://github.com/6temes/rails_admin_next/issues/72) |
58
+ | `88e6df4d` | Strip a leading :: when deriving a model's config registry key | relevant — measured: config ignored and the model listed twice | [#70](https://github.com/6temes/rails_admin_next/issues/70) |
59
+ | `a5607f24` | Use label_plural in bulk delete flash messages | relevant — measured: translated plural ignored | [#71](https://github.com/6temes/rails_admin_next/issues/71) |
60
+ | `7b60193b` | Let the page catch up before reading the database | adapted — fixed independently here | [#23](https://github.com/6temes/rails_admin_next/pull/23) |
61
+ | `ef3e3f57` | Keep one dead browser from failing the examples after it | partly relevant — the driver restore applies; the startup-timeout half is deferred (never seen in ~60 runs) | [#75](https://github.com/6temes/rails_admin_next/issues/75) |
62
+ | `cc293dea` | Wait for the autocomplete menu itself, not for one of its entries | n/a — waits here use counts or text the stale menu can't satisfy; the widget renders once per query (as `77e5d0bc`) | — |
63
+ | `d3cfba1d` | Render the list for a filter URL written by hand | relevant — measured: seven malformed shapes 500, one specific to this fork | [#65](https://github.com/6temes/rails_admin_next/issues/65) |
64
+ | `ede4e77b` | Split the adapter layer into a Reflection and a Repository | declined — multi-ORM refactor, one adapter here; four embedded fixes filed separately | [#59](https://github.com/6temes/rails_admin_next/issues/59), [#66](https://github.com/6temes/rails_admin_next/issues/66), [#67](https://github.com/6temes/rails_admin_next/issues/67), [#68](https://github.com/6temes/rails_admin_next/issues/68) |
21
65
  | `897ffc13` | Fix _discard leaking into SQL for multi-select enum filters | adapted — server half only; the client half was already stronger here (disabled options), so upstream's JS was deliberately not ported | [#36](https://github.com/6temes/rails_admin_next/pull/36) |
22
66
  | `11f214f6` | Remove config options deprecated before RailsAdmin 3.0 | adapted — the deprecator half already shipped; this removed the six options | [#40](https://github.com/6temes/rails_admin_next/pull/40) |
23
67
  | `cbea80b1` | Match GET in http_methods case-insensitively | adapted — `Actions::Base#linkable?` | [#38](https://github.com/6temes/rails_admin_next/pull/38) |
@@ -31,7 +75,7 @@ Commits are listed newest first, as `git log` reports them.
31
75
  | `2eed28e1` | Fully specify all ESM imports with an extension | n/a — measured: every relative import in `src/` already carries `.js`; browser-native ESM requires it | — |
32
76
  | `e8dec57a` | Upgrade @hotwired/turbo-rails to resolve security warning | n/a — turbo comes from the `turbo-rails` gem (2.0.23), which Dependabot reads | — |
33
77
  | `07b2066a` | Replace `asset-url` with `url` in Sass files | n/a — no Sass; one hand-owned CSS file | — |
34
- | `a0946615` | Drop support for Rails 6.x and Ruby 2.5/2.6 | n/a — this fork is Rails ~> 8.1 / Ruby >= 4.0.5 only | — |
78
+ | `a0946615` | Drop support for Rails 6.x and Ruby 2.5/2.6 | n/a — this fork is Rails ~> 8.1 / Ruby >= 4.0.9 only | — |
35
79
  | `0543cb3f` | Revert npm package.json version ahead of actual publish | n/a — no npm package | — |
36
80
  | `cf7b112e` | Run Prettier on migrated docs | n/a — upstream's vendored wiki | — |
37
81
  | `1c2b92a3` | Rewrite wiki cross-links to relative docs/ links | n/a — upstream's vendored wiki | — |
@@ -118,8 +118,16 @@ module RailsAdminNext
118
118
  extend Adapters::ActiveRecord
119
119
  end
120
120
 
121
+ # A value the field can't parse (an impossible date, malformed JSON or YAML) is
122
+ # treated as no value, element by element in an array. Only parse errors are
123
+ # rescued, so a bug in a custom parse_value still raises. Date::Error is an
124
+ # ArgumentError.
121
125
  def parse_field_value(field, value)
122
- value.is_a?(Array) ? value.map { |v| field.parse_value(v) } : field.parse_value(value)
126
+ return value.map { |v| parse_field_value(field, v) } if value.is_a?(Array)
127
+
128
+ field.parse_value(value)
129
+ rescue ArgumentError, JSON::ParserError, Psych::Exception
130
+ nil
123
131
  end
124
132
 
125
133
  class StatementBuilder
@@ -242,7 +250,7 @@ module RailsAdminNext
242
250
  end
243
251
 
244
252
  def today
245
- [Date.today, Date.today]
253
+ [Date.current, Date.current]
246
254
  end
247
255
 
248
256
  def yesterday
@@ -250,7 +258,7 @@ module RailsAdminNext
250
258
  end
251
259
 
252
260
  def this_week
253
- [Date.today.beginning_of_week, Date.today.end_of_week]
261
+ [Date.current.beginning_of_week, Date.current.end_of_week]
254
262
  end
255
263
 
256
264
  def last_week
@@ -259,7 +267,7 @@ module RailsAdminNext
259
267
  end
260
268
 
261
269
  def between
262
- [@value[1], @value[2]]
270
+ [Array.wrap(@value)[1], Array.wrap(@value)[2]]
263
271
  end
264
272
 
265
273
  def default
@@ -182,7 +182,10 @@ module RailsAdminNext
182
182
 
183
183
  def add(field, value, operator)
184
184
  field.searchable_columns.flatten.each do |column_infos|
185
- statement, value1, value2 = StatementBuilder.new(quote(column_infos[:column]), column_infos[:type], value, operator, @scope.connection.adapter_name).to_statement
185
+ attribute_type = attribute_type(field, column_infos[:column])
186
+ statement, value1, value2 = StatementBuilder.new(quote(column_infos[:column]), column_infos[:type], value, operator, @scope.connection.adapter_name, column_type: attribute_type&.type).to_statement
187
+ statement, value1, value2 = "(1 = 0)" if out_of_range?(attribute_type, [value1, value2])
188
+
186
189
  @statements << statement if statement.present?
187
190
  @values << value1 unless value1.nil?
188
191
  @values << value2 unless value2.nil?
@@ -202,6 +205,29 @@ module RailsAdminNext
202
205
  "#{@scope.connection.quote_table_name(table)}.#{@scope.connection.quote_column_name(name)}"
203
206
  end
204
207
 
208
+ # The column's own attribute type, resolved from the field's model or, for an
209
+ # association, the associated model. Nil for a column on any other table.
210
+ def attribute_type(field, column)
211
+ table, name = column.split(".")
212
+ return unless name
213
+
214
+ models = [field.abstract_model]
215
+ models.concat(Array.wrap(field.associated_model_config).map(&:abstract_model)) if field.association?
216
+ models.map(&:model).detect { it.table_name == table }&.type_for_attribute(name)
217
+ end
218
+
219
+ # A number too wide for the column (an int4 searched with a 10-digit term) makes
220
+ # PostgreSQL reject the whole query. No row can hold it, so the column matches
221
+ # nothing instead: a search still matches the other columns, a filter matches none.
222
+ def out_of_range?(attribute_type, values)
223
+ return false unless attribute_type
224
+
225
+ values.grep(Integer).each { attribute_type.serialize(it) }
226
+ false
227
+ rescue ActiveModel::RangeError
228
+ true
229
+ end
230
+
205
231
  def build
206
232
  scope = @scope.where(@statements.join(" OR "), *@values)
207
233
  scope = scope.references(*@tables.uniq) if @tables.any?
@@ -225,11 +251,20 @@ module RailsAdminNext
225
251
 
226
252
  # filters example => {"string_field"=>{"0055"=>{"o"=>"like", "v"=>"test_value"}}, ...}
227
253
  # "0055" is the filter index, no use here. o is the operator, v the value
254
+ # Hand-written or stale params can have any shape, so anything that isn't
255
+ # field => index => filter with a scalar (or flat array) value, or names no
256
+ # filterable field, is skipped.
228
257
  def filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?))
258
+ return scope unless filters.respond_to?(:each_pair)
259
+
229
260
  filters.each_pair do |field_name, filters_dump|
261
+ field = fields.detect { |f| f.name.to_s == field_name }
262
+ next unless field && filters_dump.respond_to?(:each_value)
263
+
230
264
  filters_dump.each_value do |filter_dump|
265
+ next unless filter_dump.respond_to?(:each_pair) && filter_value?(filter_dump[:v])
266
+
231
267
  wb = WhereBuilder.new(scope)
232
- field = fields.detect { |f| f.name.to_s == field_name }
233
268
  value = parse_field_value(field, filter_dump[:v])
234
269
 
235
270
  wb.add(field, value, filter_dump[:o] || RailsAdminNext::Config.default_search_operator)
@@ -240,14 +275,19 @@ module RailsAdminNext
240
275
  scope
241
276
  end
242
277
 
278
+ def filter_value?(value)
279
+ Array.wrap(value).none? { it.is_a?(Array) || it.respond_to?(:each_pair) }
280
+ end
281
+
243
282
  def build_statement(column, type, value, operator)
244
283
  StatementBuilder.new(column, type, value, operator, model.connection.adapter_name).to_statement
245
284
  end
246
285
 
247
286
  class StatementBuilder < RailsAdminNext::AbstractModel::StatementBuilder
248
- def initialize(column, type, value, operator, adapter_name)
287
+ def initialize(column, type, value, operator, adapter_name, column_type: nil)
249
288
  super(column, type, value, operator)
250
289
  @adapter_name = adapter_name
290
+ @column_type = column_type
251
291
  end
252
292
 
253
293
  protected
@@ -260,6 +300,9 @@ module RailsAdminNext
260
300
  uuid_unary_operators
261
301
  when :integer, :decimal, :float
262
302
  numeric_unary_operators
303
+ when :enum
304
+ # Only a string column can hold '' (and an integer one rejects the comparison).
305
+ %i[string text citext].include?(@column_type) ? generic_unary_operators : enum_unary_operators
263
306
  else
264
307
  generic_unary_operators
265
308
  end
@@ -288,6 +331,7 @@ module RailsAdminNext
288
331
  end
289
332
  alias_method :numeric_unary_operators, :boolean_unary_operators
290
333
  alias_method :uuid_unary_operators, :boolean_unary_operators
334
+ alias_method :enum_unary_operators, :boolean_unary_operators
291
335
 
292
336
  def range_filter(min, max)
293
337
  if min && max && min == max
@@ -20,7 +20,7 @@ module RailsAdminNext
20
20
  @objects = list_entries(@model_config, :destroy)
21
21
 
22
22
  if @objects.blank?
23
- flash[:error] = t("admin.flash.error", name: pluralize(0, @model_config.label), action: t("admin.actions.delete.done"))
23
+ flash[:error] = t("admin.flash.error", name: pluralize(0, @model_config.label, plural: @model_config.label_plural), action: t("admin.actions.delete.done"))
24
24
  redirect_to index_path
25
25
  else
26
26
  render @action.template_name
@@ -49,10 +49,10 @@ module RailsAdminNext
49
49
  end
50
50
 
51
51
  if destroyed.nil?
52
- flash[:error] = t("admin.flash.error", name: pluralize(0, @model_config.label), action: t("admin.actions.delete.done"))
52
+ flash[:error] = t("admin.flash.error", name: pluralize(0, @model_config.label, plural: @model_config.label_plural), action: t("admin.actions.delete.done"))
53
53
  else
54
- flash[:success] = t("admin.flash.successful", name: pluralize(destroyed.count, @model_config.label), action: t("admin.actions.delete.done")) unless destroyed.empty?
55
- flash[:error] = t("admin.flash.error", name: pluralize(not_destroyed.count, @model_config.label), action: t("admin.actions.delete.done")) unless not_destroyed.empty?
54
+ flash[:success] = t("admin.flash.successful", name: pluralize(destroyed.count, @model_config.label, plural: @model_config.label_plural), action: t("admin.actions.delete.done")) unless destroyed.empty?
55
+ flash[:error] = t("admin.flash.error", name: pluralize(not_destroyed.count, @model_config.label, plural: @model_config.label_plural), action: t("admin.actions.delete.done")) unless not_destroyed.empty?
56
56
  end
57
57
  redirect_to back_or_index
58
58
  end
@@ -22,7 +22,8 @@ module RailsAdminNext
22
22
  # table with attacker-chosen columns; the export form posts back to this same action.
23
23
  if format && !request.get?
24
24
  request.format = format
25
- @schema = HashHelper.symbolize(params[:schema].slice(:except, :include, :methods, :only).permit!.to_h) if params[:schema] # to_json and to_xml expect symbols for keys AND values.
25
+ serialization_schema # built while @action is still export
26
+ @schema = serialization_schema.sanitize(params[:schema].slice(:include, :methods, :only).permit!.to_h) if params[:schema]
26
27
  @objects = list_entries(@model_config, :export)
27
28
  index
28
29
  else
@@ -30,26 +30,10 @@ module RailsAdminNext
30
30
 
31
31
  register_instance_option :controller do
32
32
  proc do
33
- # Resolve the active list scope (the scope tabs) and fold it into the
34
- # relation *before* pagination. The paginated collection is a resolved
35
- # page, not a chainable relation, so a named scope can no longer be
36
- # appended afterwards.
37
33
  unless @objects
38
- scopes = @model_config.list.scopes
39
- list_scope =
40
- if scopes.empty? || params[:scope].blank?
41
- scopes.first
42
- elsif scopes.collect(&:to_s).include?(params[:scope])
43
- params[:scope].to_sym
44
- end
45
-
46
- additional_scope = get_association_scope_from_params
47
- if list_scope
48
- base_scope = additional_scope
49
- additional_scope = proc { (base_scope ? instance_eval(&base_scope) : self).send(list_scope) }
50
- end
51
-
52
- @objects = list_entries(@model_config, :index, additional_scope)
34
+ # @association is set only when the picker guard accepts associated_collection; otherwise list normally.
35
+ association_scope = get_association_scope_from_params
36
+ @objects = @association ? association_entries(@model_config, association_scope) : list_entries
53
37
  end
54
38
 
55
39
  respond_to do |format|
@@ -66,7 +50,7 @@ module RailsAdminNext
66
50
  @objects.collect { |object| {id: object.id.to_s, label: object.send(@model_config.object_label_method).to_s} }
67
51
  end
68
52
  else
69
- @objects.to_json(@schema)
53
+ @objects.to_json(@schema || serialization_schema.default)
70
54
  end
71
55
 
72
56
  if params[:send_data]
@@ -77,7 +61,7 @@ module RailsAdminNext
77
61
  end
78
62
 
79
63
  format.xml do
80
- output = @objects.map { |object| object.serializable_hash(@schema || {}) }.to_xml(root: @abstract_model.model.model_name.plural)
64
+ output = @objects.map { |object| object.serializable_hash(@schema || serialization_schema.default) }.to_xml(root: @abstract_model.model.model_name.plural)
81
65
  if params[:send_data]
82
66
  send_data output, filename: "#{params[:model_name]}_#{DateTime.now.strftime("%Y-%m-%d_%Hh%Mm%S")}.xml"
83
67
  else
@@ -24,7 +24,7 @@ module RailsAdminNext
24
24
  # registered type, so Rails falls back to whichever is declared first.
25
25
  respond_to do |format|
26
26
  format.html { render @action.template_name }
27
- format.json { render json: @object }
27
+ format.json { render json: @object.to_json(serialization_schema.default) }
28
28
  end
29
29
  end
30
30
  end
@@ -49,12 +49,8 @@ module RailsAdminNext
49
49
 
50
50
  def add_action(key, parent_class, parent, &)
51
51
  a = "RailsAdminNext::Config::Actions::#{parent_class.to_s.camelize}".constantize.new
52
- a.instance_eval(%(
53
- #{parent} true
54
- def key
55
- :#{key}
56
- end
57
- ), __FILE__, __LINE__ - 5)
52
+ a.send(parent, true)
53
+ a.define_singleton_method(:key) { key.to_sym }
58
54
  add_action_custom_key(a, &)
59
55
  end
60
56
 
@@ -68,12 +64,9 @@ module RailsAdminNext
68
64
  name = klass.to_s.demodulize.underscore.to_sym
69
65
  end
70
66
 
71
- instance_eval %{
72
- def #{name}(&block)
73
- action = #{klass}.new
74
- add_action_custom_key(action, &block)
75
- end
76
- }, __FILE__, __LINE__ - 5
67
+ define_singleton_method(name) do |&block|
68
+ add_action_custom_key(klass.new, &block)
69
+ end
77
70
  end
78
71
 
79
72
  private
@@ -137,7 +137,7 @@ module RailsAdminNext
137
137
 
138
138
  # Returns collection of all selectable records
139
139
  def collection(scope = nil)
140
- (scope || bindings[:controller].list_entries(associated_model_config, :index, associated_collection_scope, false))
140
+ (scope || bindings[:controller].association_entries(associated_model_config, associated_collection_scope))
141
141
  .map { |o| [o.send(associated_object_label_method), format_key(o.send(associated_primary_key)).to_s] }
142
142
  end
143
143
 
@@ -21,11 +21,17 @@ module RailsAdminNext
21
21
  end
22
22
 
23
23
  register_instance_option :enum_method do
24
- @enum_method ||= (bindings[:object].class.respond_to?("#{name}_enum") || (bindings[:object] || abstract_model.model.new).respond_to?("#{name}_enum")) ? "#{name}_enum" : name
24
+ @enum_method ||= begin
25
+ method_name = "#{name}_enum"
26
+ method_name if (bindings[:object]&.class || abstract_model.model).respond_to?(method_name) ||
27
+ (bindings[:object] || abstract_model.model.new).respond_to?(method_name)
28
+ end
25
29
  end
26
30
 
27
31
  register_instance_option :enum do
28
- if abstract_model.model.respond_to?(enum_method)
32
+ if enum_method.nil?
33
+ []
34
+ elsif abstract_model.model.respond_to?(enum_method)
29
35
  abstract_model.model.send(enum_method)
30
36
  else
31
37
  (bindings[:object] || abstract_model.model.new).send(enum_method)
@@ -10,17 +10,32 @@ module RailsAdminNext
10
10
  # Register field type for the type loader
11
11
  RailsAdminNext::Config::Fields::Types.register(self)
12
12
 
13
+ # The YAML is admin-supplied, so loading permits no class beyond the scalars a
14
+ # plain value needs. Dumping strips Hash/Array subclasses (a store's
15
+ # HashWithIndifferentAccess) so what the form shows can be loaded back.
16
+ PERMITTED_CLASSES = [::Date, ::Symbol, ::Time].freeze
17
+
13
18
  register_instance_option :formatted_value do
14
- RailsAdminNext.yaml_dump(value) unless value.nil?
19
+ YAML.dump(plain(value)) unless value.nil?
15
20
  end
16
21
 
17
22
  def parse_value(value)
18
- value.present? ? (RailsAdminNext.yaml_load(value) || nil) : nil
23
+ value.present? ? (YAML.safe_load(value, permitted_classes: PERMITTED_CLASSES) || nil) : nil
19
24
  end
20
25
 
21
26
  def parse_input(params)
22
27
  params[name] = parse_value(params[name]) if params[name].is_a?(::String)
23
28
  end
29
+
30
+ private
31
+
32
+ def plain(object)
33
+ case object
34
+ when Hash then object.to_h { |key, value| [plain(key), plain(value)] }
35
+ when Array then object.map { plain(it) }
36
+ else object
37
+ end
38
+ end
24
39
  end
25
40
  end
26
41
  end
@@ -9,6 +9,7 @@ module RailsAdminNext
9
9
  class Timestamp < RailsAdminNext::Config::Fields::Types::Datetime
10
10
  # Register field type for the type loader
11
11
  RailsAdminNext::Config::Fields::Types.register(self)
12
+ RailsAdminNext::Config::Fields::Types.register(:timestamptz, self)
12
13
  end
13
14
  end
14
15
  end
@@ -10,7 +10,7 @@ module RailsAdminNext
10
10
 
11
11
  # some fields are hidden by default (belongs_to keys, has_many associations in list views.)
12
12
  # unhide them if config specifically defines them
13
- field.show if field && !field.instance_variable_get("@#{field.name}_registered").is_a?(Proc)
13
+ field.show if field && !field.instance_variable_get(:@visible_registered).is_a?(Proc)
14
14
  # Specify field as virtual if type is not specifically set and field was not
15
15
  # found in default stack
16
16
  if field.nil? && type.nil?