iron_admin 0.7.0 → 0.8.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 (210) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +129 -1
  3. data/README.md +48 -1
  4. data/UPGRADING.md +495 -8
  5. data/app/assets/tailwind/iron_admin/engine.css +79 -4
  6. data/app/components/iron_admin/dashboards/activity_feed_component/item_component.html.haml +6 -9
  7. data/app/components/iron_admin/dashboards/activity_feed_component.html.haml +6 -5
  8. data/app/components/iron_admin/dashboards/activity_feed_component.rb +21 -24
  9. data/app/components/iron_admin/dashboards/chart_component.html.haml +3 -5
  10. data/app/components/iron_admin/dashboards/chart_component.rb +86 -24
  11. data/app/components/iron_admin/dashboards/concerns/themed_card.rb +39 -0
  12. data/app/components/iron_admin/dashboards/metric_card_component.html.haml +8 -7
  13. data/app/components/iron_admin/dashboards/metric_card_component.rb +2 -0
  14. data/app/components/iron_admin/dashboards/progress_component.html.haml +11 -7
  15. data/app/components/iron_admin/dashboards/progress_component.rb +11 -6
  16. data/app/components/iron_admin/dashboards/quick_links_component/link_component.html.haml +8 -9
  17. data/app/components/iron_admin/dashboards/quick_links_component.html.haml +6 -5
  18. data/app/components/iron_admin/dashboards/quick_links_component.rb +4 -12
  19. data/app/components/iron_admin/dashboards/recent_table_component.html.haml +15 -10
  20. data/app/components/iron_admin/dashboards/recent_table_component.rb +49 -0
  21. data/app/components/iron_admin/dashboards/stats_grid_component/stat_component.html.haml +13 -14
  22. data/app/components/iron_admin/dashboards/stats_grid_component.html.haml +1 -0
  23. data/app/components/iron_admin/dashboards/stats_grid_component.rb +9 -11
  24. data/app/components/iron_admin/filters/bar_component.html.haml +8 -6
  25. data/app/components/iron_admin/filters/bar_component.rb +56 -9
  26. data/app/components/iron_admin/filters/control_styles.rb +82 -0
  27. data/app/components/iron_admin/filters/date_range_component.html.haml +9 -4
  28. data/app/components/iron_admin/filters/date_range_component.rb +10 -6
  29. data/app/components/iron_admin/filters/search_component.html.haml +4 -4
  30. data/app/components/iron_admin/filters/search_component.rb +3 -5
  31. data/app/components/iron_admin/filters/select_filter_component.html.haml +1 -1
  32. data/app/components/iron_admin/filters/select_filter_component.rb +5 -16
  33. data/app/components/iron_admin/form/belongs_to_autocomplete_component.html.haml +9 -5
  34. data/app/components/iron_admin/form/belongs_to_autocomplete_component.rb +10 -8
  35. data/app/components/iron_admin/form/belongs_to_component.html.haml +2 -2
  36. data/app/components/iron_admin/form/belongs_to_component.rb +4 -18
  37. data/app/components/iron_admin/form/checkbox_component.rb +2 -3
  38. data/app/components/iron_admin/form/concerns/form_input_behavior.rb +1 -1
  39. data/app/components/iron_admin/form/control_classes.rb +108 -0
  40. data/app/components/iron_admin/form/date_picker_component.rb +3 -7
  41. data/app/components/iron_admin/form/field_wrapper_component.html.haml +3 -3
  42. data/app/components/iron_admin/form/field_wrapper_component.rb +3 -3
  43. data/app/components/iron_admin/form/nested_form_component.html.haml +4 -5
  44. data/app/components/iron_admin/form/nested_form_component.rb +12 -2
  45. data/app/components/iron_admin/form/select_component.html.haml +1 -1
  46. data/app/components/iron_admin/form/select_component.rb +2 -16
  47. data/app/components/iron_admin/form/text_input_component.rb +1 -5
  48. data/app/components/iron_admin/form/textarea_component.rb +1 -5
  49. data/app/components/iron_admin/layout/flash_component.html.haml +1 -1
  50. data/app/components/iron_admin/layout/flash_component.rb +4 -3
  51. data/app/components/iron_admin/layout/navbar_component.html.haml +15 -9
  52. data/app/components/iron_admin/layout/navbar_component.rb +29 -1
  53. data/app/components/iron_admin/layout/shell_component.html.haml +4 -4
  54. data/app/components/iron_admin/layout/shell_component.rb +16 -3
  55. data/app/components/iron_admin/layout/sidebar_component.html.haml +21 -25
  56. data/app/components/iron_admin/layout/sidebar_component.rb +76 -1
  57. data/app/components/iron_admin/layout/theme_switcher_component.html.haml +15 -0
  58. data/app/components/iron_admin/layout/theme_switcher_component.rb +74 -0
  59. data/app/components/iron_admin/resources/actions_component.html.haml +15 -8
  60. data/app/components/iron_admin/resources/actions_component.rb +34 -2
  61. data/app/components/iron_admin/resources/breadcrumb_component.html.haml +3 -3
  62. data/app/components/iron_admin/resources/breadcrumb_component.rb +8 -7
  63. data/app/components/iron_admin/resources/bulk_actions_component.html.haml +9 -8
  64. data/app/components/iron_admin/resources/bulk_actions_component.rb +47 -4
  65. data/app/components/iron_admin/resources/data_table_component.html.haml +15 -15
  66. data/app/components/iron_admin/resources/data_table_component.rb +34 -3
  67. data/app/components/iron_admin/resources/related_list_component.html.haml +7 -7
  68. data/app/components/iron_admin/resources/related_list_component.rb +26 -2
  69. data/app/components/iron_admin/resources/show_field_component.html.haml +3 -3
  70. data/app/components/iron_admin/resources/show_field_component.rb +10 -4
  71. data/app/components/iron_admin/resources/styles.rb +136 -0
  72. data/app/components/iron_admin/ui/alert_component.html.haml +2 -2
  73. data/app/components/iron_admin/ui/alert_component.rb +27 -31
  74. data/app/components/iron_admin/ui/badge_component.rb +40 -20
  75. data/app/components/iron_admin/ui/button_component.rb +37 -11
  76. data/app/components/iron_admin/ui/card_component.rb +3 -3
  77. data/app/components/iron_admin/ui/dropdown_component.html.haml +1 -1
  78. data/app/components/iron_admin/ui/dropdown_component.rb +34 -13
  79. data/app/components/iron_admin/ui/empty_state_component.html.haml +4 -5
  80. data/app/components/iron_admin/ui/empty_state_component.rb +2 -2
  81. data/app/components/iron_admin/ui/modal_component.html.haml +5 -5
  82. data/app/components/iron_admin/ui/modal_component.rb +6 -4
  83. data/app/components/iron_admin/ui/pagination_component.html.haml +14 -14
  84. data/app/components/iron_admin/ui/pagination_component.rb +26 -9
  85. data/app/components/iron_admin/ui/scopes_component.html.haml +2 -2
  86. data/app/components/iron_admin/ui/scopes_component.rb +44 -11
  87. data/app/components/iron_admin/ui/tooltip_component.rb +11 -4
  88. data/app/controllers/iron_admin/application_controller.rb +58 -4
  89. data/app/controllers/iron_admin/audit_controller.rb +30 -6
  90. data/app/controllers/iron_admin/concerns/action_executable.rb +37 -0
  91. data/app/controllers/iron_admin/concerns/assignable_references.rb +149 -0
  92. data/app/controllers/iron_admin/concerns/authorizable.rb +11 -0
  93. data/app/controllers/iron_admin/concerns/blank_password_filterable.rb +21 -0
  94. data/app/controllers/iron_admin/concerns/event_emittable.rb +47 -7
  95. data/app/controllers/iron_admin/concerns/filterable.rb +34 -9
  96. data/app/controllers/iron_admin/concerns/nested_permittable.rb +5 -3
  97. data/app/controllers/iron_admin/concerns/nested_tenant_check.rb +159 -0
  98. data/app/controllers/iron_admin/concerns/polymorphic_references.rb +131 -0
  99. data/app/controllers/iron_admin/concerns/scopeable.rb +54 -7
  100. data/app/controllers/iron_admin/concerns/searchable.rb +3 -3
  101. data/app/controllers/iron_admin/concerns/tenant_assignable.rb +118 -0
  102. data/app/controllers/iron_admin/concerns/tenant_containment.rb +146 -0
  103. data/app/controllers/iron_admin/exports_controller.rb +2 -2
  104. data/app/controllers/iron_admin/imports_controller.rb +1 -1
  105. data/app/controllers/iron_admin/resources_controller.rb +48 -41
  106. data/app/controllers/iron_admin/search_controller.rb +1 -2
  107. data/app/controllers/iron_admin/themes_controller.rb +50 -0
  108. data/app/controllers/iron_admin/tools_controller.rb +58 -5
  109. data/app/helpers/iron_admin/action_form_helper.rb +19 -0
  110. data/app/helpers/iron_admin/application_helper.rb +93 -16
  111. data/app/helpers/iron_admin/assignable_records_helper.rb +32 -0
  112. data/app/helpers/iron_admin/component_overrides_helper.rb +9 -4
  113. data/app/helpers/iron_admin/field_display_helper.rb +88 -43
  114. data/app/helpers/iron_admin/related_records_helper.rb +51 -0
  115. data/app/helpers/iron_admin/theme_helper.rb +56 -46
  116. data/app/javascript/iron_admin/controllers/cp_bulk_select_controller.js +12 -0
  117. data/app/javascript/iron_admin/controllers/cp_chart_controller.js +4 -0
  118. data/app/javascript/iron_admin/controllers/cp_disclosure_controller.js +19 -0
  119. data/app/javascript/iron_admin/controllers/cp_search_shortcut_controller.js +39 -0
  120. data/app/javascript/iron_admin/index.js +4 -0
  121. data/app/views/iron_admin/audit/index.html.haml +78 -81
  122. data/app/views/iron_admin/dashboard/index.html.haml +10 -9
  123. data/app/views/iron_admin/form/_action_field.html.haml +50 -0
  124. data/app/views/iron_admin/form/_nested_row.html.haml +18 -13
  125. data/app/views/iron_admin/imports/new.html.haml +14 -13
  126. data/app/views/iron_admin/imports/preview.html.haml +24 -21
  127. data/app/views/iron_admin/resources/_form.html.haml +70 -68
  128. data/app/views/iron_admin/resources/action_form.html.haml +16 -59
  129. data/app/views/iron_admin/resources/bulk_action_form.html.haml +20 -58
  130. data/app/views/iron_admin/resources/edit.html.haml +3 -3
  131. data/app/views/iron_admin/resources/index.html.haml +87 -74
  132. data/app/views/iron_admin/resources/new.html.haml +2 -2
  133. data/app/views/iron_admin/resources/show.html.haml +53 -41
  134. data/app/views/iron_admin/search/index.html.haml +17 -9
  135. data/app/views/iron_admin/shared/_pagination.html.haml +13 -10
  136. data/app/views/iron_admin/shared/error.html.haml +16 -12
  137. data/app/views/iron_admin/tools/action_form.html.haml +9 -55
  138. data/app/views/iron_admin/tools/show.html.haml +4 -5
  139. data/app/views/layouts/iron_admin/_document_head.html.haml +24 -0
  140. data/app/views/layouts/iron_admin/application.html.haml +4 -17
  141. data/app/views/layouts/iron_admin/minimal.html.haml +7 -0
  142. data/config/importmap.rb +2 -0
  143. data/config/locales/en.yml +23 -0
  144. data/config/routes.rb +1 -0
  145. data/docs/FAQ.md +87 -37
  146. data/docs/README.md +8 -2
  147. data/docs/components/README.md +6 -2
  148. data/docs/components/dashboard-components.md +24 -35
  149. data/docs/components/filter-components.md +44 -92
  150. data/docs/components/form-components.md +16 -32
  151. data/docs/components/resource-components.md +46 -27
  152. data/docs/components/ui-components.md +44 -26
  153. data/docs/getting-started/installation.md +5 -1
  154. data/docs/getting-started/quick-start.md +36 -23
  155. data/docs/guides/authentication.md +182 -42
  156. data/docs/guides/components.md +97 -15
  157. data/docs/guides/custom-adapters.md +24 -4
  158. data/docs/guides/dashboard.md +68 -44
  159. data/docs/guides/export.md +24 -8
  160. data/docs/guides/extending.md +206 -51
  161. data/docs/guides/fields.md +229 -70
  162. data/docs/guides/imports.md +33 -18
  163. data/docs/guides/resources.md +333 -111
  164. data/docs/guides/theming.md +930 -58
  165. data/docs/guides/troubleshooting.md +7 -3
  166. data/docs/plugin-system-design.md +8 -7
  167. data/docs/reference/configuration.md +51 -16
  168. data/docs/reference/routes.md +1 -0
  169. data/docs/reference/theme-properties.md +101 -38
  170. data/lib/generators/iron_admin/install/templates/initializer.rb.tt +7 -2
  171. data/lib/generators/iron_admin/install_audit/templates/create_iron_admin_audit_entries.rb.tt +2 -0
  172. data/lib/iron_admin/action_field.rb +11 -0
  173. data/lib/iron_admin/adapters/active_record.rb +95 -0
  174. data/lib/iron_admin/adapters/base.rb +107 -0
  175. data/lib/iron_admin/adapters/http/query.rb +21 -0
  176. data/lib/iron_admin/adapters/http.rb +84 -4
  177. data/lib/iron_admin/adapters/mongoid.rb +96 -2
  178. data/lib/iron_admin/audit_log.rb +64 -6
  179. data/lib/iron_admin/authentication_warning.rb +31 -0
  180. data/lib/iron_admin/concerns/filter_definable.rb +15 -1
  181. data/lib/iron_admin/concerns/nestable.rb +25 -0
  182. data/lib/iron_admin/concerns/scope_countable.rb +56 -0
  183. data/lib/iron_admin/concerns/soft_deletable.rb +28 -9
  184. data/lib/iron_admin/configuration/components.rb +14 -3
  185. data/lib/iron_admin/configuration/theme/neutral_defaults.rb +179 -0
  186. data/lib/iron_admin/configuration/theme.rb +292 -87
  187. data/lib/iron_admin/configuration.rb +340 -45
  188. data/lib/iron_admin/dashboard.rb +22 -38
  189. data/lib/iron_admin/engine.rb +16 -2
  190. data/lib/iron_admin/errors.rb +3 -0
  191. data/lib/iron_admin/field.rb +78 -17
  192. data/lib/iron_admin/field_inferrer.rb +61 -3
  193. data/lib/iron_admin/field_override_warning.rb +67 -0
  194. data/lib/iron_admin/import/import_preview.rb +56 -7
  195. data/lib/iron_admin/import/importer.rb +91 -14
  196. data/lib/iron_admin/import/tenant_precheck.rb +67 -0
  197. data/lib/iron_admin/plugin/registration.rb +64 -0
  198. data/lib/iron_admin/plugin_registry.rb +3 -2
  199. data/lib/iron_admin/policy.rb +22 -12
  200. data/lib/iron_admin/policy_coverage_warning.rb +46 -0
  201. data/lib/iron_admin/request_context.rb +18 -0
  202. data/lib/iron_admin/resource.rb +218 -77
  203. data/lib/iron_admin/resource_registry.rb +86 -13
  204. data/lib/iron_admin/scope_counts.rb +73 -0
  205. data/lib/iron_admin/tenant_scope.rb +30 -0
  206. data/lib/iron_admin/tool.rb +61 -12
  207. data/lib/iron_admin/tool_context.rb +10 -6
  208. data/lib/iron_admin/version.rb +1 -1
  209. data/lib/iron_admin.rb +29 -2
  210. metadata +37 -7
