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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +129 -1
- data/README.md +48 -1
- data/UPGRADING.md +495 -8
- data/app/assets/tailwind/iron_admin/engine.css +79 -4
- data/app/components/iron_admin/dashboards/activity_feed_component/item_component.html.haml +6 -9
- data/app/components/iron_admin/dashboards/activity_feed_component.html.haml +6 -5
- data/app/components/iron_admin/dashboards/activity_feed_component.rb +21 -24
- data/app/components/iron_admin/dashboards/chart_component.html.haml +3 -5
- data/app/components/iron_admin/dashboards/chart_component.rb +86 -24
- data/app/components/iron_admin/dashboards/concerns/themed_card.rb +39 -0
- data/app/components/iron_admin/dashboards/metric_card_component.html.haml +8 -7
- data/app/components/iron_admin/dashboards/metric_card_component.rb +2 -0
- data/app/components/iron_admin/dashboards/progress_component.html.haml +11 -7
- data/app/components/iron_admin/dashboards/progress_component.rb +11 -6
- data/app/components/iron_admin/dashboards/quick_links_component/link_component.html.haml +8 -9
- data/app/components/iron_admin/dashboards/quick_links_component.html.haml +6 -5
- data/app/components/iron_admin/dashboards/quick_links_component.rb +4 -12
- data/app/components/iron_admin/dashboards/recent_table_component.html.haml +15 -10
- data/app/components/iron_admin/dashboards/recent_table_component.rb +49 -0
- data/app/components/iron_admin/dashboards/stats_grid_component/stat_component.html.haml +13 -14
- data/app/components/iron_admin/dashboards/stats_grid_component.html.haml +1 -0
- data/app/components/iron_admin/dashboards/stats_grid_component.rb +9 -11
- data/app/components/iron_admin/filters/bar_component.html.haml +8 -6
- data/app/components/iron_admin/filters/bar_component.rb +56 -9
- data/app/components/iron_admin/filters/control_styles.rb +82 -0
- data/app/components/iron_admin/filters/date_range_component.html.haml +9 -4
- data/app/components/iron_admin/filters/date_range_component.rb +10 -6
- data/app/components/iron_admin/filters/search_component.html.haml +4 -4
- data/app/components/iron_admin/filters/search_component.rb +3 -5
- data/app/components/iron_admin/filters/select_filter_component.html.haml +1 -1
- data/app/components/iron_admin/filters/select_filter_component.rb +5 -16
- data/app/components/iron_admin/form/belongs_to_autocomplete_component.html.haml +9 -5
- data/app/components/iron_admin/form/belongs_to_autocomplete_component.rb +10 -8
- data/app/components/iron_admin/form/belongs_to_component.html.haml +2 -2
- data/app/components/iron_admin/form/belongs_to_component.rb +4 -18
- data/app/components/iron_admin/form/checkbox_component.rb +2 -3
- data/app/components/iron_admin/form/concerns/form_input_behavior.rb +1 -1
- data/app/components/iron_admin/form/control_classes.rb +108 -0
- data/app/components/iron_admin/form/date_picker_component.rb +3 -7
- data/app/components/iron_admin/form/field_wrapper_component.html.haml +3 -3
- data/app/components/iron_admin/form/field_wrapper_component.rb +3 -3
- data/app/components/iron_admin/form/nested_form_component.html.haml +4 -5
- data/app/components/iron_admin/form/nested_form_component.rb +12 -2
- data/app/components/iron_admin/form/select_component.html.haml +1 -1
- data/app/components/iron_admin/form/select_component.rb +2 -16
- data/app/components/iron_admin/form/text_input_component.rb +1 -5
- data/app/components/iron_admin/form/textarea_component.rb +1 -5
- data/app/components/iron_admin/layout/flash_component.html.haml +1 -1
- data/app/components/iron_admin/layout/flash_component.rb +4 -3
- data/app/components/iron_admin/layout/navbar_component.html.haml +15 -9
- data/app/components/iron_admin/layout/navbar_component.rb +29 -1
- data/app/components/iron_admin/layout/shell_component.html.haml +4 -4
- data/app/components/iron_admin/layout/shell_component.rb +16 -3
- data/app/components/iron_admin/layout/sidebar_component.html.haml +21 -25
- data/app/components/iron_admin/layout/sidebar_component.rb +76 -1
- data/app/components/iron_admin/layout/theme_switcher_component.html.haml +15 -0
- data/app/components/iron_admin/layout/theme_switcher_component.rb +74 -0
- data/app/components/iron_admin/resources/actions_component.html.haml +15 -8
- data/app/components/iron_admin/resources/actions_component.rb +34 -2
- data/app/components/iron_admin/resources/breadcrumb_component.html.haml +3 -3
- data/app/components/iron_admin/resources/breadcrumb_component.rb +8 -7
- data/app/components/iron_admin/resources/bulk_actions_component.html.haml +9 -8
- data/app/components/iron_admin/resources/bulk_actions_component.rb +47 -4
- data/app/components/iron_admin/resources/data_table_component.html.haml +15 -15
- data/app/components/iron_admin/resources/data_table_component.rb +34 -3
- data/app/components/iron_admin/resources/related_list_component.html.haml +7 -7
- data/app/components/iron_admin/resources/related_list_component.rb +26 -2
- data/app/components/iron_admin/resources/show_field_component.html.haml +3 -3
- data/app/components/iron_admin/resources/show_field_component.rb +10 -4
- data/app/components/iron_admin/resources/styles.rb +136 -0
- data/app/components/iron_admin/ui/alert_component.html.haml +2 -2
- data/app/components/iron_admin/ui/alert_component.rb +27 -31
- data/app/components/iron_admin/ui/badge_component.rb +40 -20
- data/app/components/iron_admin/ui/button_component.rb +37 -11
- data/app/components/iron_admin/ui/card_component.rb +3 -3
- data/app/components/iron_admin/ui/dropdown_component.html.haml +1 -1
- data/app/components/iron_admin/ui/dropdown_component.rb +34 -13
- data/app/components/iron_admin/ui/empty_state_component.html.haml +4 -5
- data/app/components/iron_admin/ui/empty_state_component.rb +2 -2
- data/app/components/iron_admin/ui/modal_component.html.haml +5 -5
- data/app/components/iron_admin/ui/modal_component.rb +6 -4
- data/app/components/iron_admin/ui/pagination_component.html.haml +14 -14
- data/app/components/iron_admin/ui/pagination_component.rb +26 -9
- data/app/components/iron_admin/ui/scopes_component.html.haml +2 -2
- data/app/components/iron_admin/ui/scopes_component.rb +44 -11
- data/app/components/iron_admin/ui/tooltip_component.rb +11 -4
- data/app/controllers/iron_admin/application_controller.rb +58 -4
- data/app/controllers/iron_admin/audit_controller.rb +30 -6
- data/app/controllers/iron_admin/concerns/action_executable.rb +37 -0
- data/app/controllers/iron_admin/concerns/assignable_references.rb +149 -0
- data/app/controllers/iron_admin/concerns/authorizable.rb +11 -0
- data/app/controllers/iron_admin/concerns/blank_password_filterable.rb +21 -0
- data/app/controllers/iron_admin/concerns/event_emittable.rb +47 -7
- data/app/controllers/iron_admin/concerns/filterable.rb +34 -9
- data/app/controllers/iron_admin/concerns/nested_permittable.rb +5 -3
- data/app/controllers/iron_admin/concerns/nested_tenant_check.rb +159 -0
- data/app/controllers/iron_admin/concerns/polymorphic_references.rb +131 -0
- data/app/controllers/iron_admin/concerns/scopeable.rb +54 -7
- data/app/controllers/iron_admin/concerns/searchable.rb +3 -3
- data/app/controllers/iron_admin/concerns/tenant_assignable.rb +118 -0
- data/app/controllers/iron_admin/concerns/tenant_containment.rb +146 -0
- data/app/controllers/iron_admin/exports_controller.rb +2 -2
- data/app/controllers/iron_admin/imports_controller.rb +1 -1
- data/app/controllers/iron_admin/resources_controller.rb +48 -41
- data/app/controllers/iron_admin/search_controller.rb +1 -2
- data/app/controllers/iron_admin/themes_controller.rb +50 -0
- data/app/controllers/iron_admin/tools_controller.rb +58 -5
- data/app/helpers/iron_admin/action_form_helper.rb +19 -0
- data/app/helpers/iron_admin/application_helper.rb +93 -16
- data/app/helpers/iron_admin/assignable_records_helper.rb +32 -0
- data/app/helpers/iron_admin/component_overrides_helper.rb +9 -4
- data/app/helpers/iron_admin/field_display_helper.rb +88 -43
- data/app/helpers/iron_admin/related_records_helper.rb +51 -0
- data/app/helpers/iron_admin/theme_helper.rb +56 -46
- data/app/javascript/iron_admin/controllers/cp_bulk_select_controller.js +12 -0
- data/app/javascript/iron_admin/controllers/cp_chart_controller.js +4 -0
- data/app/javascript/iron_admin/controllers/cp_disclosure_controller.js +19 -0
- data/app/javascript/iron_admin/controllers/cp_search_shortcut_controller.js +39 -0
- data/app/javascript/iron_admin/index.js +4 -0
- data/app/views/iron_admin/audit/index.html.haml +78 -81
- data/app/views/iron_admin/dashboard/index.html.haml +10 -9
- data/app/views/iron_admin/form/_action_field.html.haml +50 -0
- data/app/views/iron_admin/form/_nested_row.html.haml +18 -13
- data/app/views/iron_admin/imports/new.html.haml +14 -13
- data/app/views/iron_admin/imports/preview.html.haml +24 -21
- data/app/views/iron_admin/resources/_form.html.haml +70 -68
- data/app/views/iron_admin/resources/action_form.html.haml +16 -59
- data/app/views/iron_admin/resources/bulk_action_form.html.haml +20 -58
- data/app/views/iron_admin/resources/edit.html.haml +3 -3
- data/app/views/iron_admin/resources/index.html.haml +87 -74
- data/app/views/iron_admin/resources/new.html.haml +2 -2
- data/app/views/iron_admin/resources/show.html.haml +53 -41
- data/app/views/iron_admin/search/index.html.haml +17 -9
- data/app/views/iron_admin/shared/_pagination.html.haml +13 -10
- data/app/views/iron_admin/shared/error.html.haml +16 -12
- data/app/views/iron_admin/tools/action_form.html.haml +9 -55
- data/app/views/iron_admin/tools/show.html.haml +4 -5
- data/app/views/layouts/iron_admin/_document_head.html.haml +24 -0
- data/app/views/layouts/iron_admin/application.html.haml +4 -17
- data/app/views/layouts/iron_admin/minimal.html.haml +7 -0
- data/config/importmap.rb +2 -0
- data/config/locales/en.yml +23 -0
- data/config/routes.rb +1 -0
- data/docs/FAQ.md +87 -37
- data/docs/README.md +8 -2
- data/docs/components/README.md +6 -2
- data/docs/components/dashboard-components.md +24 -35
- data/docs/components/filter-components.md +44 -92
- data/docs/components/form-components.md +16 -32
- data/docs/components/resource-components.md +46 -27
- data/docs/components/ui-components.md +44 -26
- data/docs/getting-started/installation.md +5 -1
- data/docs/getting-started/quick-start.md +36 -23
- data/docs/guides/authentication.md +182 -42
- data/docs/guides/components.md +97 -15
- data/docs/guides/custom-adapters.md +24 -4
- data/docs/guides/dashboard.md +68 -44
- data/docs/guides/export.md +24 -8
- data/docs/guides/extending.md +206 -51
- data/docs/guides/fields.md +229 -70
- data/docs/guides/imports.md +33 -18
- data/docs/guides/resources.md +333 -111
- data/docs/guides/theming.md +930 -58
- data/docs/guides/troubleshooting.md +7 -3
- data/docs/plugin-system-design.md +8 -7
- data/docs/reference/configuration.md +51 -16
- data/docs/reference/routes.md +1 -0
- data/docs/reference/theme-properties.md +101 -38
- data/lib/generators/iron_admin/install/templates/initializer.rb.tt +7 -2
- data/lib/generators/iron_admin/install_audit/templates/create_iron_admin_audit_entries.rb.tt +2 -0
- data/lib/iron_admin/action_field.rb +11 -0
- data/lib/iron_admin/adapters/active_record.rb +95 -0
- data/lib/iron_admin/adapters/base.rb +107 -0
- data/lib/iron_admin/adapters/http/query.rb +21 -0
- data/lib/iron_admin/adapters/http.rb +84 -4
- data/lib/iron_admin/adapters/mongoid.rb +96 -2
- data/lib/iron_admin/audit_log.rb +64 -6
- data/lib/iron_admin/authentication_warning.rb +31 -0
- data/lib/iron_admin/concerns/filter_definable.rb +15 -1
- data/lib/iron_admin/concerns/nestable.rb +25 -0
- data/lib/iron_admin/concerns/scope_countable.rb +56 -0
- data/lib/iron_admin/concerns/soft_deletable.rb +28 -9
- data/lib/iron_admin/configuration/components.rb +14 -3
- data/lib/iron_admin/configuration/theme/neutral_defaults.rb +179 -0
- data/lib/iron_admin/configuration/theme.rb +292 -87
- data/lib/iron_admin/configuration.rb +340 -45
- data/lib/iron_admin/dashboard.rb +22 -38
- data/lib/iron_admin/engine.rb +16 -2
- data/lib/iron_admin/errors.rb +3 -0
- data/lib/iron_admin/field.rb +78 -17
- data/lib/iron_admin/field_inferrer.rb +61 -3
- data/lib/iron_admin/field_override_warning.rb +67 -0
- data/lib/iron_admin/import/import_preview.rb +56 -7
- data/lib/iron_admin/import/importer.rb +91 -14
- data/lib/iron_admin/import/tenant_precheck.rb +67 -0
- data/lib/iron_admin/plugin/registration.rb +64 -0
- data/lib/iron_admin/plugin_registry.rb +3 -2
- data/lib/iron_admin/policy.rb +22 -12
- data/lib/iron_admin/policy_coverage_warning.rb +46 -0
- data/lib/iron_admin/request_context.rb +18 -0
- data/lib/iron_admin/resource.rb +218 -77
- data/lib/iron_admin/resource_registry.rb +86 -13
- data/lib/iron_admin/scope_counts.rb +73 -0
- data/lib/iron_admin/tenant_scope.rb +30 -0
- data/lib/iron_admin/tool.rb +61 -12
- data/lib/iron_admin/tool_context.rb +10 -6
- data/lib/iron_admin/version.rb +1 -1
- data/lib/iron_admin.rb +29 -2
- metadata +37 -7
|
@@ -21,6 +21,11 @@ module IronAdmin
|
|
|
21
21
|
# Field types that show compact summaries on index pages.
|
|
22
22
|
INDEX_COMPACT_TYPES = %i[key_value boolean_group].freeze
|
|
23
23
|
|
|
24
|
+
# Stroke width of a 24px outline heroicon drawn at 16px (h-4 w-4), as in the sidebar and
|
|
25
|
+
# the dashboard's metric cards: its own 1.5 would come out as a blurred 1px line.
|
|
26
|
+
# @return [String]
|
|
27
|
+
SMALL_ICON_STROKE = "1.75"
|
|
28
|
+
|
|
24
29
|
# Maps field types to their display method names.
|
|
25
30
|
# @return [Hash{Symbol => Symbol}]
|
|
26
31
|
FIELD_DISPLAY_METHODS = {
|
|
@@ -65,14 +70,17 @@ module IronAdmin
|
|
|
65
70
|
end
|
|
66
71
|
end
|
|
67
72
|
|
|
68
|
-
# Displays a field value for index pages with text truncation.
|
|
73
|
+
# Displays a field value for index pages with text truncation. Rich text
|
|
74
|
+
# shows as truncated plain text, never as HTML.
|
|
69
75
|
#
|
|
70
76
|
# @param record [ActiveRecord::Base] The record
|
|
71
77
|
# @param field [IronAdmin::Field] Field configuration
|
|
72
|
-
# @return [String, nil] Formatted value (truncated for text fields)
|
|
78
|
+
# @return [String, nil] Formatted value (truncated for text and rich text fields)
|
|
73
79
|
def display_index_field_value(record, field)
|
|
74
80
|
if TRUNCATABLE_TYPES.include?(field.type)
|
|
75
81
|
display_truncated_index_value(record, field)
|
|
82
|
+
elsif field.type == :rich_text
|
|
83
|
+
display_index_rich_text(record, field)
|
|
76
84
|
elsif INDEX_COMPACT_TYPES.include?(field.type)
|
|
77
85
|
display_compact_field_value(record, field)
|
|
78
86
|
elsif (custom = IronAdmin::FieldTypeRegistry.find(field.type))
|
|
@@ -99,25 +107,23 @@ module IronAdmin
|
|
|
99
107
|
"#{record.class.model_name.human} ##{record.id}"
|
|
100
108
|
end
|
|
101
109
|
|
|
102
|
-
# Returns options for a filter select dropdown.
|
|
110
|
+
# Returns options for a filter select dropdown, evaluated when the page renders.
|
|
111
|
+
#
|
|
112
|
+
# A +:select+ filter offers, in order of precedence: its +options+ (an Array,
|
|
113
|
+
# or a callable that takes no arguments and returns one), the enum values of
|
|
114
|
+
# its column, the records of the +belongs_to+ association whose foreign key
|
|
115
|
+
# it filters (labelled and scoped like the form's belongs_to select, valued by
|
|
116
|
+
# the key the foreign key stores), or the column's distinct values. A foreign
|
|
117
|
+
# key filter keeps the column's distinct values when the +belongs_to+ field
|
|
118
|
+
# is not visible to the current user or is declared +autocomplete: true+.
|
|
103
119
|
#
|
|
104
120
|
# @param resource_class [Class] The resource class
|
|
105
121
|
# @param filter [Hash] Filter configuration
|
|
106
|
-
# @return [Array<Array(String,
|
|
122
|
+
# @return [Array<Array(String, Object)>] [label, value] pairs for the select tag
|
|
107
123
|
def filter_options_for(resource_class, filter)
|
|
108
|
-
resource_adapter = resource_class.adapter
|
|
109
|
-
column_name = filter[:name].to_s
|
|
110
|
-
|
|
111
124
|
case filter[:type]
|
|
112
125
|
when :select
|
|
113
|
-
|
|
114
|
-
if filter[:options]
|
|
115
|
-
normalize_filter_options(filter[:options])
|
|
116
|
-
elsif enums.key?(column_name)
|
|
117
|
-
enums[column_name].keys.map { |k| [k.humanize, k] }
|
|
118
|
-
else
|
|
119
|
-
resource_adapter.distinct_values(column_name).map { |v| [v.to_s.humanize, v] }
|
|
120
|
-
end
|
|
126
|
+
select_filter_options(resource_class, filter)
|
|
121
127
|
when :boolean
|
|
122
128
|
[[I18n.t("iron_admin.filters.true"), "true"], [I18n.t("iron_admin.filters.false"), "false"]]
|
|
123
129
|
else
|
|
@@ -125,8 +131,10 @@ module IronAdmin
|
|
|
125
131
|
end
|
|
126
132
|
end
|
|
127
133
|
|
|
134
|
+
# @param options [Array, nil] Labels with values, or bare values labelled by humanizing them
|
|
135
|
+
# @return [Array<Array(String, Object)>] [label, value] pairs
|
|
128
136
|
def normalize_filter_options(options)
|
|
129
|
-
options.map do |option|
|
|
137
|
+
Array(options).map do |option|
|
|
130
138
|
option.is_a?(Array) ? option : [option.to_s.humanize, option]
|
|
131
139
|
end
|
|
132
140
|
end
|
|
@@ -138,5 +146,74 @@ module IronAdmin
|
|
|
138
146
|
|
|
139
147
|
IronAdmin::ResourceRegistry.find(association_class.model_name.plural)
|
|
140
148
|
end
|
|
149
|
+
|
|
150
|
+
private
|
|
151
|
+
|
|
152
|
+
def select_filter_options(resource_class, filter)
|
|
153
|
+
options = filter[:options]
|
|
154
|
+
return normalize_filter_options(options.respond_to?(:call) ? options.call : options) if options
|
|
155
|
+
|
|
156
|
+
column_name = filter[:name].to_s
|
|
157
|
+
enum_values = resource_class.adapter.enums[column_name]
|
|
158
|
+
return enum_values.keys.map { |key| [key.humanize, key] } if enum_values
|
|
159
|
+
|
|
160
|
+
belongs_to_filter_options(resource_class, column_name) ||
|
|
161
|
+
resource_class.adapter.distinct_values(column_name).map { |value| [value.to_s.humanize, value] }
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Labelled options for the associated records some row refers to, when
|
|
165
|
+
# +column_name+ is the foreign key of a (non-polymorphic) belongs_to
|
|
166
|
+
# association; nil otherwise.
|
|
167
|
+
def belongs_to_filter_options(resource_class, column_name)
|
|
168
|
+
association = resource_class.adapter.associations(:belongs_to).find do |candidate|
|
|
169
|
+
!candidate.polymorphic? && candidate.foreign_key.to_s == column_name
|
|
170
|
+
end
|
|
171
|
+
association_class = association&.klass
|
|
172
|
+
return unless association_class
|
|
173
|
+
|
|
174
|
+
field = belongs_to_filter_field(resource_class, column_name)
|
|
175
|
+
return unless labels_foreign_key_filter?(field)
|
|
176
|
+
|
|
177
|
+
value_key = foreign_key_target(association)
|
|
178
|
+
scope = referenced_records(association_class, value_key, resource_class.adapter.distinct_values(column_name))
|
|
179
|
+
scope = IronAdmin::TenantScope.apply(scope)
|
|
180
|
+
scope.map { |record| [display_record_label(record, field.options[:display]), record.public_send(value_key)] }
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# The associated records whose +value_key+ is one of +values+: only the
|
|
184
|
+
# records some row refers to, whose labels the index already shows.
|
|
185
|
+
def referenced_records(association_class, value_key, values)
|
|
186
|
+
scope = association_class.all
|
|
187
|
+
scope.respond_to?(:in) ? scope.in(value_key => values) : scope.where(value_key => values)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def belongs_to_filter_field(resource_class, column_name)
|
|
191
|
+
resource_class.resolved_fields.find do |candidate|
|
|
192
|
+
candidate.type == :belongs_to && candidate.options[:foreign_key].to_s == column_name
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Labels need a belongs_to field the viewer can see; an +autocomplete+ field
|
|
197
|
+
# marks a table too large to list. Otherwise the filter keeps the column's
|
|
198
|
+
# distinct values, as in 0.7.0.
|
|
199
|
+
def labels_foreign_key_filter?(field)
|
|
200
|
+
return false unless field
|
|
201
|
+
|
|
202
|
+
field.visible?(filter_options_viewer) && !field.options[:autocomplete]
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# The current admin user, or nil where the view has no IronAdmin controller
|
|
206
|
+
# behind it (such as a component or helper rendered in isolation).
|
|
207
|
+
def filter_options_viewer
|
|
208
|
+
iron_admin_current_user if respond_to?(:iron_admin_current_user)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# The attribute of the associated record that the foreign key stores: the
|
|
212
|
+
# belongs_to +primary_key:+ option or the associated model's primary key,
|
|
213
|
+
# falling back to +id+ (Mongoid, or a composite key).
|
|
214
|
+
def foreign_key_target(association)
|
|
215
|
+
key = association.try(:association_primary_key)
|
|
216
|
+
key.is_a?(String) || key.is_a?(Symbol) ? key : :id
|
|
217
|
+
end
|
|
141
218
|
end
|
|
142
219
|
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IronAdmin
|
|
4
|
+
# Assignable association records and the value key their selects submit.
|
|
5
|
+
module AssignableRecordsHelper
|
|
6
|
+
# Records a person may pick for an association, after the tenant scope.
|
|
7
|
+
#
|
|
8
|
+
# @param model_class [Class] associated model
|
|
9
|
+
# @return [ActiveRecord::Relation]
|
|
10
|
+
def assignable_records(model_class)
|
|
11
|
+
IronAdmin::TenantScope.apply(model_class.all)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Key a select for +association_name+ must submit.
|
|
15
|
+
#
|
|
16
|
+
# The association's foreign-key target when +model_class+ reflects that
|
|
17
|
+
# association, otherwise +:id+.
|
|
18
|
+
#
|
|
19
|
+
# @param model_class [Class] model that declares the association
|
|
20
|
+
# @param association_name [Symbol, String]
|
|
21
|
+
# @return [Symbol]
|
|
22
|
+
def association_value_key(model_class, association_name)
|
|
23
|
+
return :id unless model_class.respond_to?(:reflect_on_association)
|
|
24
|
+
|
|
25
|
+
reflection = model_class.reflect_on_association(association_name)
|
|
26
|
+
return :id unless reflection
|
|
27
|
+
|
|
28
|
+
key = reflection.try(:association_primary_key)
|
|
29
|
+
key.is_a?(String) || key.is_a?(Symbol) ? key.to_sym : :id
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -5,17 +5,21 @@ module IronAdmin
|
|
|
5
5
|
# {IronAdmin::Configuration::Components}). Each method returns nil when the
|
|
6
6
|
# slot has no override, and the view then renders its built-in markup.
|
|
7
7
|
module ComponentOverridesHelper
|
|
8
|
-
# @param slot [Symbol] A layout slot (+:shell+, +:navbar+ or +:
|
|
8
|
+
# @param slot [Symbol] A layout slot (+:shell+, +:navbar+, +:sidebar+ or +:flash+)
|
|
9
9
|
# @return [ViewComponent::Base] The override, or the built-in component
|
|
10
|
+
# @example Render the flash slot in a custom shell
|
|
11
|
+
# = render helpers.iron_admin_component(:flash, flash: helpers.flash, current_user: @current_user)
|
|
10
12
|
def iron_admin_component(slot, **arguments)
|
|
11
13
|
IronAdmin.configuration.components.build(slot, **arguments)
|
|
12
14
|
end
|
|
13
15
|
|
|
16
|
+
# @param bulk_actions [Array<Hash>] Bulk actions the policy allows
|
|
14
17
|
# @return [ViewComponent::Base, nil] The index page's table override
|
|
15
|
-
def iron_admin_table_component(resource_class, records:, fields:, scope:)
|
|
18
|
+
def iron_admin_table_component(resource_class, records:, fields:, scope:, bulk_actions:)
|
|
16
19
|
iron_admin_resource_component(:table, resource_class) do
|
|
17
20
|
{ records: records, fields: fields, current_sort: params[:sort], current_direction: params[:direction],
|
|
18
|
-
scope: scope, query: params[:q], base_url: iron_admin_sort_base_url(resource_class, scope),
|
|
21
|
+
scope: scope, query: params[:q], base_url: iron_admin_sort_base_url(resource_class, scope),
|
|
22
|
+
bulk_actions: bulk_actions, }
|
|
19
23
|
end
|
|
20
24
|
end
|
|
21
25
|
|
|
@@ -33,7 +37,8 @@ module IronAdmin
|
|
|
33
37
|
def iron_admin_filter_bar_component(resource_class, filters:, scope:)
|
|
34
38
|
iron_admin_resource_component(:filter_bar, resource_class) do
|
|
35
39
|
{ filters: filters, form_url: resources_path(resource_class.resource_name), scope: scope,
|
|
36
|
-
query: params[:q], active_count: filters.count { |filter| iron_admin_active_filter?(filter) },
|
|
40
|
+
query: params[:q], active_count: filters.count { |filter| iron_admin_active_filter?(filter) },
|
|
41
|
+
current_sort: iron_admin_index_state["sort"], current_direction: iron_admin_index_state["direction"], }
|
|
37
42
|
end
|
|
38
43
|
end
|
|
39
44
|
|
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module IronAdmin
|
|
4
|
-
#
|
|
5
|
-
# Extracted from ApplicationHelper for organization.
|
|
4
|
+
# Helper methods for rendering specific field types, all private but the show page's
|
|
5
|
+
# display_show_field_value. Extracted from ApplicationHelper for organization.
|
|
6
6
|
module FieldDisplayHelper # rubocop:disable Metrics/ModuleLength
|
|
7
|
+
# strftime format of datetime values, such as "Oct 25, 2026 at 6:30 PM" (squished, since
|
|
8
|
+
# %l pads one-digit hours), unless a field sets its own +format+. The audit log uses it too.
|
|
9
|
+
# @return [String]
|
|
10
|
+
DATETIME_FORMAT = "%b %d, %Y at %l:%M %p"
|
|
11
|
+
|
|
12
|
+
# Displays a field value on a show page: the formatted value, or, when the field is empty, a
|
|
13
|
+
# muted dash with a name for screen readers, so an empty row does not read as a value that
|
|
14
|
+
# did not load. A false value is not empty.
|
|
15
|
+
#
|
|
16
|
+
# @param record [ActiveRecord::Base] The record
|
|
17
|
+
# @param field [IronAdmin::Field] Field configuration
|
|
18
|
+
# @return [String, Object] Formatted value, or the empty value marker
|
|
19
|
+
def display_show_field_value(record, field)
|
|
20
|
+
value = display_field_value(record, field)
|
|
21
|
+
return value if value.equal?(false) || value.present?
|
|
22
|
+
|
|
23
|
+
content_tag(:span, class: "relative") do
|
|
24
|
+
content_tag(:span, "\u2014", class: cp_muted_text, "aria-hidden": "true") +
|
|
25
|
+
content_tag(:span, I18n.t("iron_admin.fields.empty_value"), class: "sr-only")
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
7
29
|
private
|
|
8
30
|
|
|
9
31
|
def display_hidden(_record, _field)
|
|
@@ -22,8 +44,8 @@ module IronAdmin
|
|
|
22
44
|
return if value.blank?
|
|
23
45
|
|
|
24
46
|
lang = field.options[:language].presence || ""
|
|
25
|
-
content_tag(:pre, class: "overflow-x-auto
|
|
26
|
-
content_tag(:code, value, class: "
|
|
47
|
+
content_tag(:pre, class: "overflow-x-auto p-4 text-sm #{t.border_radius} #{t.code_block}") do
|
|
48
|
+
content_tag(:code, value, class: "#{t.code_font} language-#{lang}")
|
|
27
49
|
end
|
|
28
50
|
end
|
|
29
51
|
|
|
@@ -52,19 +74,12 @@ module IronAdmin
|
|
|
52
74
|
IronAdmin.configuration.badge_colors[value.to_s] ||
|
|
53
75
|
IronAdmin.configuration.badge_colors[value] ||
|
|
54
76
|
:gray
|
|
55
|
-
color_classes = badge_color_classes(color)
|
|
56
77
|
|
|
57
|
-
|
|
58
|
-
class: "inline-flex px-2 py-1 text-xs font-semibold rounded-full #{color_classes}")
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def badge_color_classes(color)
|
|
62
|
-
IronAdmin::Configuration::BADGE_COLOR_CLASSES[color.to_sym] ||
|
|
63
|
-
IronAdmin::Configuration::BADGE_COLOR_CLASSES[:gray]
|
|
78
|
+
render(IronAdmin::Ui::BadgeComponent.new(text: value.to_s.humanize, color: color))
|
|
64
79
|
end
|
|
65
80
|
|
|
66
81
|
def display_password
|
|
67
|
-
content_tag(:span, "\u2022" * 8, class: "
|
|
82
|
+
content_tag(:span, "\u2022" * 8, class: "tracking-wider #{cp_muted_text}")
|
|
68
83
|
end
|
|
69
84
|
|
|
70
85
|
def display_file(record, field)
|
|
@@ -74,7 +89,7 @@ module IronAdmin
|
|
|
74
89
|
return unless attachment.attached?
|
|
75
90
|
|
|
76
91
|
if attachment.image?
|
|
77
|
-
image_tag main_app.url_for(attachment), class: "h-16 w-16 object-cover
|
|
92
|
+
image_tag main_app.url_for(attachment), class: "h-16 w-16 object-cover #{t.control_radius}"
|
|
78
93
|
else
|
|
79
94
|
content_tag(:span, class: "inline-flex items-center gap-1.5") do
|
|
80
95
|
heroicon("paper-clip", variant: :mini, options: { class: "h-4 w-4 #{cp_muted_text}" }) +
|
|
@@ -93,10 +108,9 @@ module IronAdmin
|
|
|
93
108
|
safe_join(
|
|
94
109
|
attachments.map do |attachment|
|
|
95
110
|
if attachment.image?
|
|
96
|
-
image_tag main_app.url_for(attachment), class: "h-12 w-12 object-cover
|
|
111
|
+
image_tag main_app.url_for(attachment), class: "h-12 w-12 object-cover #{t.control_radius}"
|
|
97
112
|
else
|
|
98
|
-
content_tag(:span, attachment.filename.to_s,
|
|
99
|
-
class: "inline-flex items-center px-2 py-1 text-xs rounded bg-gray-100 text-gray-700")
|
|
113
|
+
content_tag(:span, attachment.filename.to_s, class: "inline-flex items-center px-2 py-0.5 text-xs #{chip_classes}")
|
|
100
114
|
end
|
|
101
115
|
end
|
|
102
116
|
)
|
|
@@ -107,7 +121,7 @@ module IronAdmin
|
|
|
107
121
|
content = record.public_send(field.name)
|
|
108
122
|
return if content.blank?
|
|
109
123
|
|
|
110
|
-
content_tag(:div, sanitize(content.to_s), class: "
|
|
124
|
+
content_tag(:div, sanitize(content.to_s), class: "trix-content")
|
|
111
125
|
end
|
|
112
126
|
|
|
113
127
|
def display_markdown(record, field)
|
|
@@ -118,7 +132,7 @@ module IronAdmin
|
|
|
118
132
|
require "redcarpet"
|
|
119
133
|
renderer = Redcarpet::Render::HTML.new(hard_wrap: true, filter_html: true)
|
|
120
134
|
markdown = Redcarpet::Markdown.new(renderer, autolink: true, tables: true, fenced_code_blocks: true)
|
|
121
|
-
content_tag(:div, markdown.render(content).html_safe, class: "
|
|
135
|
+
content_tag(:div, markdown.render(content).html_safe, class: "trix-content") # rubocop:disable Rails/OutputSafety
|
|
122
136
|
rescue LoadError
|
|
123
137
|
content_tag(:pre, content, class: "text-sm whitespace-pre-wrap #{cp_body_text}")
|
|
124
138
|
end
|
|
@@ -134,8 +148,7 @@ module IronAdmin
|
|
|
134
148
|
content_tag(:div, class: "flex flex-wrap gap-1") do
|
|
135
149
|
safe_join(
|
|
136
150
|
tags.map do |tag|
|
|
137
|
-
content_tag(:span, tag,
|
|
138
|
-
class: "inline-flex px-2 py-0.5 text-xs font-medium rounded-full bg-indigo-50 text-indigo-700")
|
|
151
|
+
content_tag(:span, tag, class: "inline-flex items-center px-2 py-0.5 text-xs #{chip_classes}")
|
|
139
152
|
end
|
|
140
153
|
)
|
|
141
154
|
end
|
|
@@ -147,7 +160,7 @@ module IronAdmin
|
|
|
147
160
|
|
|
148
161
|
content_tag(:span, class: "inline-flex items-center gap-1") do
|
|
149
162
|
link_to(value, value, target: "_blank", rel: "noopener noreferrer", class: cp_link) +
|
|
150
|
-
heroicon("arrow-top-right-on-square", variant: :mini, options: { class: "h-3.5 w-3.5 #{
|
|
163
|
+
heroicon("arrow-top-right-on-square", variant: :mini, options: { class: "h-3.5 w-3.5 #{t.icon_muted}" })
|
|
151
164
|
end
|
|
152
165
|
end
|
|
153
166
|
|
|
@@ -163,7 +176,7 @@ module IronAdmin
|
|
|
163
176
|
return if value.blank?
|
|
164
177
|
|
|
165
178
|
content_tag(:span, class: "inline-flex items-center gap-2") do
|
|
166
|
-
content_tag(:span, "", class: "inline-block h-5 w-5
|
|
179
|
+
content_tag(:span, "", class: "inline-block h-5 w-5 #{t.control_radius} #{t.border_width} #{t.card_border}",
|
|
167
180
|
style: "background-color: #{ERB::Util.html_escape(value)}") +
|
|
168
181
|
content_tag(:code, value, class: "text-xs #{cp_muted_text}")
|
|
169
182
|
end
|
|
@@ -181,9 +194,9 @@ module IronAdmin
|
|
|
181
194
|
|
|
182
195
|
def display_boolean(record, field)
|
|
183
196
|
if record.public_send(field.name)
|
|
184
|
-
boolean_icon("check-circle",
|
|
197
|
+
boolean_icon("check-circle", t.success_text, I18n.t("iron_admin.fields.boolean_true"))
|
|
185
198
|
else
|
|
186
|
-
boolean_icon("x-circle",
|
|
199
|
+
boolean_icon("x-circle", cp_muted_text, I18n.t("iron_admin.fields.boolean_false"))
|
|
187
200
|
end
|
|
188
201
|
end
|
|
189
202
|
|
|
@@ -209,7 +222,7 @@ module IronAdmin
|
|
|
209
222
|
value = record.public_send(field.name)
|
|
210
223
|
return if value.nil?
|
|
211
224
|
|
|
212
|
-
fmt = field.options[:format] ||
|
|
225
|
+
fmt = field.options[:format] || DATETIME_FORMAT
|
|
213
226
|
value.strftime(fmt).squish
|
|
214
227
|
end
|
|
215
228
|
|
|
@@ -261,7 +274,7 @@ module IronAdmin
|
|
|
261
274
|
pairs = parse_hash_value(value)
|
|
262
275
|
return if pairs.empty?
|
|
263
276
|
|
|
264
|
-
content_tag(:dl, class: "divide-y
|
|
277
|
+
content_tag(:dl, class: "divide-y text-sm #{t.table_border} #{t.control_radius} #{t.border_width} #{t.card_border}") do
|
|
265
278
|
safe_join(pairs.map { |k, v| key_value_pair_row(k, v) })
|
|
266
279
|
end
|
|
267
280
|
end
|
|
@@ -281,11 +294,12 @@ module IronAdmin
|
|
|
281
294
|
end
|
|
282
295
|
|
|
283
296
|
def boolean_group_pill(value)
|
|
284
|
-
content_tag(
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
297
|
+
content_tag(:span, value.to_s.humanize, class: "inline-block px-2 py-0.5 mr-1 mb-1 text-xs #{chip_classes}")
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# Tags, file names and other values shown as chips.
|
|
301
|
+
def chip_classes
|
|
302
|
+
"#{t.badge_shape} #{t.chip}"
|
|
289
303
|
end
|
|
290
304
|
|
|
291
305
|
def display_external_image(record, field)
|
|
@@ -297,7 +311,7 @@ module IronAdmin
|
|
|
297
311
|
tag.img(
|
|
298
312
|
src: url,
|
|
299
313
|
alt: "",
|
|
300
|
-
class: "#{height} w-auto object-cover
|
|
314
|
+
class: "#{height} w-auto object-cover #{t.control_radius} #{t.border_width} #{t.card_border}",
|
|
301
315
|
loading: "lazy",
|
|
302
316
|
onerror: "this.style.display='none'"
|
|
303
317
|
)
|
|
@@ -308,10 +322,9 @@ module IronAdmin
|
|
|
308
322
|
return if value.nil?
|
|
309
323
|
|
|
310
324
|
pct = calculate_progress_percentage(value, field)
|
|
311
|
-
color = field.options[:color] || "bg-indigo-600"
|
|
312
325
|
|
|
313
326
|
content_tag(:div, class: "flex items-center gap-2") do
|
|
314
|
-
progress_bar_track(pct, color) +
|
|
327
|
+
progress_bar_track(pct, field.options[:color]) +
|
|
315
328
|
content_tag(:span, "#{value.to_i}%", class: "text-xs tabular-nums #{cp_muted_text}")
|
|
316
329
|
end
|
|
317
330
|
end
|
|
@@ -320,12 +333,40 @@ module IronAdmin
|
|
|
320
333
|
value = record.public_send(field.name)
|
|
321
334
|
return if value.blank?
|
|
322
335
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
336
|
+
display_truncated_text(value.to_s)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# A long text is cut to INDEX_TRUNCATION_LENGTH, with the whole text in the title.
|
|
340
|
+
def display_truncated_text(text)
|
|
341
|
+
truncated = truncate(text, length: ApplicationHelper::INDEX_TRUNCATION_LENGTH)
|
|
342
|
+
return truncated if text.length <= ApplicationHelper::INDEX_TRUNCATION_LENGTH
|
|
343
|
+
|
|
344
|
+
content_tag(:span, truncated, title: text)
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
# Index cells show rich text as plain text, truncated like long text fields.
|
|
348
|
+
def display_index_rich_text(record, field)
|
|
349
|
+
text = rich_text_plain_text(record.public_send(field.name))
|
|
350
|
+
display_truncated_text(text) if text.present?
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# The text of rich text HTML on one line: tags dropped, block elements
|
|
354
|
+
# (headings included) kept apart, script and style elements dropped with
|
|
355
|
+
# their content. Loofah ships with Rails through rails-html-sanitizer.
|
|
356
|
+
def rich_text_plain_text(content)
|
|
357
|
+
return "" if content.blank?
|
|
358
|
+
|
|
359
|
+
Loofah.fragment(rich_text_html(content)).scrub!(:prune).to_text(encode_special_chars: false).squish
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# A has_rich_text attribute's stored HTML, with each attachment replaced by
|
|
363
|
+
# its plain text (an image's caption or "[filename]", a mention's name) as
|
|
364
|
+
# ActionText's to_plain_text does; any other value as a String.
|
|
365
|
+
def rich_text_html(content)
|
|
366
|
+
body = content.try(:body)
|
|
367
|
+
return content.to_s unless body.respond_to?(:render_attachments)
|
|
368
|
+
|
|
369
|
+
body.render_attachments(with_full_attributes: false, &:to_plain_text).to_html
|
|
329
370
|
end
|
|
330
371
|
|
|
331
372
|
def display_compact_field_value(record, field)
|
|
@@ -381,9 +422,13 @@ module IronAdmin
|
|
|
381
422
|
((value.to_f - min) / (max - min) * 100.0).clamp(0.0, 100.0)
|
|
382
423
|
end
|
|
383
424
|
|
|
425
|
+
# A +color+ class fills the bar; without one, the theme's chart border color does. Both are
|
|
426
|
+
# rounded with control_radius, like the dashboard's ProgressComponent.
|
|
384
427
|
def progress_bar_track(pct, color)
|
|
385
|
-
|
|
386
|
-
|
|
428
|
+
width = "width: #{pct.round(1)}%"
|
|
429
|
+
style = color ? width : "#{width}; background-color: #{t.chart_border_color}"
|
|
430
|
+
content_tag(:div, class: "flex-1 h-2 overflow-hidden #{t.control_radius} #{t.progress_track}") do
|
|
431
|
+
content_tag(:div, "", class: "h-2 #{t.control_radius} #{color}".strip, style: style)
|
|
387
432
|
end
|
|
388
433
|
end
|
|
389
434
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IronAdmin
|
|
4
|
+
# Applies the related resource read policy and tenant scope to show-page panels.
|
|
5
|
+
module RelatedRecordsHelper
|
|
6
|
+
# Whether the current user may see this association panel.
|
|
7
|
+
#
|
|
8
|
+
# HABTM with no registered resource has no read policy, so the panel stays
|
|
9
|
+
# visible. Every other panel requires the related resource to allow +:read+.
|
|
10
|
+
#
|
|
11
|
+
# @param assoc [Hash] association config; +:resource+ is nil when HABTM has none
|
|
12
|
+
# @return [Boolean]
|
|
13
|
+
def related_panel_readable?(assoc)
|
|
14
|
+
resource = assoc[:resource]
|
|
15
|
+
return true if resource.nil?
|
|
16
|
+
|
|
17
|
+
resource.crud_allowed?(:read, iron_admin_current_user)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Tenant-scoped records for a has_many or HABTM panel.
|
|
21
|
+
#
|
|
22
|
+
# @param record [Object] parent record that owns the association
|
|
23
|
+
# @param assoc [Hash] association config with a +:name+ key
|
|
24
|
+
# @return [Object] the association after IronAdmin::TenantScope.apply
|
|
25
|
+
def related_records(record, assoc)
|
|
26
|
+
IronAdmin::TenantScope.apply(record.public_send(assoc[:name]))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Has-one record, or nil when it is missing or outside the tenant scope.
|
|
30
|
+
#
|
|
31
|
+
# Without a configured tenant scope the associated record is returned as-is
|
|
32
|
+
# and no extra query runs. With a tenant scope, membership is checked through
|
|
33
|
+
# the related resource adapter: the record is kept only when filtering
|
|
34
|
+
# +TenantScope.apply(adapter.all)+ by its primary key finds a row.
|
|
35
|
+
#
|
|
36
|
+
# @param record [Object] parent record that owns the association
|
|
37
|
+
# @param assoc [Hash] association config with +:name+ and +:resource+
|
|
38
|
+
# @return [Object, nil]
|
|
39
|
+
def related_record(record, assoc)
|
|
40
|
+
related = record.public_send(assoc[:name])
|
|
41
|
+
return if related.nil?
|
|
42
|
+
return related unless IronAdmin::TenantScope.configured?
|
|
43
|
+
|
|
44
|
+
adapter = assoc[:resource].adapter
|
|
45
|
+
scoped = IronAdmin::TenantScope.apply(adapter.all)
|
|
46
|
+
primary_key = adapter.primary_key
|
|
47
|
+
found = adapter.filter(scoped, primary_key.to_sym, related.public_send(primary_key)).first
|
|
48
|
+
found ? related : nil
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|