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
@@ -8,33 +8,42 @@ module IronAdmin
8
8
  # filters, actions, search, and authorization.
9
9
  #
10
10
  # @example Basic resource definition
11
- # class UserResource < IronAdmin::Resource
12
- # field :email, readonly: true
13
- # field :role, type: :select, options: %w[admin user guest]
14
- # field :password_digest, visible: false
11
+ # module IronAdmin
12
+ # module Resources
13
+ # class UserResource < IronAdmin::Resource
14
+ # field :email, readonly: true
15
+ # # Fields take choices:. Filters take options:.
16
+ # field :role, type: :select, choices: %w[admin user guest]
17
+ # field :password_digest, visible: false
15
18
  #
16
- # searchable :name, :email
17
- # index_fields :id, :name, :email, :role, :created_at
19
+ # searchable :name, :email
20
+ # index_fields :id, :name, :email, :role, :created_at
18
21
  #
19
- # filter :role, type: :select, options: %w[admin user guest]
22
+ # filter :role, type: :select, options: %w[admin user guest]
20
23
  #
21
- # action :suspend, icon: "pause", confirm: true do |record|
22
- # record.update!(suspended_at: Time.current)
23
- # end
24
+ # action :suspend, icon: "pause", confirm: true do |record|
25
+ # record.update!(suspended_at: Time.current)
26
+ # end
24
27
  #
25
- # policy do
26
- # allow :read
27
- # allow :update, if: ->(user) { user.admin? }
28
- # deny :delete
28
+ # policy do
29
+ # allow :read
30
+ # allow :update, if: ->(user) { user.admin? }
31
+ # deny :delete
32
+ # end
33
+ # end
29
34
  # end
30
35
  # end
31
36
  #
32
37
  # @example Resource with associations
33
- # class OrderResource < IronAdmin::Resource
34
- # belongs_to :customer
35
- # has_many :line_items, resource: LineItemResource
38
+ # module IronAdmin
39
+ # module Resources
40
+ # class OrderResource < IronAdmin::Resource
41
+ # belongs_to :customer
42
+ # has_many :line_items, resource: LineItemResource
36
43
  #
37
- # preload :customer, :line_items
44
+ # preload :customer, :line_items
45
+ # end
46
+ # end
38
47
  # end
39
48
  #
40
49
  # @see IronAdmin::Field For field configuration options
@@ -46,6 +55,7 @@ module IronAdmin
46
55
  include Concerns::LiveUpdatable
47
56
  include Concerns::ComponentOverridable
48
57
  include Concerns::FilterDefinable
58
+ include Concerns::ScopeCountable
49
59
 
50
60
  class_attribute :field_overrides, default: {}
51
61
  class_attribute :_searchable_columns, default: nil
@@ -61,6 +71,17 @@ module IronAdmin
61
71
  class_attribute :export_field_names, default: nil
62
72
  class_attribute :denied_crud_actions, default: []
63
73
  class_attribute :defined_associations, default: {}
74
+
75
+ # Model this resource manages when the class name does not name it.
76
+ #
77
+ # The URL segment is {.resource_name}. An override of a different model
78
+ # comes from the class path under `IronAdmin::Resources`
79
+ # (`AutoNoteResource` is `auto_notes`, `Shop::NoteResource` is
80
+ # `shop_notes`), not from the model's plural. An override of the model the
81
+ # class name already implies keeps that model's plural. Define
82
+ # `self.resource_name` to choose another name. There is no writer.
83
+ #
84
+ # @return [Class, nil]
64
85
  class_attribute :model_class_override, default: nil
65
86
  class_attribute :adapter_class, default: :active_record
66
87
  class_attribute :_soft_delete_scopes, default: []
@@ -105,16 +126,24 @@ module IronAdmin
105
126
  # @return [Class] The ActiveRecord model class
106
127
  #
107
128
  # @example Override with model_class_override
108
- # class LegacyUserResource < IronAdmin::Resource
109
- # self.model_class_override = OldUser
129
+ # module IronAdmin
130
+ # module Resources
131
+ # class LegacyUserResource < IronAdmin::Resource
132
+ # self.model_class_override = OldUser
133
+ # end
134
+ # end
110
135
  # end