@@ -193,6 +193,24 @@ module IronAdmin
193
193
  raise NotImplementedError
194
194
  end
195
195
 
196
+ # Number of records the scope lists, matching the index total, or nil
197
+ # when the adapter cannot know it exactly. Unknown is not zero.
198
+ #
199
+ # The default asks {#count}, so a custom adapter keeps working. When
200
+ # {#count} raises +NotImplementedError+, the count is unknown: this
201
+ # returns nil and logs nothing.
202
+ # {ActiveRecord} counts rows with +count(:all)+ and turns a grouped Hash
203
+ # into its size, which is what the index pagination uses. {Http} returns
204
+ # +meta.total+, or nil when the response has none.
205
+ #
206
+ # @param scope [Object, nil] the relation to count, or every record when nil
207
+ # @return [Integer, nil]
208
+ def exact_count(scope = nil)
209
+ count(scope)
210
+ rescue NotImplementedError
211
+ nil
212
+ end
213
+
196
214
  # --- Search ---
197
215
 
198
216
  # Searches a single column with LIKE/ILIKE.
@@ -222,6 +240,31 @@ module IronAdmin
222
240
  raise NotImplementedError
223
241
  end
224
242
 
243
+ # Plain equality values from +scope+ that are attributes of the scope's model.
244
+ #
245
+ # Adapters override this with the scalar equalities they can read. The
246
+ # default assigns nothing. Keys are attribute names as Strings. Arrays,
247
+ # ranges, hashes, sets, and relations are not plain equalities.
248
+ #
249
+ # @param scope [Object] relation whose equality conditions are read
250
+ # @return [Hash{String => Object}]
251
+ def scope_attributes(_scope)
252
+ {}
253
+ end
254
+
255
+ # Builds a new unsaved record inside +scope+.
256
+ #
257
+ # Adapters that copy equality conditions off a scope override this so
258
+ # those conditions become attributes. The default ignores +scope+ and
259
+ # delegates to {#build}.
260
+ #
261
+ # @param scope [Object] Relation the record is built from
262
+ # @param attrs [Hash] Initial attributes
263
+ # @return [Object] New record
264
+ def build_in(_scope, attrs = {})
265
+ build(attrs)
266
+ end
267
+
225
268
  # Persists a record.
