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
|
@@ -23,7 +23,8 @@ module IronAdmin
|
|
|
23
23
|
resource: @resource_class.name,
|
|
24
24
|
record_id: record.id.to_s,
|
|
25
25
|
changes: audited_changes(record),
|
|
26
|
-
ip_address: request.remote_ip
|
|
26
|
+
ip_address: request.remote_ip,
|
|
27
|
+
tenant_key: iron_admin_audit_tenant_key
|
|
27
28
|
)
|
|
28
29
|
|
|
29
30
|
IronAdmin::AuditLog.log(event)
|
|
@@ -41,7 +42,7 @@ module IronAdmin
|
|
|
41
42
|
@resource_class.adapter.after_commit { emit_event(action, record) }
|
|
42
43
|
end
|
|
43
44
|
|
|
44
|
-
# The record's saved changes, with
|
|
45
|
+
# The record's saved changes, with concealed fields and the host app's
|
|
45
46
|
# filter_parameters replaced by "[FILTERED]".
|
|
46
47
|
#
|
|
47
48
|
# @param record [Object]
|
|
@@ -52,19 +53,58 @@ module IronAdmin
|
|
|
52
53
|
|
|
53
54
|
# Built once per request: bulk actions and imports emit one event per
|
|
54
55
|
# record, and resolving the resource's fields for each would repeat the
|
|
55
|
-
# same work for every row.
|
|
56
|
+
# same work for every row. The filter is built from a single
|
|
57
|
+
# +resolved_fields+ call.
|
|
56
58
|
#
|
|
57
59
|
# @return [ActiveSupport::ParameterFilter]
|
|
58
60
|
def audit_parameter_filter
|
|
59
61
|
@audit_parameter_filter ||= begin
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
fields = @resource_class.resolved_fields
|
|
63
|
+
exact_names = concealed_names_for(fields).map { |name| /\A#{Regexp.escape(name)}\z/ }
|
|
64
|
+
ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters + exact_names)
|
|
62
65
|
end
|
|
63
66
|
end
|
|
64
67
|
|
|
65
|
-
#
|
|
68
|
+
# Names of fields declared with type :password.
|
|
69
|
+
#
|
|
70
|
+
# @return [Array<String>]
|
|
66
71
|
def password_field_names
|
|
67
|
-
@resource_class.resolved_fields
|
|
72
|
+
password_names_for(@resource_class.resolved_fields)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Password fields plus fields concealed from the current user.
|
|
76
|
+
#
|
|
77
|
+
# @return [Array<String>]
|
|
78
|
+
def concealed_field_names
|
|
79
|
+
concealed_names_for(@resource_class.resolved_fields)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Names of the given fields declared with type :password.
|
|
83
|
+
#
|
|
84
|
+
# @param fields [Array<IronAdmin::Field>] Resolved resource fields
|
|
85
|
+
# @return [Array<String>]
|
|
86
|
+
def password_names_for(fields)
|
|
87
|
+
fields.filter_map do |field|
|
|
88
|
+
field.name.to_s if field.type == :password
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Password names plus every column of a field concealed from the current
|
|
93
|
+
# user. +fields+ were already resolved, so audit keeps a single
|
|
94
|
+
# resolved_fields call per request. A hidden belongs_to contributes its
|
|
95
|
+
# foreign key, and a hidden polymorphic belongs_to its type and id columns.
|
|
96
|
+
#
|
|
97
|
+
# @param fields [Array<IronAdmin::Field>] Resolved resource fields
|
|
98
|
+
# @return [Array<String>]
|
|
99
|
+
def concealed_names_for(fields)
|
|
100
|
+
user = iron_admin_current_user
|
|
101
|
+
concealed = fields.flat_map do |field|
|
|
102
|
+
next [] if field.type == :password
|
|
103
|
+
next [] unless field.concealed_from?(user)
|
|
104
|
+
|
|
105
|
+
field.column_names.map(&:to_s)
|
|
106
|
+
end
|
|
107
|
+
(password_names_for(fields) + concealed).uniq
|
|
68
108
|
end
|
|
69
109
|
end
|
|
70
110
|
end
|
|
@@ -30,19 +30,44 @@ module IronAdmin
|
|
|
30
30
|
scope
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
# Filters the current user may apply.
|
|
34
|
+
#
|
|
35
|
+
# A filter declared with +filter+ stays unless its name is a resolved
|
|
36
|
+
# field this user cannot see. An inferred filter also drops when its
|
|
37
|
+
# name is a column of such a field, so a concealed +belongs_to+ hides
|
|
38
|
+
# an inferred filter on its foreign key. A filter no field claims stays.
|
|
39
|
+
#
|
|
40
|
+
# @return [Array<Hash>] Visible filter configurations
|
|
33
41
|
def iron_admin_visible_filters
|
|
34
|
-
@iron_admin_visible_filters ||=
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
@iron_admin_visible_filters ||= iron_admin_select_visible_filters
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def iron_admin_select_visible_filters
|
|
46
|
+
fields = @resource_class.resolved_fields
|
|
47
|
+
user = iron_admin_current_user
|
|
48
|
+
visible_fields = fields.select { |field| field.visible?(user) }
|
|
49
|
+
declared = @resource_class.defined_filters.map { |entry| entry[:name].to_sym }
|
|
50
|
+
|
|
51
|
+
@resource_class.all_filters.select do |filter|
|
|
52
|
+
name = filter[:name].to_sym
|
|
53
|
+
# Declared filters compare field names; inferred filters compare columns.
|
|
54
|
+
reader = declared.include?(name) ? :name : :column_names
|
|
55
|
+
iron_admin_name_visible?(name, fields, visible_fields, reader)
|
|
43
56
|
end
|
|
44
57
|
end
|
|
45
58
|
|
|
59
|
+
# @return [Boolean] true when the filter stays offered
|
|
60
|
+
def iron_admin_name_visible?(filter_name, fields, visible_fields, reader)
|
|
61
|
+
names = iron_admin_claim_names(fields, reader)
|
|
62
|
+
visible = iron_admin_claim_names(visible_fields, reader)
|
|
63
|
+
|
|
64
|
+
names.exclude?(filter_name) || visible.include?(filter_name)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def iron_admin_claim_names(fields, reader)
|
|
68
|
+
fields.flat_map { |field| Array(field.public_send(reader)) }.map(&:to_sym)
|
|
69
|
+
end
|
|
70
|
+
|
|
46
71
|
def apply_operator_filter(scope, filter)
|
|
47
72
|
sub = iron_admin_filter_param(filter[:name].to_s)
|
|
48
73
|
return scope unless sub.is_a?(ActionController::Parameters) && sub["value"].present?
|
|
@@ -10,13 +10,15 @@ module IronAdmin
|
|
|
10
10
|
|
|
11
11
|
# Builds the permit list for a nested association's attributes.
|
|
12
12
|
#
|
|
13
|
-
# Includes
|
|
14
|
-
# :_destroy when allow_destroy is true, and
|
|
13
|
+
# Includes fields that are neither read-only nor concealed, :id for
|
|
14
|
+
# matching existing records, :_destroy when allow_destroy is true, and
|
|
15
|
+
# the position_field when present.
|
|
15
16
|
#
|
|
16
17
|
# @param nested [IronAdmin::NestedAssociation]
|
|
17
18
|
# @return [Array] Permit list for strong params
|
|
18
19
|
def build_nested_permit_list(nested)
|
|
19
|
-
|
|
20
|
+
user = iron_admin_current_user
|
|
21
|
+
nested_fields = nested.fields.reject { |f| f.readonly?(user) || f.concealed_from?(user) }
|
|
20
22
|
permit_list = nested_fields.flat_map { |f| nested_field_permit(f) }
|
|
21
23
|
permit_list << :id
|
|
22
24
|
permit_list << :_destroy if nested.allow_destroy
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IronAdmin
|
|
4
|
+
module Concerns
|
|
5
|
+
# In-memory tenant check for nested attribute rows before a write.
|
|
6
|
+
module NestedTenantCheck
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# @param record [Object]
|
|
12
|
+
# @param source [Hash, nil]
|
|
13
|
+
# @return [Symbol] +:inside+, +:outside+, or +:unknown+
|
|
14
|
+
def nested_rows_verdict(record, source)
|
|
15
|
+
return :inside if source.nil?
|
|
16
|
+
|
|
17
|
+
verdict = :inside
|
|
18
|
+
string_keyed(source).each do |key, value|
|
|
19
|
+
nested = nested_for_attributes(key)
|
|
20
|
+
next unless nested
|
|
21
|
+
|
|
22
|
+
verdict = rows_verdict(record, nested, value)
|
|
23
|
+
break unless verdict == :inside
|
|
24
|
+
end
|
|
25
|
+
verdict
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @param key [String]
|
|
29
|
+
# @return [Object, nil]
|
|
30
|
+
def nested_for_attributes(key)
|
|
31
|
+
return unless key.end_with?("_attributes")
|
|
32
|
+
|
|
33
|
+
name = key.delete_suffix("_attributes")
|
|
34
|
+
@resource_class.nested_associations.find { |nested| nested.name.to_s == name }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @param record [Object]
|
|
38
|
+
# @param nested [Object]
|
|
39
|
+
# @param rows [Hash, Array, nil]
|
|
40
|
+
# @return [Symbol]
|
|
41
|
+
def rows_verdict(record, nested, rows)
|
|
42
|
+
verdict = :inside
|
|
43
|
+
nested_attribute_rows(nested, rows).each do |row|
|
|
44
|
+
verdict = nested_row_verdict(record, nested, row)
|
|
45
|
+
break unless verdict == :inside
|
|
46
|
+
end
|
|
47
|
+
verdict
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# A +:has_one+ entry is one row. A +:has_many+ entry is an Array or an
|
|
51
|
+
# index-to-row Hash, matching {TenantAssignable}.
|
|
52
|
+
#
|
|
53
|
+
# @param nested [Object]
|
|
54
|
+
# @param rows [Hash, Array, nil]
|
|
55
|
+
# @return [Array]
|
|
56
|
+
def nested_attribute_rows(nested, rows)
|
|
57
|
+
return [] if rows.nil?
|
|
58
|
+
return [rows] if nested.kind == :has_one
|
|
59
|
+
return rows if rows.is_a?(Array)
|
|
60
|
+
|
|
61
|
+
string_keyed(rows).values
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# @param record [Object]
|
|
65
|
+
# @param nested [Object]
|
|
66
|
+
# @param row [Hash]
|
|
67
|
+
# @return [Symbol]
|
|
68
|
+
def nested_row_verdict(record, nested, row)
|
|
69
|
+
attributes = string_keyed(row)
|
|
70
|
+
return :inside if destroy_row?(attributes)
|
|
71
|
+
|
|
72
|
+
child_adapter = nested_adapter(nested)
|
|
73
|
+
return :unknown if child_adapter.nil?
|
|
74
|
+
|
|
75
|
+
scope = IronAdmin::TenantScope.apply(nested.reflection.klass.all)
|
|
76
|
+
candidate = nested_candidate(record, nested, attributes, scope, child_adapter)
|
|
77
|
+
return :unknown if candidate.nil?
|
|
78
|
+
|
|
79
|
+
child_adapter.scope_match(scope, candidate)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Memoized per association for this request. +nil+ when the adapter cannot be built.
|
|
83
|
+
#
|
|
84
|
+
# @param nested [Object]
|
|
85
|
+
# @return [Object, nil]
|
|
86
|
+
def nested_adapter(nested)
|
|
87
|
+
adapters = (@nested_adapters ||= {})
|
|
88
|
+
name = nested.name.to_s
|
|
89
|
+
return adapters[name] if adapters.key?(name)
|
|
90
|
+
|
|
91
|
+
adapters[name] = new_nested_adapter(nested)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @param nested [Object]
|
|
95
|
+
# @return [Object, nil]
|
|
96
|
+
def new_nested_adapter(nested)
|
|
97
|
+
adapter.class.new(nested.reflection.klass)
|
|
98
|
+
rescue StandardError
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# @param record [Object]
|
|
103
|
+
# @param nested [Object]
|
|
104
|
+
# @param attributes [Hash]
|
|
105
|
+
# @param scope [Object]
|
|
106
|
+
# @param child_adapter [Object]
|
|
107
|
+
# @return [Object, nil]
|
|
108
|
+
def nested_candidate(record, nested, attributes, scope, child_adapter)
|
|
109
|
+
return child_adapter.build_in(scope, attributes) if attributes["id"].blank?
|
|
110
|
+
|
|
111
|
+
child = nested_child(record, nested, attributes["id"])
|
|
112
|
+
return nil if child.nil?
|
|
113
|
+
|
|
114
|
+
child_adapter.preview(child, attributes)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# @param record [Object]
|
|
118
|
+
# @param nested [Object]
|
|
119
|
+
# @param id [String, Integer]
|
|
120
|
+
# @return [Object, nil]
|
|
121
|
+
def nested_child(record, nested, id)
|
|
122
|
+
associated = record.public_send(nested.name)
|
|
123
|
+
return single_child(associated, id) if nested.kind == :has_one
|
|
124
|
+
return nil if associated.nil?
|
|
125
|
+
|
|
126
|
+
associated.to_a.find { |child| same_id?(child, id) }
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# @param associated [Object, nil]
|
|
130
|
+
# @param id [String, Integer]
|
|
131
|
+
# @return [Object, nil]
|
|
132
|
+
def single_child(associated, id)
|
|
133
|
+
return associated if same_id?(associated, id)
|
|
134
|
+
|
|
135
|
+
nil
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# @param child [Object, nil]
|
|
139
|
+
# @param id [String, Integer]
|
|
140
|
+
# @return [Boolean]
|
|
141
|
+
def same_id?(child, id)
|
|
142
|
+
!child.nil? && child.id.to_s == id.to_s
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# @param attributes [Hash]
|
|
146
|
+
# @return [Boolean, nil]
|
|
147
|
+
def destroy_row?(attributes)
|
|
148
|
+
ActiveModel::Type::Boolean.new.cast(attributes["_destroy"])
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @param attrs [Hash, ActionController::Parameters]
|
|
152
|
+
# @return [Hash]
|
|
153
|
+
def string_keyed(attrs)
|
|
154
|
+
source = attrs.respond_to?(:to_unsafe_h) ? attrs.to_unsafe_h : attrs.to_h
|
|
155
|
+
source.each_with_object({}) { |(key, value), copy| copy[key.to_s] = value }
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IronAdmin
|
|
4
|
+
module Concerns
|
|
5
|
+
# Allowlist and tenant-scope checks for posted polymorphic belongs_to references.
|
|
6
|
+
module PolymorphicReferences
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
def polymorphic_references_assignable?(record, attrs)
|
|
12
|
+
polymorphic_fields.map { |field| polymorphic_field_assignable?(record, attrs, field) }.all?
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def polymorphic_fields
|
|
16
|
+
writable_form_fields.select { |field| field.type == :polymorphic_belongs_to }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Rejects a submitted, non-blank polymorphic type outside the field allowlist.
|
|
20
|
+
#
|
|
21
|
+
# Always applies, even when no tenant scope is configured. One error is
|
|
22
|
+
# added per rejected field. Blank and omitted types are left alone.
|
|
23
|
+
#
|
|
24
|
+
# @param record [ActiveRecord::Base] record about to be saved
|
|
25
|
+
# @param attrs [Hash] string-keyed permitted attributes
|
|
26
|
+
# @return [Boolean] true when this check added no error
|
|
27
|
+
def polymorphic_types_allowed?(record, attrs)
|
|
28
|
+
return true unless attrs.is_a?(Hash)
|
|
29
|
+
|
|
30
|
+
polymorphic_fields.map { |field| polymorphic_field_type_allowed?(record, attrs, field) }.all?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def polymorphic_field_type_allowed?(record, attrs, field)
|
|
34
|
+
return true if polymorphic_submitted_type_allowed?(attrs, field)
|
|
35
|
+
|
|
36
|
+
record.errors.add(field.name, message: not_assignable_message)
|
|
37
|
+
false
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def polymorphic_field_assignable?(record, attrs, field)
|
|
41
|
+
return true unless polymorphic_column_submitted?(attrs, field)
|
|
42
|
+
# Type errors are recorded by polymorphic_types_allowed?, which always runs.
|
|
43
|
+
return false unless polymorphic_submitted_type_allowed?(attrs, field)
|
|
44
|
+
|
|
45
|
+
target_id = polymorphic_target_id(record, attrs, field)
|
|
46
|
+
return true if target_id.blank? || polymorphic_target_assignable?(record, attrs, field, target_id)
|
|
47
|
+
|
|
48
|
+
record.errors.add(field.name, message: not_assignable_message)
|
|
49
|
+
false
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def polymorphic_submitted_type_allowed?(attrs, field)
|
|
53
|
+
column = field.options[:type_column].to_s
|
|
54
|
+
return true unless attrs.key?(column)
|
|
55
|
+
|
|
56
|
+
type_name = attrs[column].to_s
|
|
57
|
+
return true if type_name.blank?
|
|
58
|
+
|
|
59
|
+
Array(field.options[:types]).any? { |type| type.name.to_s == type_name }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def polymorphic_column_submitted?(attrs, field)
|
|
63
|
+
return false unless attrs.is_a?(Hash)
|
|
64
|
+
|
|
65
|
+
type_column = field.options[:type_column].to_s
|
|
66
|
+
id_column = field.options[:id_column].to_s
|
|
67
|
+
attrs.key?(type_column) || attrs.key?(id_column)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def polymorphic_target_assignable?(record, attrs, field, target_id)
|
|
71
|
+
type_class = polymorphic_type_class(record, attrs, field)
|
|
72
|
+
return false unless type_class
|
|
73
|
+
|
|
74
|
+
helpers.assignable_records(type_class).exists?(id: target_id)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def polymorphic_target_id(record, attrs, field)
|
|
78
|
+
column = field.options[:id_column].to_s
|
|
79
|
+
# Blank is a submitted clear; fall back only when the key was omitted.
|
|
80
|
+
return attrs[column] if attrs.key?(column)
|
|
81
|
+
|
|
82
|
+
record.public_send(field.options[:id_column])
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def polymorphic_type_class(record, attrs, field)
|
|
86
|
+
type_name = polymorphic_type_name(record, attrs, field).to_s
|
|
87
|
+
Array(field.options[:types]).find { |type| type.name.to_s == type_name }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def polymorphic_type_name(record, attrs, field)
|
|
91
|
+
column = field.options[:type_column].to_s
|
|
92
|
+
return attrs[column] if attrs.key?(column)
|
|
93
|
+
|
|
94
|
+
record.public_send(field.options[:type_column])
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Keeps a stored polymorphic target whose type is outside the allowlist.
|
|
98
|
+
#
|
|
99
|
+
# The edit form posts a blank type and id when that stored type has no
|
|
100
|
+
# options, and a blank pair would otherwise clear the association. When
|
|
101
|
+
# the stored type is present and is not the name of one of
|
|
102
|
+
# +field.options[:types]+, and the submitted type and id are both blank
|
|
103
|
+
# or absent, both keys are removed. Returns a string-keyed copy when a
|
|
104
|
+
# key is removed, otherwise +attrs+.
|
|
105
|
+
#
|
|
106
|
+
# @param record [ActiveRecord::Base] record being updated
|
|
107
|
+
# @param attrs [Hash, ActionController::Parameters] permitted attributes
|
|
108
|
+
# @return [Hash, ActionController::Parameters]
|
|
109
|
+
def keep_unlisted_polymorphic_targets(record, attrs)
|
|
110
|
+
return attrs unless attrs.respond_to?(:key?)
|
|
111
|
+
|
|
112
|
+
fields = polymorphic_fields.select { |field| blank_unlisted_polymorphic_target?(record, attrs, field) }
|
|
113
|
+
return attrs if fields.empty?
|
|
114
|
+
|
|
115
|
+
kept = string_keyed_attributes(attrs)
|
|
116
|
+
fields.each { |field| kept.except!(field.options[:type_column].to_s, field.options[:id_column].to_s) }
|
|
117
|
+
kept
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def blank_unlisted_polymorphic_target?(record, attrs, field)
|
|
121
|
+
type_column = field.options[:type_column].to_s
|
|
122
|
+
id_column = field.options[:id_column].to_s
|
|
123
|
+
stored = record.public_send(type_column).to_s
|
|
124
|
+
return false if stored.blank? || Array(field.options[:types]).any? { |type| type.name.to_s == stored }
|
|
125
|
+
return false unless attrs.key?(type_column) || attrs.key?(id_column)
|
|
126
|
+
|
|
127
|
+
[type_column, id_column].all? { |column| !attrs.key?(column) || attrs[column].blank? }
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -10,9 +10,7 @@ module IronAdmin
|
|
|
10
10
|
private
|
|
11
11
|
|
|
12
12
|
def base_scope
|
|
13
|
-
|
|
14
|
-
scope = IronAdmin.configuration.tenant_scope_block.call(scope) if IronAdmin.configuration.tenant_scope_block
|
|
15
|
-
scope
|
|
13
|
+
IronAdmin::TenantScope.apply(adapter.all)
|
|
16
14
|
end
|
|
17
15
|
|
|
18
16
|
def collection_scope
|
|
@@ -86,6 +84,35 @@ module IronAdmin
|
|
|
86
84
|
defined_scope&.dig(:name)&.to_s
|
|
87
85
|
end
|
|
88
86
|
|
|
87
|
+
# Sets the selected scope and the tab counts. Counts run only when the
|
|
88
|
+
# index is going to render: a pagination redirect returns before this.
|
|
89
|
+
def assign_index_scope
|
|
90
|
+
@current_scope = current_scope_name
|
|
91
|
+
@scope_counts = scope_count_map
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def scope_count_map
|
|
95
|
+
return {} if respond_to?(:turbo_frame_request_id, true) && turbo_frame_request_id == "records"
|
|
96
|
+
|
|
97
|
+
ScopeCounts.new(
|
|
98
|
+
resource: @resource_class,
|
|
99
|
+
adapter: adapter,
|
|
100
|
+
relation_for: method(:relation_for_scope_count)
|
|
101
|
+
).to_h
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# What one tab would list: the tenant scope, the current filters, that
|
|
105
|
+
# tab's scope, the current search and the index preloading. Not the
|
|
106
|
+
# selected scope, not the sorting, and not a page. fallback: false
|
|
107
|
+
# keeps a nil or false body from becoming a relation, so search and
|
|
108
|
+
# preloading stop before they run.
|
|
109
|
+
def relation_for_scope_count(scope_body)
|
|
110
|
+
scoped = apply_resource_scope(apply_filters(base_scope), scope_body, fallback: false)
|
|
111
|
+
return scoped if scoped.nil? || scoped == false
|
|
112
|
+
|
|
113
|
+
apply_preloading(apply_search(scoped))
|
|
114
|
+
end
|
|
115
|
+
|
|
89
116
|
def apply_scopes(scope)
|
|
90
117
|
defined_scope = @resource_class.all_scopes.find { |s| s[:name].to_s == params[:scope] }
|
|
91
118
|
defined_scope ||= @resource_class.all_scopes.find { |s| s[:default] }
|
|
@@ -95,10 +122,24 @@ module IronAdmin
|
|
|
95
122
|
apply_resource_scope(scope, defined_scope[:scope])
|
|
96
123
|
end
|
|
97
124
|
|
|
98
|
-
|
|
125
|
+
# Applies a resource scope body to the relation.
|
|
126
|
+
#
|
|
127
|
+
# A callable body runs against the relation. Nil or false with
|
|
128
|
+
# fallback becomes the incoming scope, as Active Record treats a
|
|
129
|
+
# scope that returns nil or false as all. Without fallback the nil
|
|
130
|
+
# or false result is returned. A non-callable body is merged as is.
|
|
131
|
+
#
|
|
132
|
+
# @param scope [Object] relation built so far
|
|
133
|
+
# @param scope_body [Object] callable scope or a mergeable relation
|
|
134
|
+
# @param fallback [Boolean] replace a nil or false result with scope
|
|
135
|
+
# @return [Object]
|
|
136
|
+
def apply_resource_scope(scope, scope_body, fallback: true)
|
|
99
137
|
return scope.merge(scope_body) unless scope_body.respond_to?(:call)
|
|
100
138
|
|
|
101
|
-
scope_body.arity.zero? ? scope.instance_exec(&scope_body) : scope_body.call(scope)
|
|
139
|
+
result = scope_body.arity.zero? ? scope.instance_exec(&scope_body) : scope_body.call(scope)
|
|
140
|
+
return scope if fallback && (result.nil? || result == false)
|
|
141
|
+
|
|
142
|
+
result
|
|
102
143
|
end
|
|
103
144
|
|
|
104
145
|
def apply_sorting(scope)
|
|
@@ -109,9 +150,15 @@ module IronAdmin
|
|
|
109
150
|
adapter.order_by(scope, sort_col, sort_dir)
|
|
110
151
|
end
|
|
111
152
|
|
|
112
|
-
# Sorting by a
|
|
153
|
+
# Sorting by a concealed field would leak the relative order of its values.
|
|
154
|
+
# Columns of that field count too, so a hidden belongs_to rejects its foreign key.
|
|
113
155
|
def sortable_column?(name)
|
|
114
|
-
adapter.has_column?(name)
|
|
156
|
+
return false unless adapter.has_column?(name)
|
|
157
|
+
|
|
158
|
+
column = name.to_s
|
|
159
|
+
@resource_class.resolved_fields.none? do |field|
|
|
160
|
+
field.concealed_from?(iron_admin_current_user) && field.column_names.map(&:to_s).include?(column)
|
|
161
|
+
end
|
|
115
162
|
end
|
|
116
163
|
|
|
117
164
|
def apply_preloading(scope)
|
|
@@ -71,17 +71,17 @@ module IronAdmin
|
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
def visible_searchable_columns
|
|
74
|
-
@resource_class.visible_searchable_columns(iron_admin_current_user)
|
|
74
|
+
@visible_searchable_columns ||= @resource_class.visible_searchable_columns(iron_admin_current_user)
|
|
75
75
|
end
|
|
76
76
|
|
|
77
|
-
# A field query searches only fields the user
|
|
77
|
+
# A field query searches only fields the user is allowed to see.
|
|
78
78
|
def field_searchable?(field_name)
|
|
79
79
|
searchable_field_names.include?(field_name)
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
def searchable_field_names
|
|
83
83
|
@searchable_field_names ||= @resource_class.resolved_fields
|
|
84
|
-
.
|
|
84
|
+
.reject { |f| f.concealed_from?(iron_admin_current_user) }
|
|
85
85
|
.map(&:name)
|
|
86
86
|
end
|
|
87
87
|
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IronAdmin
|
|
4
|
+
module Concerns
|
|
5
|
+
# Stamps tenant equalities onto new nested rows, strips them from existing
|
|
6
|
+
# rows, and keeps hidden many-to-many ids when updating.
|
|
7
|
+
module TenantAssignable
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
include TenantContainment
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# Copies tenant equalities into new nested rows and strips them from saved rows.
|
|
14
|
+
#
|
|
15
|
+
# Returns +attrs+ unchanged unless a tenant scope is configured. Otherwise
|
|
16
|
+
# returns a Hash with string keys. For each nested association whose
|
|
17
|
+
# +"#{name}_attributes"+ entry is present, every row drops the nested
|
|
18
|
+
# model's tenant columns. A +:has_one+ entry is one row of columns. A
|
|
19
|
+
# +:has_many+ entry is a Hash of index to row, or an Array of rows. A row
|
|
20
|
+
# without a present +id+ then merges those tenant values, which win.
|
|
21
|
+
#
|
|
22
|
+
# @param attrs [Hash, ActionController::Parameters] permitted attributes
|
|
23
|
+
# @return [Hash, ActionController::Parameters]
|
|
24
|
+
def with_nested_tenant_attributes(attrs)
|
|
25
|
+
return attrs unless IronAdmin::TenantScope.configured?
|
|
26
|
+
|
|
27
|
+
stamped = string_keyed_attributes(attrs)
|
|
28
|
+
@resource_class.nested_associations.each do |nested|
|
|
29
|
+
key = "#{nested.name}_attributes"
|
|
30
|
+
next unless stamped.key?(key)
|
|
31
|
+
|
|
32
|
+
stamped[key] = stamp_nested_attribute_rows(nested, stamped[key])
|
|
33
|
+
end
|
|
34
|
+
stamped
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Keeps many-to-many ids that this tenant is not allowed to assign.
|
|
38
|
+
#
|
|
39
|
+
# Update calls this after +references_assignable?+. It first removes a
|
|
40
|
+
# blank polymorphic type and id whose stored type is outside the
|
|
41
|
+
# allowlist, so that pair does not clear the association. When a tenant
|
|
42
|
+
# scope is configured and attrs include +"<singular>_ids"+ for a habtm
|
|
43
|
+
# association, that value becomes the submitted ids plus the record's
|
|
44
|
+
# current ids that +assignable_records+ does not include, as unique
|
|
45
|
+
# strings. A new record has no hidden associations, so create does not
|
|
46
|
+
# call this.
|
|
47
|
+
#
|
|
48
|
+
# @param attrs [Hash, ActionController::Parameters] permitted attributes
|
|
49
|
+
# @return [Hash, ActionController::Parameters]
|
|
50
|
+
def preserve_hidden_habtm_ids(attrs)
|
|
51
|
+
kept = keep_unlisted_polymorphic_targets(@record, attrs)
|
|
52
|
+
return kept unless IronAdmin::TenantScope.configured?
|
|
53
|
+
|
|
54
|
+
preserved = string_keyed_attributes(kept)
|
|
55
|
+
replaced = false
|
|
56
|
+
@resource_class.habtm_associations.each do |assoc|
|
|
57
|
+
column = "#{assoc[:name].to_s.singularize}_ids"
|
|
58
|
+
next unless preserved.key?(column)
|
|
59
|
+
|
|
60
|
+
preserved[column] = submitted_plus_hidden_ids(assoc, preserved[column])
|
|
61
|
+
replaced = true
|
|
62
|
+
end
|
|
63
|
+
replaced ? preserved : kept
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Removes tenant columns from submitted attributes.
|
|
67
|
+
#
|
|
68
|
+
# Keys are compared as strings. Only scalar equalities returned by
|
|
69
|
+
# +scope_attributes+ are protected.
|
|
70
|
+
#
|
|
71
|
+
# @param attrs [Hash, ActionController::Parameters] permitted attributes
|
|
72
|
+
# @return [Hash, ActionController::Parameters]
|
|
73
|
+
def without_tenant_attributes(attrs)
|
|
74
|
+
protected_keys = adapter.scope_attributes(base_scope).keys.map(&:to_s)
|
|
75
|
+
removable = attrs.keys.select { |key| protected_keys.include?(key.to_s) }
|
|
76
|
+
attrs.except(*removable)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def stamp_nested_attribute_rows(nested, rows)
|
|
80
|
+
tenant = nested_tenant_attributes(nested)
|
|
81
|
+
return apply_nested_tenant_row(rows, tenant) if nested.kind == :has_one
|
|
82
|
+
return rows.map { |row| apply_nested_tenant_row(row, tenant) } if rows.is_a?(Array)
|
|
83
|
+
|
|
84
|
+
string_keyed_attributes(rows).each_with_object({}) do |(index, row), stamped|
|
|
85
|
+
stamped[index] = apply_nested_tenant_row(row, tenant)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def nested_tenant_attributes(nested)
|
|
90
|
+
scope = IronAdmin::TenantScope.apply(nested.reflection.klass.all)
|
|
91
|
+
adapter.scope_attributes(scope)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def apply_nested_tenant_row(row, tenant)
|
|
95
|
+
attributes = string_keyed_attributes(row)
|
|
96
|
+
stripped = attributes.except(*tenant.keys.map(&:to_s))
|
|
97
|
+
return stripped if attributes["id"].present?
|
|
98
|
+
|
|
99
|
+
stripped.merge(string_keyed_attributes(tenant))
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def submitted_plus_hidden_ids(assoc, raw_ids)
|
|
103
|
+
klass = assoc[:reflection].klass
|
|
104
|
+
hidden = @record.public_send(assoc[:name]).where.not(
|
|
105
|
+
klass.primary_key => helpers.assignable_records(klass).select(klass.primary_key)
|
|
106
|
+
).pluck(klass.primary_key)
|
|
107
|
+
(Array(raw_ids) + hidden).map(&:to_s).uniq
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def string_keyed_attributes(attrs)
|
|
111
|
+
source = attrs.respond_to?(:to_h) ? attrs.to_h : attrs
|
|
112
|
+
source.each_with_object({}) do |(key, value), copy|
|
|
113
|
+
copy[key.to_s] = value
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|