111
136
  #
112
137
  # @example HTTP adapter — no Ruby model required
113
- # class ProductResource < IronAdmin::Resource
114
- # self.adapter_class = :http
115
- # # `model` returns `Adapters::Http::ModelProxy.new(self)` — no
116
- # # `Product` constant required; fields come from the API
117
- # # response on first access.
138
+ # module IronAdmin
139
+ # module Resources
140
+ # class ProductResource < IronAdmin::Resource
141
+ # self.adapter_class = :http
142
+ # # `model` returns `Adapters::Http::ModelProxy.new(self)` — no
143
+ # # `Product` constant required; fields come from the API
144
+ # # response on first access.
145
+ # end
146
+ # end
118
147
  # end
119
148
  def model
120
149
  return model_class_override if model_class_override
@@ -130,26 +159,47 @@ module IronAdmin
130
159
  #
131
160
  # @param name [Symbol] The field name (must match a database column or association)
132
161
  # @param options [Hash] Configuration options for the field
133
- # @option options [Symbol] :type Override the inferred field type
134
- # (:string, :text, :integer, :boolean, :date, :datetime, :select, :belongs_to)
162
+ # @option options [Symbol] :type Override the inferred field type.
163
+ # {FieldInferrer} assigns :text, :textarea, :number, :boolean, :date,
164
+ # :datetime, :time, :json, :url, :email, :select, :belongs_to,
165
+ # :polymorphic_belongs_to, :file, :files and :rich_text. The form also
166
+ # renders :badge, :password, :hidden, :color, :currency, :markdown,
167
+ # :tags, :key_value, :boolean_group, :external_image, :progress_bar,
168
+ # :code and :radio. :string and :integer are not field types, and neither
169
+ # is any other unknown type (including :time, which is inferred but has
170
+ # no form branch): the form renders a text input. An import still casts
171
+ # +type: :integer+ with +Integer()+. A type registered with
172
+ # {FieldTypeRegistry} uses that registration.
135
173
  # @option options [Boolean, Proc] :visible Whether the field is visible
136
174
  # (Proc receives the current user)
137
175
  # @option options [Boolean, Proc] :readonly Whether the field is read-only
138
176
  # (Proc receives the current user)
139
- # @option options [String] :label Custom label for the field
140
- # @option options [Array] :options For select fields, the available choices
177
+ # @option options [String] :label No effect on index, show or form labels.
178
+ # Those pages humanize the field name. Import column matching uses it as
179
+ # an extra header candidate.
180
+ # @option options [Array] :choices Choices for a :select field. The form
181
+ # reads :choices. :options is the filter key, not the field key.
182
+ # +field :role, type: :select, options: [...]+ raises +NoMethodError+
183
+ # (+map+ for nil) on new and edit.
141
184
  # @option options [Boolean] :autocomplete For belongs_to, force autocomplete mode
142
- # @option options [Proc] :format Custom formatting proc for display
185
+ # @option options [String] :format strftime pattern for :date and :datetime
186
+ # on the index, the show page and, when the value is a String, the import
187
+ # preview. Defaults are +"%b %d, %Y"+ and +"%b %d, %Y at %l:%M %p"+ (the
188
+ # datetime text is then squished). A Proc is not called. A datetime raises
189
+ # +NoMethodError+ (+TimeWithZone#strftime+ calls +gsub+ on the format). A
190
+ # +Date+ raises +TypeError+. Other types ignore :format.
143
191
  #
144
192
  # @example Basic field configuration
145
193
  # field :email, readonly: true
146
- # field :role, type: :select, options: %w[admin user guest]
194
+ # field :role, type: :select, choices: %w[admin user guest]
147
195
  #
148
196
  # @example Conditional visibility
149
197
  # field :salary, visible: ->(user) { user.admin? || user.hr? }
150
198
  #
151
- # @example Custom formatting
152
- # field :price, format: ->(value) { number_to_currency(value) }
199
+ # @example strftime pattern for a date
200
+ # field :published_on, type: :date, format: "%Y-%m-%d"
201
+ #
202
+ # A belongs_to foreign key such as +field :organization_id+ configures that association field.
153
203
  #