226
269
  # @param record [Object] The record to save
227
270
  # @return [Boolean] Success
@@ -261,6 +304,45 @@ module IronAdmin
261
304
  raise NotImplementedError
262
305
  end
263
306
 
307
+ # Whether {#transaction} can roll a write back.
308
+ #
309
+ # Data sources that persist each write immediately return +false+.
310
+ # Mongoid and HTTP keep this default. Their writes are checked in
311
+ # memory and rejected unless the candidate is inside the tenant.
312
+ # Active Record returns +true+ and keeps the post-write rollback.
313
+ #
314
+ # @return [Boolean]
315
+ def supports_transactions?
316
+ false
317
+ end
318
+
319
+ # Whether +record+ satisfies +scope+, evaluated in memory.
320
+ #
321
+ # Returns +:inside+ or +:outside+ when the adapter can decide, and
322
+ # +:unknown+ when it cannot. Callers write only on +:inside+. +record+
323
+ # may be unsaved and already carry its new values. The default
324
+ # cannot tell.
325
+ #
326
+ # @param scope [Object] tenant scope to test
327
+ # @param record [Object] candidate, possibly unsaved
328
+ # @return [Symbol] +:inside+, +:outside+, or +:unknown+
329
+ def scope_match(_scope, _record)
330
+ :unknown
331
+ end
332
+
333
+ # A non-persisted copy of +record+ with +attrs+ applied.
334
+ #
335
+ # Used to test an update against the tenant before writing. The copy
336
+ # is never saved. The default cannot build one and returns +nil+,
337
+ # which rejects the write.
338
+ #
339
+ # @param record [Object] record being updated
340
+ # @param attrs [Hash] attributes that would be written
341
+ # @return [Object, nil]
342
+ def preview(_record, _attrs)
343
+ nil
344
+ end
345
+
264
346
  # --- Transactions ---
