madmin 3.3.0 → 3.4.1

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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -1
  3. data/app/controllers/madmin/application_controller.rb +2 -0
  4. data/app/controllers/madmin/base_controller.rb +2 -0
  5. data/app/controllers/madmin/dashboard_controller.rb +2 -0
  6. data/app/controllers/madmin/resource_controller.rb +6 -2
  7. data/app/helpers/madmin/application_helper.rb +2 -0
  8. data/app/helpers/madmin/filters_helper.rb +2 -0
  9. data/app/helpers/madmin/nav_helper.rb +2 -0
  10. data/app/helpers/madmin/sort_helper.rb +14 -1
  11. data/app/views/madmin/application/_pagination.html.erb +4 -2
  12. data/app/views/madmin/application/show.html.erb +1 -1
  13. data/app/views/madmin/fields/attachment/_index.html.erb +4 -4
  14. data/app/views/madmin/fields/attachment/_show.html.erb +6 -6
  15. data/config/locales/el.yml +1 -0
  16. data/config/locales/en.yml +1 -0
  17. data/config/locales/fr.yml +1 -0
  18. data/config/locales/ko.yml +1 -0
  19. data/lib/generators/madmin/field/field_generator.rb +2 -0
  20. data/lib/generators/madmin/install/install_generator.rb +2 -0
  21. data/lib/generators/madmin/resource/resource_generator.rb +2 -0
  22. data/lib/generators/madmin/resource/templates/resource.rb.tt +3 -0
  23. data/lib/generators/madmin/views/edit_generator.rb +2 -0
  24. data/lib/generators/madmin/views/form_generator.rb +2 -0
  25. data/lib/generators/madmin/views/index_generator.rb +2 -0
  26. data/lib/generators/madmin/views/javascript_generator.rb +2 -0
  27. data/lib/generators/madmin/views/layout_generator.rb +2 -0
  28. data/lib/generators/madmin/views/navigation_generator.rb +2 -0
  29. data/lib/generators/madmin/views/new_generator.rb +2 -0
  30. data/lib/generators/madmin/views/show_generator.rb +2 -0
  31. data/lib/generators/madmin/views/views_generator.rb +2 -0
  32. data/lib/madmin/countless_page.rb +50 -0
  33. data/lib/madmin/engine.rb +2 -0
  34. data/lib/madmin/field.rb +2 -0
  35. data/lib/madmin/fields/array.rb +2 -0
  36. data/lib/madmin/fields/attachment.rb +9 -5
  37. data/lib/madmin/fields/attachments.rb +2 -0
  38. data/lib/madmin/fields/belongs_to.rb +2 -0
  39. data/lib/madmin/fields/boolean.rb +2 -0
  40. data/lib/madmin/fields/currency.rb +2 -0
  41. data/lib/madmin/fields/date.rb +2 -0
  42. data/lib/madmin/fields/date_time.rb +2 -0
  43. data/lib/madmin/fields/decimal.rb +2 -0
  44. data/lib/madmin/fields/enum.rb +2 -0
  45. data/lib/madmin/fields/file.rb +2 -0
  46. data/lib/madmin/fields/float.rb +2 -0
  47. data/lib/madmin/fields/has_many.rb +3 -1
  48. data/lib/madmin/fields/has_one.rb +2 -0
  49. data/lib/madmin/fields/integer.rb +2 -0
  50. data/lib/madmin/fields/json.rb +2 -0
  51. data/lib/madmin/fields/nested_has_many.rb +2 -0
  52. data/lib/madmin/fields/nested_has_one.rb +2 -0
  53. data/lib/madmin/fields/password.rb +2 -0
  54. data/lib/madmin/fields/polymorphic.rb +2 -0
  55. data/lib/madmin/fields/rich_text.rb +2 -0
  56. data/lib/madmin/fields/select.rb +2 -0
  57. data/lib/madmin/fields/string.rb +2 -0
  58. data/lib/madmin/fields/text.rb +2 -0
  59. data/lib/madmin/fields/time.rb +2 -0
  60. data/lib/madmin/filter.rb +8 -6
  61. data/lib/madmin/generator_helpers.rb +2 -0
  62. data/lib/madmin/member_action.rb +2 -0
  63. data/lib/madmin/menu.rb +44 -8
  64. data/lib/madmin/namespace.rb +2 -0
  65. data/lib/madmin/page.rb +2 -0
  66. data/lib/madmin/pagination.rb +20 -4
  67. data/lib/madmin/resource.rb +53 -9
  68. data/lib/madmin/resource_builder.rb +2 -0
  69. data/lib/madmin/search.rb +5 -0
  70. data/lib/madmin/version.rb +3 -1
  71. data/lib/madmin/view_generator.rb +2 -0
  72. data/lib/madmin.rb +33 -9
  73. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 565609ba87bcf66877e39fc115b8f6fedff6337a3dbc45ca2ea8b045ee52902e