154
204
  # @return [void]
155
205
  def field(name, **options)
@@ -217,32 +267,45 @@ module IronAdmin
217
267
  # @param name [Symbol] The scope name (displayed as tab label)
218
268
  # @param lambda [Proc] A lambda that receives and returns an ActiveRecord::Relation
219
269
  # @param default [Boolean] Whether this scope is selected by default
270
+ # @param count [Boolean] Whether the tab shows how many records this scope
271
+ # would list when scope counts are on. +false+ stores +count: false+,
272
+ # skips that query and draws the name with no number. Any other value,
273
+ # including the default, stores no +:count+ key, and a missing key is
274
+ # counted.
220
275
  #
221
276
  # @example Basic scopes
222
277
  # scope :active, -> { where(active: true) }
223
278
  # scope :recent, -> { where("created_at > ?", 1.week.ago) }, default: true
279
+ # scope :archived, -> { where(archived: true) }, count: false
224
280
  #
225
281
  # @example Scope with complex query
226
282
  # scope :high_value, -> { where("total > ?", 1000).order(total: :desc) }
227
283
  #
228
284
  # @return [void]
229
- def scope(name, lambda, default: false)
230
- self.defined_scopes = defined_scopes + [{ name: name, scope: lambda, default: default }]
285
+ def scope(name, lambda, default: false, count: true)
286
+ definition = { name: name, scope: lambda, default: default }
287
+ definition[:count] = false if count == false
288
+ self.defined_scopes = defined_scopes + [definition]
231
289
  end
232
290
 
233
291
  # Defines a custom action for individual records.
234
292
  #
235
- # Actions appear in the actions dropdown on show and index pages.
236
- # They execute arbitrary code when triggered.
293
+ # Actions appear on the show page. They execute arbitrary code when triggered.
294
+ # An action with form_fields opens its form first: the show page links to it,
295
+ # the form posts to the action, and the action does not run while a required
296
+ # field is blank.
237
297
  #
238
298
  # @param name [Symbol] The action name
299
+ # @param form_fields [Array<IronAdmin::ActionField, Hash>] Inputs of the action's form
239
300
  # @param options [Hash] Configuration options
240
301
  # @option options [String] :icon Heroicon name (e.g., "lock-closed", "trash")
241
- # @option options [Boolean] :confirm Whether to show a confirmation dialog
302
+ # @option options [Boolean] :confirm Whether to show a confirmation dialog (on the form's
303
+ # submit when the action has form_fields)
242
304
  # @option options [String] :confirm_message Custom confirmation message
243
305
  # @option options [Symbol] :method HTTP method (:post, :patch, :delete)
244
- # @yield [record] Block executed when action is triggered
306
+ # @yield [record, params] Block executed when action is triggered
245
307
  # @yieldparam record [ActiveRecord::Base] The record to act upon
308
+ # @yieldparam params [Hash{Symbol => String}] The form's values (two-argument blocks only)
246
309
  #
247
310
  # @example Simple action
248
311
  # action :publish do |record|
@@ -262,17 +325,23 @@ module IronAdmin
262
325
 
263
326
  # Defines a bulk action for multiple selected records.
264
327
  #
265
- # Bulk actions appear in the bulk actions dropdown when records
328
+ # Bulk actions appear in the bulk actions bar when records
266
329
  # are selected on the index page. All records are processed in
267
- # a single database transaction.
330
+ # a single database transaction. A bulk action with form_fields
331
+ # takes the selection to its form, which posts to the action.
268
332
  #
269
333
  # @param name [Symbol] The action name
334
+ # @param form_fields [Array<IronAdmin::ActionField, Hash>] Inputs of the action's form
270
335
  # @param options [Hash] Configuration options
271
336
  # @option options [String] :icon Heroicon name
272
- # @option options [Boolean] :confirm Whether to show a confirmation dialog
273
- # @yield [records] Block executed when action is triggered
337
+ # @option options [Boolean] :confirm Whether to show a confirmation dialog (on the form's
338
+ # submit when the action has form_fields)
339
+ # @option options [String] :confirm_message Custom confirmation message
340
+ # @yield [records, params] Block executed when action is triggered
274
341
  # @yieldparam records [ActiveRecord::Relation] The selected records