265
347
 
266
348
  # Wraps a block in a transaction.
@@ -270,6 +352,17 @@ module IronAdmin
270
352
  raise NotImplementedError
271
353
  end
272
354
 
355
+ # Runs the block in a nested transaction that can roll back on its own.
356
+ #
357
+ # Adapters without nested transactions, including Mongoid, keep this
358
+ # default and run the block directly. Active Record opens a savepoint.
359
+ #
360
+ # @yield The block to run inside the savepoint
361
+ # @return [Object] the block's return value
362
+ def savepoint(&)
363
+ yield
364
+ end
365
+
273
366
  # Runs the block once the current transaction commits, and never if it
274
367
  # rolls back. IronAdmin audits changes and calls on_action through it.
275
368
  # Data sources without transactions persist each write at once, so the
@@ -290,6 +383,20 @@ module IronAdmin
290
383
  raise NotImplementedError
291
384
  end
292
385
 
386
+ # Whether `with_deleted` / `only_deleted` tabs can be registered.
387
+ # @return [Boolean]
388
+ def supports_soft_delete_tabs?
389
+ false
390
+ end
391
+
392
+ # The scope without the column's condition, keeping only rows whose column is not nil.
393
+ # @param scope [Object] The current query scope
394
+ # @param column [Symbol, String] Soft-delete column
395
+ # @return [Object]
396
+ def only_soft_deleted(_scope, _column)
397
+ raise NotImplementedError
398
+ end
399
+
293
400
  # --- Batch ---
294
401
 
295
402
  # Iterates records in memory-efficient batches.
@@ -37,6 +37,16 @@ module IronAdmin
37
37
  @total || @records.length
38
38
  end
39
39
 