4
- data.tar.gz: eaf2ef17153d0b50b61ffa19922b34c6f2d810cc28d2b899cc0f45365f4ce501
3
+ metadata.gz: bd82191abecdc838bfce22f4ce0ee425c76bb90e4b2d7dbc84c9ef67300e87ad
4
+ data.tar.gz: 021edea5d8d473665fc7e7a8d4f7329fbf538d80bc7dbb8ba3ea501d63522888
5
5
  SHA512:
6
- metadata.gz: f9ef7c90457d5dda3297aa9343273e15d354742538da9d6166a6921d2bd80646ed4ebd388e34fc2cc381e4c5e05fd3f98991ae49aba7eec02668c9e44c2dc823
7
- data.tar.gz: ea8d222e6ab24dc84a49506d764286ec1636db07ece7cbf7fe965f3b7f7427184063d69af321225c25123453f687914bb799a26a6fd2448ce5ca2d9c015b9a1b
6
+ metadata.gz: aca73d8097279751ad80464a78b5622a0e561b0d0c092f3ad87ef1fc76aa14d00b77585e037777b2a822d756551c893045856d7140a50f388e659b9f2d6ef4b1
7
+ data.tar.gz: b2f109c4bdae4bdb28f69b84bf2881bebbbcaf70ebe33ee3b4142b280ae83df81fc3c22e0bbb3b3788d80b7c00c308bcd00b0bf38895c85bd7658cf6b16c3580
data/README.md CHANGED
@@ -73,7 +73,21 @@ class AuditLogResource < Madmin::Resource
73
73
  end