275
- # @yieldreturn [Boolean, nil] Return false to rollback the transaction
342
+ # @yieldparam params [Hash{Symbol => String}] The form's values (two-argument blocks only)
343
+ # @yieldreturn [Boolean, nil] +false+ rolls the transaction back.
344
+ # Use +next false+. +return false+ raises +LocalJumpError+.
276
345
  #
277
346
  # @example Bulk deactivate
278
347
  # bulk_action :deactivate do |records|
@@ -281,7 +350,7 @@ module IronAdmin
281
350
  #
282
351
  # @example Bulk action with rollback on failure
283
352
  # bulk_action :process do |records|
284
- # return false unless ExternalService.process(records)
353
+ # next false unless ExternalService.process(records)
285
354
  # end
286
355
  #
287
356
  # @return [void]
@@ -342,7 +411,8 @@ module IronAdmin
342
411
  #
343
412
  # @param options [Hash] Menu configuration options
344
413
  # @option options [String] :icon Heroicon name for the menu item
345
- # @option options [String] :label Custom label (defaults to pluralized model name)
414
+ # @option options [String] :label Ignored for a resource. The sidebar uses
415
+ # {.label}, the pluralized model name. A tool's menu +label:+ is used.
346
416
  # @option options [Integer] :priority Sort order (lower = higher in menu)
347
417
  # @option options [String] :group Group name for the sidebar section
348
418
  # (sibling resources sharing the same `:group` are listed together;
@@ -351,7 +421,7 @@ module IronAdmin
351
421
  # compatibility with earlier docs.
352
422
  #
353
423
  # @example
354
- # menu icon: "users", label: "Team Members", priority: 10, group: "People"
424
+ # menu icon: "users", priority: 10, group: "People"
355
425
  #
356
426
  # @return [void]
357
427
  def menu(**options)
@@ -369,7 +439,8 @@ module IronAdmin
369
439
  # Defines the authorization policy for this resource.
370
440
  #
371
441
  # Policies control which actions users can perform. Without a policy,
372
- # all actions are allowed by default.
442
+ # {Configuration#default_policy} decides: +:allow+ (the default) allows
443
+ # every action, +:deny+ denies them all.
373
444
  #
374
445
  # @yield Block that configures the policy using the Policy DSL
375
446
  #
@@ -438,18 +509,31 @@ module IronAdmin
438
509
  return false unless action_allowed?(action)
439
510
 
440
511
  policy = resource_policy
441
- return true unless policy
512
+ return IronAdmin.configuration.default_allow? unless policy
442
513
 
443
514
  policy.allowed?(action, user)
444
515
  end
445
516
 
446
- # Searchable columns the user is allowed to see. Secret fields
447
- # ({Field::SECRET_TYPES}) are left out, even when {.searchable} lists them.
517
+ # Checks a custom or bulk action against the resource policy. Without
518
+ # a policy, {Configuration#default_policy} decides.
519
+ #
520
+ # @param action_name [Symbol, String] the action's name
521
+ # @param user [Object, nil] Current user passed to policy conditions
522
+ # @return [Boolean]
523
+ def custom_action_allowed?(action_name, user)
524
+ policy = resource_policy
525
+ return IronAdmin.configuration.default_allow? unless policy
526
+
527
+ policy.action_allowed?(action_name, user)
528
+ end
529
+
530
+ # Searchable columns the user is allowed to see. Concealed fields
531
+ # are left out, even when {.searchable} lists them.
448
532
  #
449
533
  # @param user [Object, nil]
450
534
  # @return [Array<Symbol>]
451
535
  def visible_searchable_columns(user)
452
- visible_names = resolved_fields.select { |field| field.visible?(user) && !field.secret? }.map(&:name)
536
+ visible_names = resolved_fields.reject { |field| field.concealed_from?(user) }.map(&:name)
453
537
  searchable_columns.select { |column| visible_names.include?(column.to_sym) }
454
538
  end
455
539
 