40
+ # The provider's exact total, or nil when the response did not include
41
+ # one. Zero stays zero. Unlike {#count}, this never falls back to the
42
+ # page length.
43
+ #
44
+ # @return [Integer, nil]
45
+ def exact_total
46
+ fetch!
47
+ @total
48
+ end
49
+
40
50
  def first
41
51
  fetch!
42
52
  @records.first
@@ -69,6 +79,17 @@ module IronAdmin
69
79
  dup_with(params: @params.merge(other.accumulated_params))
70
80
  end
71
81
 
82
+ # Filters accumulated by {#where}, including non-scalar values.
83
+ #
84
+ # Keys keep the type the caller passed. Does not perform a request.
85
+ # {IronAdmin::Adapters::Http#scope_attributes} stringifies the scalar
86
+ # equalities and drops lists.
87
+ #
88
+ # @return [Hash]
89
+ def conditions
90
+ @params.dup
91
+ end
92
+
72
93
  protected
73
94
 
74
95
  def accumulated_params
@@ -9,10 +9,14 @@ module IronAdmin
9
9
  # Faraday is used as the HTTP transport (lazy loaded).
10
10
  #
11
11
  # @example Minimal resource configuration
12
- # class ProductResource < IronAdmin::Resource
13
- # self.adapter_class = :http
14
- # http_config do |c|
15
- # c.base_url = "https://api.example.com/v1"
12
+ # module IronAdmin
13
+ # module Resources
14
+ # class ProductResource < IronAdmin::Resource
15
+ # self.adapter_class = :http
16
+ # http_config do |c|
17
+ # c.base_url = "https://api.example.com/v1"
18
+ # end
19
+ # end
16
20
  # end
17
21
  # end
18
22
  class Http < Base
@@ -99,6 +103,18 @@ module IronAdmin
99
103
  (scope || all).count
100
104
  end
101
105
 
106
+ # The provider's exact total for +scope+, or for every record when
107
+ # +scope+ is nil. A response with no +meta.total+ returns nil. An
108
+ # unknown total is not zero. {#count} still returns the page length,
109
+ # which pagination uses.
110
+ #
111
+ # @param scope [IronAdmin::Adapters::Http::Query, nil]
112
+ # @return [Integer, nil]
113
+ def exact_count(scope = nil)
114
+ relation = scope || all
115
+ relation.respond_to?(:exact_total) ? relation.exact_total : nil
116
+ end
117
+
102
118
  # --- Search ---
103
119
 
104
120
  def search_column(scope, column, query)
@@ -148,6 +164,66 @@ module IronAdmin
148
164
  connection.delete(record.id.to_s)
149
165
  end
150
166
 
167
+ # Plain scalar equalities on an HTTP query.
168
+ #
169
+ # Keys are Strings. Arrays, ranges, hashes, and sets are omitted so an
170
+ # +IN+ list is not stamped onto a scalar attribute. Reads
171
+ # {IronAdmin::Adapters::Http::Query#conditions} and does not request.
172
+ #
173
+ # @param scope [IronAdmin::Adapters::Http::Query]
174
+ # @return [Hash{String => Object}]
175
+ def scope_attributes(scope)
176
+ return {} unless scope.respond_to?(:conditions)
177
+
178
+ scope.conditions.each_with_object({}) do |(key, value), kept|
179
+ next if composite_scope_value?(value)
180
+
181
+ kept[key.to_s] = value
182
+ end
183
+ end
184
+
185
+ # Builds a record from +scope+ so equality conditions become attributes.
186
+ #
187
+ # Submitted +attrs+ are applied first. {#scope_attributes} is assigned
188
+ # afterwards, so a posted tenant id cannot move the row to another tenant.
189
+ #
190
+ # @param scope [IronAdmin::Adapters::Http::Query]
191
+ # @param attrs [Hash] initial attributes
192
+ # @return [IronAdmin::Adapters::Http::Record]
193
+ def build_in(scope, attrs = {})
194
+ build(attrs).tap { |record| record.assign_attributes(scope_attributes(scope)) }
195
+ end
196
+
197
+ # Whether every condition on +scope+ is a scalar equality that +record+ has.
198
+ #
199
+ # Returns +:unknown+ when any condition is an +IN+ list, range, hash, or set,
200
+ # or when +record+ is missing. An empty condition list is +:inside+.
201
+ #
202
+ # @param scope [IronAdmin::Adapters::Http::Query]
203
+ # @param record [Object] candidate, possibly unsaved
204
+ # @return [Symbol] +:inside+, +:outside+, or +:unknown+
205
+ def scope_match(scope, record)
206
+ return :unknown unless scope.respond_to?(:conditions)
207
+ return :unknown if record.nil?
208
+
209
+ conditions = scope.conditions
210
+ return :unknown if conditions.any? { |_key, value| composite_scope_value?(value) }
211
+
212
+ conditions.all? { |key, value| record[key] == value } ? :inside : :outside
213
+ end
214
+
215
+ # A non-persisted copy of +record+ with +attrs+ merged in.
216
+ #
217
+ # The copy is never saved. +_persisted: false+ keeps an id from making
218
+ # the copy look stored.
219
+ #
220
+ # @param record [IronAdmin::Adapters::Http::Record]
221
+ # @param attrs [Hash]
222
+ # @return [IronAdmin::Adapters::Http::Record]
223
+ def preview(record, attrs)
224
+ Record.new(record.attributes.merge(attrs.to_h).merge(_persisted: false))
225
+ end
226
+
151
227
  # --- Transactions ---
152
228
 
153
229
  def transaction
@@ -192,6 +268,10 @@ module IronAdmin
192
268
 