74
74
  ```
75
75
 
76
- The new, edit, and delete links are hidden for read-only resources, and any write actions redirect back to the index.
76
+ The new, edit, and delete links are hidden for read-only resources, and any write actions redirect back to the referring page. An attachment belongs to its record, so writes to it are refused when either resource is read-only.
77
+
78
+ ### Large Tables
79
+
80
+ Index pages count the records to show page numbers and a total. On tables with tens of millions of rows that count alone can take long enough that the page never loads. Override `countless?` to skip it:
81
+
82
+ ```ruby
83
+ class SubmissionEventResource < Madmin::Resource
84
+ def self.countless?
85
+ true
86
+ end
87
+ end
88
+ ```
89
+
90
+ The index and has many listings for that resource then show Previous and Next links without page numbers or a total. Madmin loads one record more than the page size to tell whether there is a next page. Pairing this with `sortable_columns` limited to indexed columns keeps sorting cheap too.
77
91
 
78
92
  ### Scopes
79
93
 
@@ -340,6 +354,17 @@ end
340
354
 
341
355
  Hooks registered after a class has already loaded run immediately, so ordering in your initializers doesn't matter. The controllers live in the engine's `app/` directory and are reloadable, which means their hooks re-run on every reload in development — keep the blocks idempotent.
342
356
 
357
+ ## Ractors
358
+
359
+ Rails is working toward serving requests from Ractors, and Madmin keeps up: its settings are readable from any Ractor, nothing memoized is written during a request, and rendering the menu changes nothing shared. Once Rails' experimental `Rails.application.ractorize!` has run, make Madmin's state shareable too:
360
+
361
+ ```ruby
362
+ Rails.application.ractorize!
363
+ Madmin.ractorize!
364
+ ```
365
+
366
+ This freezes the settings, the menu and every resource's attributes and actions. Member and collection action blocks are shared as well, as is the menu's `before_render` block on Ruby 4.0, which works as long as they don't capture something unshareable like a mutable local variable, and raises `Ractor::IsolationError` for one that does. Earlier Rubies can't share a `before_render` block at all, since `Ractor.shareable_proc` is what detaches it from the initializer it was written in. `Madmin.importmap` is left to importmap-rails, as `Rails.application.importmap` is.
367
+
343
368
  ## Authentication
344
369
 
345
370
  You can use a couple of strategies to authenticate users who are trying to
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class ApplicationController < Madmin::BaseController
3
5
  include Rails.application.routes.url_helpers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class BaseController < ActionController::Base
3
5
  include ::ActiveStorage::SetCurrent if defined?(::ActiveStorage)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class DashboardController < Madmin::ApplicationController
3
5
  def show
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class ResourceController < Madmin::ApplicationController
3
5
  include SortHelper
@@ -91,7 +93,7 @@ module Madmin
91
93
  # Returns [page, records]. Override for non-ActiveRecord collections and
92
94
  # return an object responding to the Madmin::Page interface.
93
95
  def paginate_collection(collection)
94
- paginate(collection, page: params[:page], per_page: Madmin::Page.per_page_for(params[:per_page]))
96
+ paginate(collection, page: params[:page], per_page: Madmin::Page.per_page_for(params[:per_page]), countless: resource.countless?)
95
97
  end
96
98
 
97
99
  def valid_scope
@@ -145,7 +147,9 @@ module Madmin
145
147
  helper_method :filters
146
148
 
147
149
  def enforce_readonly
148
- redirect_to resource.index_path, alert: t("madmin.flash.readonly", name: resource.friendly_name) if resource.readonly?
150
+ if resource.readonly_for?(@record)
151
+ redirect_back_or_to resource.index_path, alert: t("madmin.flash.readonly", name: resource.friendly_name), status: :see_other
152
+ end
149
153
  end
150
154
 
151
155
  ActiveSupport.run_load_hooks(:madmin_resource_controller, self)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module ApplicationHelper
3
5
  include Rails.application.routes.url_helpers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module FiltersHelper
3
5
  FILTER_INPUT_TYPES = {string: "text", number: "number", date: "date", datetime: "datetime-local", boolean: "text", array: "text"}.freeze
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin::NavHelper
2
4
  def nav_link_to(name = nil, options = {}, html_options = {}, &block)
3
5
  if block
@@ -1,6 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module SortHelper
5
+ # sort_column ignores a sort outside sortable_columns, so a header for one
6
+ # of those gets a plain title rather than a link that would silently re-sort
7
+ # by the default column
3
8
  def sortable(column, title, options = {})
9
+ return title unless sortable_column?(column)
10
+
4
11
  matching_column = (column.to_s == sort_column)
5
12
  direction = (sort_direction == "asc") ? "desc" : "asc"
6
13
 
@@ -14,7 +21,7 @@ module Madmin
14
21
  end
15
22
 
16
23
  def sort_column
17
- resource.sortable_columns.include?(params[:sort]) ? params[:sort] : default_sort_column
24
+ sortable_column?(params[:sort]) ? params[:sort] : default_sort_column
18
25
  end
19
26
 
20
27
  def sort_direction
@@ -44,5 +51,11 @@ module Madmin
44
51
  </svg>
45
52
  SVG
46
53
  end
54
+
55
+ private
56
+
57
+ def sortable_column?(column)
58
+ resource.sortable_columns.include?(column.to_s)
59
+ end
47
60
  end
48
61
  end
@@ -1,5 +1,5 @@
1
1
  <div class="pagination">
2
- <% if page.last > 1 %>
2
+ <% if page.prev || page.next %>
3
3
  <nav class="pages" aria-label="<%= t("madmin.pagination.label") %>">
4
4
  <%= link_to_if page.prev, "<", page.prev && current_path_with(page.param => page.prev), aria: { label: t("madmin.pagination.previous") } do %>
5
5
  <a role="link" aria-disabled="true" aria-label="<%= t("madmin.pagination.previous") %>">&lt;</a>
@@ -24,8 +24,10 @@
24
24
  <span class="pagination-info">
25
25
  <% if page.to.zero? %>
26
26
  <%= t("madmin.pagination.empty") %>
27
- <% else %>
27
+ <% elsif page.count %>
28
28
  <%= t("madmin.pagination.info", from: page.from, to: page.to, count: page.count) %>
29
+ <% else %>
30
+ <%= t("madmin.pagination.range", from: page.from, to: page.to) %>
29
31
  <% end %>
30
32
  </span>
31
33
 
@@ -13,7 +13,7 @@
13
13
  <% end %>
14
14
  <% unless resource.readonly? %>
15
15
  <%= link_to t("madmin.actions.edit"), resource.edit_path(@record), class: "btn btn-secondary" %>
16
- <%= button_to t("madmin.actions.delete"), resource.show_path(@record), method: :delete, data: { turbo_confirm: t("madmin.confirmations.delete", resource: resource.friendly_name, name: resource.display_name(@record)) }, class: "btn btn-danger" %>
16
+ <%= button_to t("madmin.actions.delete"), resource.show_path(@record), method: :delete, data: { turbo_confirm: resource.delete_confirmation(@record) }, class: "btn btn-danger" %>
17
17
  <% end %>
18
18
  </div>
19
19
  </header>
@@ -1,7 +1,7 @@
1
- <% if (attachment = field.value(record)) && attachment.attached? %>
2
- <% if attachment.variable? %>
3
- <%= image_tag main_app.url_for(attachment), class: "max-h-8" %>
1
+ <% if (attached = field.value(record)) && attached.attached? %>
2
+ <% if attached.variable? %>
3
+ <%= image_tag main_app.url_for(attached), class: "max-h-8" %>
4
4
  <% else %>
5
- <%= attachment.filename %>
5
+ <%= attached.filename %>
6
6
  <% end %>
7
7
  <% end %>
@@ -1,11 +1,11 @@
1
- <% if (attachment = field.value(record)) && attachment.attached? %>
2
- <% if attachment.variable? %>
3
- <%= image_tag main_app.url_for(attachment), class: "max-h-32" %>
1
+ <% if (attached = field.value(record)) && attached.attached? %>
2
+ <% if attached.variable? %>
3
+ <%= image_tag main_app.url_for(attached), class: "max-h-32" %>
4
4
  <% else %>
5
- <%= link_to attachment.filename, main_app.url_for(attachment), target: :_blank, class: "text-blue-500 underline" %>
5
+ <%= link_to attached.filename, main_app.url_for(attached), target: :_blank, class: "text-blue-500 underline" %>
6
6
  <% end %>
7
7
 
8
- <% if (remove_path = field.remove_path(attachment)) %>
9
- <%= link_to t("madmin.actions.remove"), remove_path, data: { turbo_method: :delete, turbo_confirm: t("madmin.confirmations.remove_with_changes", name: attachment.filename.to_s) } %>
8
+ <% if (remove_path = field.remove_path(attached)) %>
9
+ <%= link_to t("madmin.actions.remove"), remove_path, data: { turbo_method: :delete, turbo_confirm: t("madmin.confirmations.remove_with_changes", name: attached.filename.to_s) } %>
10
10
  <% end %>
11
11
  <% end %>
@@ -47,6 +47,7 @@ el:
47
47
  previous: Προηγούμενη
48
48
  next: Επόμενη
49
49
  info: "Εμφάνιση %{from}-%{to} από %{count}"
50
+ range: "Εμφάνιση %{from}-%{to}"
50
51
  empty: Δεν βρέθηκαν εγγραφές
51
52
  per_page: Ανά σελίδα
52
53
  filters:
@@ -47,6 +47,7 @@ en:
47
47
  previous: Previous
48
48
  next: Next
49
49
  info: "Displaying %{from}-%{to} of %{count}"
50
+ range: "Displaying %{from}-%{to}"
50
51
  empty: No records found
51
52
  per_page: Per page
52
53
  filters:
@@ -47,6 +47,7 @@ fr:
47
47
  previous: Précédent
48
48
  next: Suivant
49
49
  info: "Affichage de %{from} à %{to} sur %{count}"
50
+ range: "Affichage de %{from} à %{to}"
50
51
  empty: Aucun enregistrement trouvé
51
52
  per_page: Par page
52
53
  filters:
@@ -46,6 +46,7 @@ ko:
46
46
  previous: 이전
47
47
  next: 다음
48
48
  info: "%{count}개 중 %{from}-%{to} 표시"
49
+ range: "%{from}-%{to} 표시"
49
50
  empty: 레코드가 없습니다
50
51
  per_page: 페이지당
51
52
  filters:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Generators
3
5
  class FieldGenerator < Rails::Generators::NamedBase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/generator_helpers"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Generators
3
5
  class ResourceGenerator < Rails::Generators::NamedBase
@@ -26,6 +26,9 @@ class <%= class_name %>Resource < Madmin::Resource
26
26
  # Customize the display name of records in the admin area.
27
27
  # def self.display_name(record) = record.name
28
28
 
29
+ # Customize the confirmation shown before deleting a record. Return nil to skip it.
30
+ # def self.delete_confirmation(record) = "Delete #{record.name}? This can't be undone."
31
+
29
32
  # Customize the default sort column and direction.
30
33
  # def self.default_sort_column = "created_at"
31
34
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/view_generator"
2
4
 
3
5
  module Madmin
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Madmin
4
+ # A page of results for a relation that was never counted. One row more than
5
+ # the page size is loaded, so it knows whether a next page exists, but not how
6
+ # many pages there are. The pagination partial shows Previous and Next links
7
+ # without page numbers or a total.
8
+ class CountlessPage < Page
9
+ # Rows loaded for this page, up to per_page + 1
10
+ attr_reader :fetched
11
+
12
+ def initialize(fetched:, page: 1, per_page: Madmin.per_page, param: :page)
13
+ super(count: nil, page: page, per_page: per_page, param: param)
14
+ @fetched = fetched
15
+ end
16
+
17
+ def count
18
+ nil
19
+ end
20
+
21
+ def more?
22
+ fetched > per_page
23
+ end
24
+
25
+ def resizable?
26
+ page > 1 || fetched > per_page_options.min
27
+ end
28
+
29
+ def from
30
+ fetched.zero? ? 0 : offset + 1
31
+ end
32
+
33
+ def to
34
+ fetched.zero? ? 0 : offset + [fetched, per_page].min
35
+ end
36
+
37
+ def prev
38
+ page - 1 if page > 1
39
+ end
40
+
41
+ def next
42
+ page + 1 if more?
43
+ end
44
+
45
+ # No page numbers without a total
46
+ def series(*)
47
+ []
48
+ end
49
+ end
50
+ end
data/lib/madmin/engine.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class Engine < ::Rails::Engine
3
5
  isolate_namespace Madmin
data/lib/madmin/field.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class Field
3
5
  attr_reader :attribute_name, :model, :options, :resource
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  # PostgreSQL array columns, shown and edited as comma separated values
@@ -1,11 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Attachment < Field
4
- # Path for removing the attachment, or nil when the attachment resource
5
- # or its destroy route has been removed from the app
6
- def remove_path(attachment)
7
- resource = Madmin.resource_for(attachment)
8
- resource.show_path(attachment) if resource.route?(:destroy)
6
+ # Path for removing the attachment, or nil when it can't be removed
7
+ def remove_path(attached)
8
+ attachment_resource = Madmin.resource_for(attached)
9
+
10
+ if attachment_resource.route?(:destroy) && !attachment_resource.readonly_for?(attached.attachment)
11
+ attachment_resource.show_path(attached)
12
+ end
9
13
  rescue MissingResource
10
14
  end
11
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Attachments < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class BelongsTo < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Boolean < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Currency < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Date < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class DateTime < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Decimal < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Enum < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class File < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Float < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class HasMany < Field
@@ -40,7 +42,7 @@ module Madmin
40
42
  page_key = "#{attribute_name}_page"
41
43
  per_page = Madmin::Page.per_page_for(params["#{attribute_name}_per_page"])
42
44
 
43
- paginate value(record), page: params[page_key], per_page: per_page, param: page_key
45
+ paginate value(record), page: params[page_key], per_page: per_page, param: page_key, countless: associated_resource&.countless?
44
46
  end
45
47
  end
46
48
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class HasOne < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Integer < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Json < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class NestedHasMany < HasMany
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class NestedHasOne < HasOne
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Password < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Polymorphic < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class RichText < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Select < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class String < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Text < Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module Fields
3
5
  class Time < Field
data/lib/madmin/filter.rb CHANGED
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  # One condition from the index filters, like `created_at gte 2026-08-15T09:00`
3
5
  class Filter
4
6
  OPERATORS = {
5
- string: %w[contains eq starts_with blank present],
6
- number: %w[eq gt gte lt lte blank],
7
- date: %w[eq gte lte blank],
8
- datetime: %w[gte lte blank],
9
- boolean: %w[true false],
10
- array: %w[includes]
7
+ string: %w[contains eq starts_with blank present].freeze,
8
+ number: %w[eq gt gte lt lte blank].freeze,
9
+ date: %w[eq gte lte blank].freeze,
10
+ datetime: %w[gte lte blank].freeze,
11
+ boolean: %w[true false].freeze,
12
+ array: %w[includes].freeze
11
13
  }.freeze
12
14
 
13
15
  WITHOUT_VALUE = %w[blank present true false].freeze
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  module GeneratorHelpers
3
5
  ROUTES_FILE = {default: "config/routes.rb", separated: "config/routes/madmin.rb"}.freeze
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  # Wraps a member action block along with its options so views can decide
3
5
  # where to render it. Responds to `to_proc` and `call` so it can be used
data/lib/madmin/menu.rb CHANGED
@@ -1,31 +1,46 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
4
+ # The navigation menu. Items are added from an initializer with `add`, or
5
+ # from a `before_render` block when they need something from the request,
6
+ # and every resource adds itself with its `menu` options.
7
+ #
8
+ # Each render builds the items from scratch, so the menu follows the locale
9
+ # and code reloading without being reset, and rendering changes nothing
10
+ # shared between requests.
2
11
  class Menu
3
12
  def initialize
4
- @children = {}
13
+ @items = []
14
+ @before_render = nil
5
15
  end
6
16
 
7
- def reset
8
- @children = {}
17
+ # Adds an item to every render. Takes the same options as a resource's `menu`
18
+ def add(options)
19
+ @items << options.dup
9
20
  end
10
21
 
22
+ # A block instance_eval'd on every render before the resources are added.
23
+ # Use `add` inside it for items that depend on the request
11
24
  def before_render(&block)
12
25
  if block_given?
13
- @before_render = block
26
+ @before_render = shareable(block)
14
27
  else
15
28
  @before_render
16
29
  end
17
30
  end
18
31
 
19
32
  def render(&block)
20
- instance_eval(&@before_render) if @before_render
33
+ tree = Tree.new
34
+ @items.each { |options| tree.add options }
35
+ tree.instance_eval(&@before_render) if @before_render
21
36
 
22
37
  # Ensure all the resources have been added to the menu
23
38
  Madmin.resources.each do |resource|
24
39
  next if resource.menu_options == false
25
- add resource.menu_options
40
+ tree.add resource.menu_options
26
41
  end
27
42
 
28
- items.each(&block)
43
+ tree.items.each(&block)
29
44
  end
30
45
 
31
46
  module Node
@@ -51,7 +66,14 @@ module Madmin
51
66
  end
52
67
  end
53
68
 
54
- include Node
69
+ # The items of a single render
70
+ class Tree
71
+ include Node
72
+
73
+ def initialize
74
+ @children = {}
75
+ end
76
+ end
55
77
 
56
78
  class Item
57
79
  include Node
@@ -67,5 +89,19 @@ module Madmin
67
89
  @children = {}
68
90
  end
69
91
  end
92
+
93
+ private
94
+
95
+ # A proc other Ractors can call, on Ruby 4.0. The block is instance_eval'd,
96
+ # so its self is replaced either way. A block capturing something
97
+ # unshareable, or any block on an earlier Ruby, is kept as it is, which
98
+ # Madmin.ractorize! then reports
99
+ def shareable(block)
100
+ return block unless Ractor.respond_to?(:shareable_proc)
101
+
102
+ Ractor.shareable_proc(self: nil, &block)
103
+ rescue Ractor::IsolationError
104
+ block
105
+ end
70
106
  end
71
107
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class Namespace
3
5
  def initialize(namespace)
data/lib/madmin/page.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  # Holds the numbers for a single page of results. Knows nothing about the database.
3
5
  #
@@ -1,13 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  # Paginates ActiveRecord relations using only the relation API (count, order,
3
5
  # offset, limit) so the SQL is generated by whichever database adapter is in use.
4
6
  module Pagination
5
7
  private
6
8
 
7
- # Returns [Madmin::Page, records]
8
- def paginate(collection, page: 1, per_page: Madmin.per_page, param: :page)
9
- page = Madmin::Page.new(count: count_for(collection), page: page, per_page: per_page, param: param)
10
- [page, with_tiebreaker(collection).offset(page.offset).limit(page.per_page)]
9
+ # Returns [Madmin::Page, records]. With countless, the relation isn't counted
10
+ # and the records come back as an Array rather than a relation
11
+ def paginate(collection, page: 1, per_page: Madmin.per_page, param: :page, countless: false)
12
+ if countless
13
+ paginate_countless(collection, page: page, per_page: per_page, param: param)
14
+ else
15
+ page = Madmin::Page.new(count: count_for(collection), page: page, per_page: per_page, param: param)
16
+ [page, with_tiebreaker(collection).offset(page.offset).limit(page.per_page)]
17
+ end
18
+ end
19
+
20
+ # Loads one row past the page to learn whether a next page exists, so the
21
+ # page object is built after the query
22
+ def paginate_countless(collection, page:, per_page:, param:)
23
+ page = [page.to_i, 1].max
24
+ records = with_tiebreaker(collection).offset((page - 1) * per_page).limit(per_page + 1).to_a
25
+
26
+ [Madmin::CountlessPage.new(page: page, per_page: per_page, param: param, fetched: records.size), records.first(per_page)]
11
27
  end
12
28
 
13
29
  def count_for(collection)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class Resource
3
5
  Attribute = Data.define(:name, :type, :field)
@@ -151,6 +153,14 @@ module Madmin
151
153
  "#{record.class} ##{record.id}"
152
154
  end
153
155
 
156
+ # Override for wording that needs more than the display name. Return nil to skip the confirmation.
157
+ def delete_confirmation(record)
158
+ I18n.t("madmin.resources.#{model.model_name.i18n_key}.confirmations.delete",
159
+ default: :"madmin.confirmations.delete",
160
+ resource: friendly_name,
161
+ name: display_name(record))
162
+ end
163
+
154
164
  def friendly_model?
155
165
  model.respond_to? :friendly
156
166
  end
@@ -159,6 +169,19 @@ module Madmin
159
169
  false
160
170
  end
161
171
 
172
+ # Whether a write to `record` through this resource is refused. Attachments
173
+ # defer to the resource of the record they belong to, and that check lives
174
+ # here because ActiveStorage::AttachmentResource is generated into the host app
175
+ def readonly_for?(record)
176
+ readonly? || (record.is_a?(::ActiveStorage::Attachment) && Madmin.resource_for(record.record).readonly?)
177
+ end
178
+
179
+ # Override to return true for tables too large to count. Index and has many
180
+ # pages then page with Previous and Next links and no page numbers or total
181
+ def countless?
182
+ false
183
+ end
184
+
162
185
  def sortable_columns
163
186
  model_column_names
164
187
  end
@@ -255,29 +278,30 @@ module Madmin
255
278
  }[type]
256
279
  end
257
280
 
281
+ # Plain ActiveModel classes have no columns or associations to ask, so their unknown attributes default to a String field
258
282
  def infer_type(name)
259
283
  name_string = name.to_s
260
284
 
261
- if model.attribute_types.include?(name_string)
262
- column_type = model.attribute_types[name_string]
285
+ if model_attribute_types.include?(name_string)
286
+ column_type = model_attribute_types[name_string]
263
287
  if column_type.is_a? ::ActiveRecord::Enum::EnumType
264
288
  :enum
265
- elsif model.columns_hash[name_string].try(:array?)
289
+ elsif model_column(name_string).try(:array?)
266
290
  :array
267
291
  else
268
292
  column_type.type || :string
269
293
  end
270
- elsif (association = model.reflect_on_association(name))
294
+ elsif (association = model.try(:reflect_on_association, name))
271
295
  type_for_association(association)
272
- elsif model.reflect_on_association(:"rich_text_#{name_string}")
296
+ elsif model.try(:reflect_on_association, :"rich_text_#{name_string}")
273
297
  :rich_text
274
- elsif model.reflect_on_association(:"#{name_string}_attachment")
298
+ elsif model.try(:reflect_on_association, :"#{name_string}_attachment")
275
299
  :attachment
276
- elsif model.reflect_on_association(:"#{name_string}_attachments")
300
+ elsif model.try(:reflect_on_association, :"#{name_string}_attachments")
277
301
  :attachments
278
302
 
279
303
  # has_secure_password
280
- elsif model.attribute_types.include?("#{name_string}_digest") || name_string.ends_with?("_confirmation")
304
+ elsif model_attribute_types.include?("#{name_string}_digest") || name_string.ends_with?("_confirmation")
281
305
  :password
282
306
 
283
307
  # ActiveRecord Store
@@ -298,8 +322,10 @@ module Madmin
298
322
  end
299
323
  end
300
324
 
325
+ # Not memoized: Rails already memoizes it, and a Ractor other than the
326
+ # main one can't write instance variables of a class
301
327
  def url_helpers
302
- @url_helpers ||= Rails.application.routes.url_helpers
328
+ Rails.application.routes.url_helpers
303
329
  end
304
330
 
305
331
  # Raises MissingRoute with instructions instead of a NoMethodError for
@@ -340,8 +366,26 @@ module Madmin
340
366
  @menu_options.with_defaults(key: @menu_options[:label] || name, label: friendly_plural_name, url: index_path)
341
367
  end
342
368
 
369
+ # Settles what the class memoizes and freezes the attributes and actions
370
+ # so the resource can be used from any Ractor. See Madmin.ractorize!
371
+ def ractorize!
372
+ model
373
+ route_namespace
374
+ menu_options
375
+ Ractor.make_shareable([attributes, scopes, member_actions, collection_actions, @menu_options])
376
+ self
377
+ end
378
+
343
379
  private
344
380
 
381
+ def model_attribute_types
382
+ model.try(:attribute_types) || {}
383
+ end
384
+
385
+ def model_column(name)
386
+ model.columns_hash[name] if model.respond_to?(:columns_hash)
387
+ end
388
+
345
389
  def translated_name(count: 1)
346
390
  model.model_name.human(count: count, default: "").presence
347
391
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
4
  class ResourceBuilder
3
5
  attr_reader :model
data/lib/madmin/search.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # based on Administrate Search: https://github.com/thoughtbot/administrate/blob/main/lib/administrate/search.rb
2
4
 
3
5
  module Madmin
@@ -11,7 +13,10 @@ module Madmin
11
13
  @filters = filters
12
14
  end
13
15
 
16
+ # Search and filters are SQL, so collections without a table pass through for `scoped_resources` to search
14
17
  def run
18
+ return @scoped_resource unless @scoped_resource.respond_to?(:table_name)
19
+
15
20
  results = query.blank? ? @scoped_resource.all : search_results(@scoped_resource)
16
21
  filters.reduce(results) { |scope, filter| filter.apply(scope) }
17
22
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Madmin
2
- VERSION = "3.3.0"
4
+ VERSION = "3.4.1"
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/generators/base"
2
4
  require "madmin/generator_helpers"
3
5
  require "madmin/namespace"
data/lib/madmin.rb CHANGED
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "madmin/engine"
2
4
  require "importmap-rails"
3
5
  require "stimulus-rails"
4
6
  require "turbo-rails"
5
7
 
6
8
  module Madmin
9
+ autoload :CountlessPage, "madmin/countless_page"
7
10
  autoload :Field, "madmin/field"
8
11
  autoload :Filter, "madmin/filter"
9
12
  autoload :GeneratorHelpers, "madmin/generator_helpers"
@@ -43,12 +46,17 @@ module Madmin
43
46
  autoload :Time, "madmin/fields/time"
44
47
  end
45
48
 
46
- mattr_accessor :importmap, default: Importmap::Map.new
47
- mattr_accessor :menu, default: Menu.new
48
- mattr_accessor :per_page, default: 20
49
- mattr_accessor :site_name
50
- mattr_accessor :stylesheets, default: []
51
- mattr_accessor :resource_locations, default: []
49
+ # Settings live in instance variables of the module rather than class
50
+ # variables, which Ractors other than the main one can't read at all
51
+ class << self
52
+ attr_accessor :importmap, :menu, :per_page, :site_name, :stylesheets, :resource_locations
53
+ end
54
+
55
+ self.importmap = Importmap::Map.new
56
+ self.menu = Menu.new
57
+ self.per_page = 20
58
+ self.stylesheets = []
59
+ self.resource_locations = []
52
60
 
53
61
  class MissingResource < StandardError
54
62
  end
@@ -102,7 +110,7 @@ module Madmin
102
110
  end
103
111
 
104
112
  def resources_by_model
105
- @resources_by_model ||= resources.group_by(&:model)
113
+ @resources_by_model ||= resources.group_by(&:model).each_value(&:freeze).freeze
106
114
  end
107
115
 
108
116
  def resource_name_for(object)
@@ -137,14 +145,30 @@ module Madmin
137
145
  MESSAGE
138
146
  end
139
147
 
148
+ # Frozen, like resources_by_model, so the memo is readable from any Ractor
140
149
  def resources
141
- @resources ||= resource_names.map(&:constantize)
150
+ @resources ||= resource_names.map(&:constantize).freeze
142
151
  end
143
152
 
144
153
  def reset_resources!
145
154
  @resources = nil
146
155
  @resources_by_model = nil
147
- menu.reset
156
+ end
157
+
158
+ # Makes Madmin's settings, menu and resources Ractor-shareable, for apps
159
+ # that serve requests from Ractors. Call it after Rails.application.ractorize!.
160
+ # Resource attributes, member and collection actions and the menu's
161
+ # `before_render` block are frozen and shared too. A block that captures a
162
+ # local variable can't be shared, and raises Ractor::IsolationError here
163
+ # rather than during a request.
164
+ #
165
+ # Madmin.importmap is left alone: Importmap::Map caches per request, so
166
+ # it is importmap-rails' to share, as with Rails.application.importmap.
167
+ def ractorize!
168
+ resources.each(&:ractorize!)
169
+ resources_by_model
170
+ Ractor.make_shareable([site_name, stylesheets, resource_locations, menu])
171
+ self
148
172
  end
149
173
 
150
174
  def resource_names
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: madmin
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -220,6 +220,7 @@ files:
220
220
  - lib/generators/madmin/views/show_generator.rb
221
221
  - lib/generators/madmin/views/views_generator.rb
222
222
  - lib/madmin.rb
223
+ - lib/madmin/countless_page.rb
223
224
  - lib/madmin/engine.rb
224
225
  - lib/madmin/field.rb
225
226
  - lib/madmin/fields/array.rb