@@ -506,21 +590,6 @@ module IronAdmin
506
590
  self.defined_associations = defined_associations.merge(name => { kind: :belongs_to, **options })
507
591
  end
508
592
 
509
- # Declares a has_many association for this resource.
510
- #
511
- # Has_many associations appear as related lists on the show page.
512
- #
513
- # @param name [Symbol] The association name
514
- # @param options [Hash] Configuration options
515
- # @option options [Class] :resource The associated resource class
516
- # @option options [Array<Symbol>] :fields Fields to display in the related list
517
- #
518
- # @example
519
- # has_many :orders
520
- # has_many :comments, fields: [:id, :body, :created_at]
521
- #
522
- # has_many and has_one are defined in Concerns::Nestable
523
-
524
593
  # Declares a has_and_belongs_to_many association for this resource.
525
594
  #
526
595
  # HABTM associations appear as multi-select checkboxes on forms and
@@ -542,24 +611,43 @@ module IronAdmin
542
611
  #
543
612
  # Merges auto-inferred fields from the database schema with
544
613
  # any customizations made via {.field} and association declarations.
614
+ # For a belongs_to field, merge association options, then an override
615
+ # registered under its foreign key, then an override registered under
616
+ # the association name. Later entries win.
545
617
  #
546
618
  # @return [Array<IronAdmin::Field>] Configured field objects
547
619
  def resolved_fields
548
- inferred = FieldInferrer.call(adapter)
549
-
550
- inferred.map do |field|
551
- overrides = field_overrides[field.name] || {}
552
- assoc_overrides = defined_associations[field.name] || {}
553
- merged = assoc_overrides.except(:kind).merge(overrides)
620
+ FieldInferrer.call(adapter).map do |field|
621
+ assoc_options = (defined_associations[field.name] || {}).except(:kind)
622
+ fk_name = field.type == :belongs_to ? field.options[:foreign_key] : nil
623
+ fk_options = fk_name ? (field_overrides[fk_name] || {}) : {}
624
+ name_options = field_overrides[field.name] || {}
625
+ merged = assoc_options.merge(fk_options).merge(name_options)
554
626
 
555
627
  if merged.any?
556
- Field.new(field.name, type: field.type, **field.options, **merged)
628
+ field.merge(**merged)
557
629
  else
558
630
  field
559
631
  end
560
632
  end
561
633
  end
562
634
 
635
+ # Names given to {.field} that match neither a resolved field nor the
636
+ # foreign key of a belongs_to field, in declaration order.
637
+ #
638
+ # @return [Array<Symbol>] override names whose options are ignored
639
+ def unresolved_field_overrides
640
+ known = resolved_fields.flat_map do |field|
641
+ if field.type == :belongs_to
642
+ [field.name, field.options[:foreign_key]]
643
+ else
644
+ [field.name]
645
+ end
646
+ end
647
+
648
+ field_overrides.keys.reject { |name| known.include?(name) }
649
+ end
650
+
563
651
  # Returns associations to preload for index/show queries.
564
652
  #
565
653
  # By default, preloads all belongs_to associations to avoid N+1 queries.
@@ -634,10 +722,39 @@ module IronAdmin
634
722
  end
635
723
  end
636
724
 
637
- # Returns the URL-friendly resource name (pluralized model name).
725
+ # Returns the URL segment for this resource.
726
+ #
727
+ # A resource whose model is the one its class name implies
728
+ # (`UserResource` → `User`, `Billing::InvoiceResource` →
729
+ # `Billing::Invoice`) uses the model's plural, so those names and URLs
730
+ # stay. A {.model_class_override} of that same model is the same case,
731
+ # including when the model has a custom `model_name`. A
732
+ # {.model_class_override} of a different model, an overridden
733
+ # `self.model`, or a class name that implies no model derives the
734
+ # segment from the full class path under `IronAdmin::Resources`
735
+ # (`AutoNoteResource` is "auto_notes", `Shop::NoteResource` is
736
+ # "shop_notes", `Archive::NoteResource` is "archive_notes"). An HTTP
737
+ # adapter resource without an override keeps the adapter's name.
738
+ # An anonymous class, which has no name to derive from, uses the model's
739
+ # plural. A subclass that defines `self.resource_name` keeps that name.
740
+ # There is no writer.
741
+ #
742
+ # @example Choose a name
743
+ # module IronAdmin
744
+ # module Resources
745
+ # class ArchivedNoteResource < IronAdmin::Resource
746
+ # self.model_class_override = ::Note
747
+ # def self.resource_name = "archived_notes"
748
+ # end
749
+ # end
750
+ # end
638
751
  #