193
269
  private
194
270
 
271
+ def composite_scope_value?(value)
272
+ value.is_a?(Array) || value.is_a?(Range) || value.is_a?(Hash) || (defined?(Set) && value.is_a?(Set))
273
+ end
274
+
195
275
  def add_api_errors(record, data) # rubocop:disable Naming/PredicateMethod
196
276
  return false unless data.is_a?(Hash) && data["errors"].is_a?(Hash)
197
277
 
@@ -9,8 +9,12 @@ module IronAdmin
9
9
  # query interface, field introspection, and association reflection.
10
10
  #
11
11
  # @example Configuring a resource to use Mongoid
12
- # class ArticleResource < IronAdmin::Resource
13
- # self.adapter_class = :mongoid
12
+ # module IronAdmin
13
+ # module Resources
14
+ # class ArticleResource < IronAdmin::Resource
15
+ # self.adapter_class = :mongoid
16
+ # end
17
+ # end
14
18
  # end
15
19
  class Mongoid < Base
16
20
  # Maps Mongoid field types (Ruby classes) to IronAdmin symbol types.
@@ -160,6 +164,83 @@ module IronAdmin
160
164
  model_class.new(cast_array_attributes(attrs))
161
165
  end
162
166
 
167
+ # Plain scalar equalities on +scope+ that are fields of the scope's model.
168
+ #
169
+ # Reads the Mongoid selector and keeps keys of +scope.klass+ when the
170
+ # scope exposes it, otherwise this adapter's model. Hashes such as
171
+ # +$in+ and arrays are omitted.
172
+ #
173
+ # @param scope [Mongoid::Criteria]
174
+ # @return [Hash{String => Object}]
175
+ def scope_attributes(scope)
176
+ return {} unless scope.respond_to?(:selector)
177
+
178
+ model = scope.respond_to?(:klass) ? scope.klass : model_class
179
+ allowed = model.fields.keys.map(&:to_s)
180
+ scope.selector.each_with_object({}) do |(key, value), kept|
181
+ name = key.to_s
182
+ next unless allowed.include?(name)
183
+ next if value.is_a?(Hash) || value.is_a?(Array)
184
+
185
+ kept[name] = value
186
+ end
187
+ end
188
+
189
+ # Builds a record from +scope+ so equality conditions become attributes.
190
+ #
191
+ # Submitted +attrs+ are applied first, including the array-field cast from
192
+ # {#build}. {#scope_attributes} is assigned afterwards, so a posted tenant
193
+ # id cannot move the document to another tenant.
194
+ #
195
+ # @param scope [Mongoid::Criteria] Scope to build from
196
+ # @param attrs [Hash, ActionController::Parameters] Initial attributes
197
+ # @return [Mongoid::Document] New record
198
+ def build_in(scope, attrs = {})
199
+ build(attrs).tap { |record| record.assign_attributes(scope_attributes(scope)) }
200
+ end
201
+
202
+ # Whether +record+ matches +scope+ in memory.
203
+ #
204
+ # Uses +Mongoid::Matchable#_matches?+ with the criteria selector when
205
+ # the record implements it. Returns +nil+ when the record or the scope
206
+ # cannot be checked, and the caller then rejects the write.
207
+ #
208
+ # @param scope [Object] criteria whose +selector+ is tested
209
+ # @param record [Object] candidate, possibly unsaved
210
+ # @return [Boolean, nil]
211
+ # Whether +record+ satisfies the criteria +scope+, evaluated in memory.
212
+ #
213
+ # Returns +:unknown+ when +record+ cannot run +_matches?+, when +scope+
214
+ # has no selector, or when +_matches?+ itself returns +nil+.
215
+ #
216
+ # @param scope [Object] criteria whose +selector+ is tested
217
+ # @param record [Object] candidate, possibly unsaved
218
+ # @return [Symbol] +:inside+, +:outside+, or +:unknown+
219
+ def scope_match(scope, record)
220
+ return :unknown unless record.respond_to?(:_matches?)
221
+ return :unknown unless scope.respond_to?(:selector)
222
+
223
+ matched = record._matches?(scope.selector)
224
+ return :unknown if matched.nil?
225
+
226
+ matched ? :inside : :outside
227
+ end
228
+
229
+ # A non-persisted copy of +record+ with +attrs+ applied.
230
+ #
231
+ # The copy is built from the document attributes without +_id+, then
232
+ # {#cast_array_attributes} is assigned. Nothing is saved.
233
+ #
234
+ # @param record [Object] document being updated
235
+ # @param attrs [Hash] attributes that would be written
236
+ # @return [Object]
237
+ def preview(record, attrs)
238
+ attributes = record.attributes.except("_id", :_id)
239
+ copy = record.class.new(attributes)
240
+ copy.assign_attributes(cast_array_attributes(attrs))
241
+ copy
242
+ end
243
+
163
244
  def save(record)
164
245
  record.save
165
246
  end
@@ -188,6 +269,19 @@ module IronAdmin
188
269
  new_scope
189
270
  end
190
271
 
272
+ # @return [Boolean]
273
+ def supports_soft_delete_tabs?
274
+ true
275
+ end
276
+
277
+ # The scope without the column's condition, keeping only rows whose column is not nil.
278
+ # @param scope [Object] The current query scope
279
+ # @param column [Symbol, String] Soft-delete column
280
+ # @return [Object]
281
+ def only_soft_deleted(scope, column)
282
+ unscope_column(scope, column).where(column.to_s => { "$ne" => nil })
283
+ end
284
+
191
285
  # --- Batch ---
192
286
 
193
287
  def find_each(scope, &)
@@ -55,6 +55,7 @@ module IronAdmin
55
55
 
56
56
  # @return [String] The IP address of the request
57
57
  attr_accessor :ip_address
58
+ attr_accessor :tenant_key
58
59
 
59
60
  # @return [Time] When the action occurred
60
61
  attr_accessor :timestamp
@@ -92,6 +93,7 @@ module IronAdmin
92
93
  record_id: record_id,
93
94
  changes: changes,
94
95
  ip_address: ip_address,
96
+ tenant_key: tenant_key,
95
97
  timestamp: timestamp,
96
98
  }
97
99
  end
@@ -131,6 +133,10 @@ module IronAdmin
131
133
  # @option filters [Integer, String] :record_id Filter by record ID
132
134
  # @option filters [Time, Date] :from Entries after this time
133
135
  # @option filters [Time, Date] :to Entries before this time
136
+ # @option filters [String, nil] :tenant_key Only the entries with this
137
+ # tenant key. When the key is given and nil, no entries match.
138
+ # @raise [ConfigurationError] with database storage, when +:tenant_key+ is
139
+ # given and the table has no +tenant_key+ column
134
140
  #
135
141
  # @return [Array<Entry>, ActiveRecord::Relation] Matching entries
136
142
  #
@@ -147,6 +153,31 @@ module IronAdmin
147
153
  end
148
154
  end
149
155
 
156
+ # The identifier database storage keeps for a user: its id, or the value
157
+ # itself (a stored identifier). Blank without a user.
158
+ #
159
+ # @param user [Object, nil] A user, or a stored identifier
160
+ # @return [String]
161
+ def user_identifier(user)
162
+ user.respond_to?(:id) ? user.id.to_s : user.to_s
163
+ end
164
+
165
+ # Labels for the users of the given entries, from one call to the
166
+ # {Configuration#audit_user_labels} block.
167
+ #
168
+ # @param entries [Enumerable<Entry, AuditEntry>]
169
+ # @return [Hash{String => String}] Label by user identifier; empty without
170
+ # the block. Users the block gives no label are left out.
171
+ def user_labels(entries)
172
+ block = IronAdmin.configuration.audit_user_labels_block
173
+ return {} unless block
174
+
175
+ identifiers = entries.map { |entry| user_identifier(entry.user) }.compact_blank.uniq
176
+ return {} if identifiers.empty?
177
+
178
+ block.call(identifiers).to_h.to_h { |identifier, label| [identifier.to_s, label.to_s] }.compact_blank
179
+ end
180
+
150
181
  # Clears all audit entries.
151
182
  #
152
183
  # @api private
@@ -176,7 +207,8 @@ module IronAdmin
176
207
  resource: event.resource,
177
208
  record_id: event.record_id,
178
209
  changes: event.changes,
179
- ip_address: event.ip_address
210
+ ip_address: event.ip_address,
211
+ tenant_key: event_tenant_key(event)
180
212
  )
181
213
 
182
214
  entries << entry
@@ -184,18 +216,35 @@ module IronAdmin
184
216
  end
185
217
 
186
218
  def log_to_database(event)
187
- AuditEntry.create!(
188
- user_identifier: event.user.respond_to?(:id) ? event.user.id.to_s : event.user.to_s,
219
+ attributes = {
220
+ user_identifier: user_identifier(event.user),
189
221
  action: event.action.to_s,
190
222
  resource: event.resource.to_s,
191
223
  record_id: event.record_id,
192
224
  record_changes: event.changes,
193
- ip_address: event.ip_address
194
- )
225
+ ip_address: event.ip_address,
226
+ }
227
+ attributes[:tenant_key] = event_tenant_key(event) if tenant_column?
228
+ AuditEntry.create!(attributes)
229
+ end
230
+
231
+ # Events built by the host app may not answer +tenant_key+.
232
+ def event_tenant_key(event)
233
+ event.respond_to?(:tenant_key) ? event.tenant_key&.to_s.presence : nil
234
+ end
235
+
236
+ def tenant_column?
237
+ AuditEntry.column_names.include?("tenant_key")
195
238
  end
196
239
 
197
240
  def query_memory(filters)
198
- result = entries
241
+ return [] if filters.key?(:tenant_key) && filters[:tenant_key].nil?
242
+
243
+ result = filters.key?(:tenant_key) ? entries.select { |e| e.tenant_key == filters[:tenant_key] } : entries
244
+ filter_memory_entries(result, filters)
245
+ end
246
+
247
+ def filter_memory_entries(result, filters)
199
248
  result = result.select { |e| e.resource == filters[:resource] } if filters[:resource]
200
249
  result = result.select { |e| e.timestamp >= filters[:from] } if filters[:from]
201
250
  result = result.select { |e| e.timestamp <= filters[:to] } if filters[:to]
@@ -206,6 +255,15 @@ module IronAdmin
206
255
 
207
256
  def query_database(filters)
208
257
  scope = AuditEntry.all
258
+ if filters.key?(:tenant_key)
259
+ unless tenant_column?
260
+ raise ConfigurationError, "config.audit_tenant needs a tenant_key column on iron_admin_audit_entries. " \
261
+ "See UPGRADING.md."
262
+ end
263
+ return AuditEntry.none if filters[:tenant_key].nil?
264
+
265
+ scope = scope.where(tenant_key: filters[:tenant_key])
266
+ end
209
267
  scope = scope.by_resource(filters[:resource]) if filters[:resource]