639
- # @return [String] The resource name (e.g., "users", "orders")
640
- delegate :resource_name, to: :adapter
752
+ # @return [String] The resource name (e.g., "users", "auto_notes", "shop_notes")
753
+ def resource_name
754
+ return adapter.resource_name if uses_model_resource_name?
755
+
756
+ class_derived_resource_name
757
+ end
641
758
 
642
759
  # Returns the human-readable label for this resource.
643
760
  #
@@ -660,6 +777,30 @@ module IronAdmin
660
777
 
661
778
  private
662
779
 
780
+ # True when the segment is the model's plural rather than the class path.
781
+ def uses_model_resource_name?
782
+ return true if name.nil?
783
+ return true if adapter_class == :http && !model_class_override && !model_method_overridden?
784
+ return false if (model_class_override && model_class_override != implied_model_class) || model_method_overridden?
785
+
786
+ !implied_model_class.nil?
787
+ end
788
+
789
+ def model_method_overridden?
790
+ method(:model).owner != IronAdmin::Resource.singleton_class
791
+ end
792
+
793
+ # The constant named by this resource class, or nil when there is none.
794
+ def implied_model_class
795
+ name.sub(/Resource\z/, "").sub(/\AIronAdmin::Resources::/, "").constantize
796
+ rescue NameError
797
+ nil
798
+ end
799
+
800
+ def class_derived_resource_name
801
+ name.delete_prefix("IronAdmin::Resources::").delete_suffix("Resource").underscore.tr("/", "_").pluralize
802
+ end
803
+
663
804
  # Resolves the adapter class from a symbol or class.
664
805
  # Symbols are resolved via the Adapters::Registry (lazy loading).
665
806
  # Classes are used directly (for custom adapters).
@@ -32,16 +32,27 @@ module IronAdmin
32
32
  # is swallowed and {.finalize!} re-runs with the correct adapter
33
33
  # after the class body has finished evaluating.
34
34
  #
35
+ # A class whose name equals the name of a class already stored is a
36
+ # reloaded version of it: this replaces that entry in the remembered
37
+ # list and in the registry, and it is not a collision. Two different
38
+ # classes that compute the same resource name keep the first one
39
+ # registered. The other stays out of the registry, and {.finalize!}
40
+ # logs that collision once. Before 0.8 the last class registered
41
+ # replaced the previous one. The registry key is computed before the
42
+ # class is remembered, so a class that has no key (an anonymous class
43
+ # whose model cannot be named) raises here and is not remembered.
44
+ #
35
45
  # @param resource_class [Class] The resource class to register
36
46
  # @return [Class] The registered resource class
37
47
  def register(resource_class)
38
- # Stage the registry key first so the resource is discoverable
39
- # even if `register_soft_delete_features` raises. We use a key
40
- # derived from the class name (no adapter/model lookup) so HTTP
41
- # resources without a Ruby model class — and any other case where
42
- # `resource_name` would touch a not-yet-loaded adapter — still
43
- # land in the registry.
44
- resources[registry_key_for(resource_class)] = resource_class
48
+ # Compute the key before remembering the class. An anonymous class
49
+ # with no model raises here; remembering it first would make every
50
+ # later {.finalize!} raise inside {#rekey_resources!}. The key is
51
+ # provisional until {.finalize!}: `inherited` runs before the
52
+ # class body sets `model_class_override` and `resource_name`.
53
+ registry_key_for(resource_class)
54
+ remember_resource(resource_class)
55
+ store_resource(resource_class)
45
56
  begin
46
57
  resource_class.register_soft_delete_features
47
58
  rescue StandardError
@@ -75,8 +86,15 @@ module IronAdmin
75
86
  # and Mongoid/HTTP resources would 500 with `undefined method
76
87
  # 'columns' for <MongoidModel>`.
77
88
  #
89
+ # The registry is also re-keyed by each resource's final
90
+ # {Resource.resource_name}. A reloaded class (the same name) already
91
+ # replaced its stale entry in {.register}. Two different classes that
92
+ # still share a name keep the first one registered; the other is
93
+ # logged once here and left out.
94
+ #
78
95
  # @return [void]
79
96
  def finalize!
97
+ rekey_resources!
80
98
  all.each do |resource_class|
81
99
  resource_class.instance_variable_set(:@adapter, nil)
82
100
  resource_class.register_soft_delete_features
@@ -136,6 +154,7 @@ module IronAdmin
136
154
  # @return [void]
137
155
  def reset!
138
156
  @resources = {}
157
+ @registered_classes = []
139
158
  end
140
159
 
141
160
  private
@@ -144,15 +163,69 @@ module IronAdmin
144
163
  @resources ||= {}
145
164
  end
146
165
 
147
- # Builds the registry key without touching the adapter or model.
148
- # Falls back from `resource_class.resource_name` (which delegates
149
- # to the adapter and may raise on HTTP/Mongoid resources before
150
- # `to_prepare` finalizes the lifecycle) to a name derived purely
151
- # from the class identifier.
166
+ def registered_classes
167
+ @registered_classes ||= []
168
+ end
169
+
170
+ def remember_resource(resource_class)
171
+ index = registered_classes.index { |stored| same_registered_class?(stored, resource_class) }
172
+ if index
173
+ registered_classes[index] = resource_class
174
+ else
175
+ registered_classes << resource_class
176
+ end
177
+ end
178
+
179
+ # A reloaded class (the same name) replaces its previous entry, including
180
+ # a move to a new key. A different class leaves the first occupant of the
181
+ # key in place and stays in {#registered_classes} so {.finalize!} can
182
+ # place it once its resource name is final.
183
+ def store_resource(resource_class)
184
+ key = registry_key_for(resource_class)
185
+ resources.delete_if { |_stored_key, stored| same_registered_class?(stored, resource_class) }
186
+ resources[key] ||= resource_class
187
+ end
188
+
189
+ # Same object, or a reload of it: another class object with the same name.
190
+ # Anonymous classes have no name, so only the same object matches.
191
+ def same_registered_class?(stored, resource_class)
192
+ stored.equal?(resource_class) || (resource_class.name && stored.name == resource_class.name)
193
+ end
194
+
195
+ def rekey_resources!
196
+ rebuilt = {}
197
+ registered_classes.each do |resource_class|
198
+ key = registry_key_for(resource_class)
199
+ occupant = rebuilt[key]
200
+ if occupant && occupant != resource_class
201
+ log_registry_collision(occupant, resource_class, key)
202
+ else
203
+ rebuilt[key] = resource_class
204
+ end
205
+ end
206
+ @resources = rebuilt
207
+ end
208
+
209
+ def log_registry_collision(kept, skipped, key)
210
+ return unless defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
211
+
212
+ Rails.logger.error(
213
+ "[IronAdmin] #{skipped.name} was not registered under \"#{key}\" because " \
214
+ "#{kept.name} already uses that resource name. " \
215
+ "Define def self.resource_name = \"a_unique_name\" on one of them."
216
+ )
217
+ end
218
+
219
+ # Builds the registry key. `resource_name` may raise before the class
220
+ # body has set the adapter, so fall back to the class path. The
221
+ # derivation matches {Resource.resource_name}.
152
222
  def registry_key_for(resource_class)
153
223
  resource_class.resource_name
154
224
  rescue StandardError
155
- resource_class.name.demodulize.sub(/Resource\z/, "").underscore.pluralize
225
+ name = resource_class.name
226
+ raise if name.nil?
227
+
228
+ name.delete_prefix("IronAdmin::Resources::").delete_suffix("Resource").underscore.tr("/", "_").pluralize
156
229
  end
157
230
 
158
231
  def warn_finalize_failure(resource_class, error)