210
268
  scope = scope.by_action(filters[:action]) if filters[:action]
211
269
  scope = scope.by_record_id(filters[:record_id]) if filters[:record_id]
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronAdmin
4
+ # Warns at boot when production runs with no +config.authenticate+ block.
5
+ #
6
+ # The engine cannot deny by itself in that case: protecting the mount with
7
+ # a route constraint (Devise's +authenticate :user do ... end+) is a valid
8
+ # setup that needs no block. So it says so once, at boot, in production.
9
+ #
10
+ # @api private
11
+ module AuthenticationWarning
12
+ # The logged message.
13
+ MESSAGE = "[IronAdmin] config.authenticate is not set: the admin answers every request " \
14
+ "unless its mount is protected by a route constraint. Set config.authenticate " \
15
+ "in config/initializers/iron_admin.rb. See " \
16
+ "https://github.com/rubylab-app/iron_admin/blob/main/docs/guides/authentication.md"
17
+
18
+ # Logs {MESSAGE} when +env+ is production and no authenticate block is set.
19
+ #
20
+ # @param configuration [IronAdmin::Configuration]
21
+ # @param env [ActiveSupport::EnvironmentInquirer]
22
+ # @param logger [#warn, nil]
23
+ # @return [Boolean] whether it warned
24
+ def self.call(configuration: IronAdmin.configuration, env: Rails.env, logger: Rails.logger)
25
+ return false unless env.production? && configuration.authenticate_block.nil?
26
+
27
+ logger&.warn(MESSAGE)
28
+ true
29
+ end
30
+ end
31
+ end
@@ -15,10 +15,21 @@ module IronAdmin
15
15
  module FilterDsl
16
16
  # Defines an index filter. It replaces the auto-inferred filter of the same name.
17
17
  #
18
+ # A +:select+ filter without +options+ offers the enum values of its column,
19
+ # the associated records when the column is a +belongs_to+ foreign key, or
20
+ # else the column's distinct values. A foreign key filter keeps the column's
21
+ # distinct values when the +belongs_to+ field is not visible to the current
22
+ # user, or is declared +autocomplete: true+ (give it +options+ to label them).
23
+ #
18
24
  # @param name [Symbol] The column or attribute to filter by
19
25
  # @param options [Hash] :type, :options, :label
26
+ # @option options [Array, #call] :options The choices of a +:select+ filter:
27
+ # [label, value] pairs or bare values (labelled by humanizing them), or a
28
+ # callable that takes no arguments and returns them when the index renders
20
29
  # @example
21
30
  # filter :status, type: :select, options: %w[pending active]
31
+ # @example Choices read when the page renders, not when the resource loads
32
+ # filter :category_id, type: :select, options: -> { Category.order(:name).pluck(:name, :id) }
22
33
  # @return [void]
23
34
  def filter(name, **options)
24
35
  self.defined_filters = defined_filters + [{ name: name, **options }]
@@ -60,9 +71,12 @@ module IronAdmin
60
71
  private
61
72
 
62
73
  def sensitive_field_names
63
- field_overrides.filter_map do |name, options|
74
+ from_overrides = field_overrides.filter_map do |name, options|
64
75
  name.to_sym if Field::SECRET_TYPES.include?(options[:type]) || options[:visible] == false
65
76
  end
77
+ from_fields = resolved_fields.select(&:always_concealed?).flat_map(&:column_names)
78
+
79
+ (from_overrides + from_fields).uniq
66
80
  end
67
81
 
68
82
  # A declared :select on an enum with no options of its own offers the enum values.
@@ -11,6 +11,26 @@ module IronAdmin
11
11
 
12
12
  # DSL methods for declaring has_many/has_one with nested options.
13
13
  module AssociationDsl
14
+ # Declares a has_many association.
15
+ #
16
+ # The show page's related list does not read +:fields+. That option is
17
+ # used only when +nested: true+, as the columns of the nested form.
18
+ # With no +:fields+, the nested form infers the associated model's
19
+ # fields and skips id, created_at, updated_at and the foreign key.
20
+ #
21
+ # @param name [Symbol] The association name
22
+ # @param nested [Boolean] Render a nested form (default: false)
23
+ # @param allow_destroy [Boolean] Allow removing a nested row (default: true)
24
+ # @param position_field [Symbol, nil] Column that stores the nested row order
25
+ # @param fields [Array<Symbol>, nil] Nested-form columns. The related list ignores them.
26
+ # @param options [Hash] Other options, such as +:resource+
27
+ # @return [void]
28
+ #
29
+ # @example Related list
30
+ # has_many :orders
31
+ #
32
+ # @example Nested form columns
33
+ # has_many :comments, nested: true, fields: [:body, :created_at]
14
34
  def has_many(name, nested: false, allow_destroy: true, position_field: nil, fields: nil, **options)
15
35
  self.defined_associations = defined_associations.merge(
16
36
  name => {
@@ -20,6 +40,11 @@ module IronAdmin
20
40
  )
21
41
  end
22
42
 
43
+ # Declares a has_one association. +:fields+ feeds the nested form only,
44
+ # the same way as {#has_many}, and does not change a related list.
45
+ #
46
+ # @param fields [Array<Symbol>, nil] Nested-form columns
47
+ # @return [void]
23
48
  def has_one(name, nested: false, allow_destroy: true, fields: nil, **options)
24
49
  self.defined_associations = defined_associations.merge(
25
50
  name => {