rails_ai_admin 0.4.1 → 0.5.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 +64 -0
- data/README.md +4 -9
- data/SECURITY.md +1 -1
- data/app/assets/builds/rails_ai_admin.js +5 -5
- data/app/controllers/rails_ai_admin/application_controller.rb +19 -6
- data/app/controllers/rails_ai_admin/assistant_controller.rb +20 -7
- data/app/controllers/rails_ai_admin/audit_controller.rb +5 -1
- data/app/controllers/rails_ai_admin/bulk_changes_controller.rb +26 -6
- data/app/controllers/rails_ai_admin/dashboard_controller.rb +3 -2
- data/app/controllers/rails_ai_admin/deleted_records_controller.rb +12 -4
- data/app/controllers/rails_ai_admin/files_controller.rb +16 -4
- data/app/controllers/rails_ai_admin/mcp_controller.rb +30 -4
- data/app/controllers/rails_ai_admin/mcp_tokens_controller.rb +4 -2
- data/app/controllers/rails_ai_admin/operations_controller.rb +5 -2
- data/app/controllers/rails_ai_admin/resources_controller.rb +44 -5
- data/app/controllers/rails_ai_admin/selections_controller.rb +41 -5
- data/app/helpers/rails_ai_admin/assistant_helper.rb +3 -1
- data/app/helpers/rails_ai_admin/fields_helper.rb +36 -7
- data/app/helpers/rails_ai_admin/history_helper.rb +1 -1
- data/app/views/layouts/rails_ai_admin/application.html.erb +1 -0
- data/app/views/layouts/rails_ai_admin/frame.html.erb +1 -1
- data/app/views/layouts/rails_ai_admin/status.html.erb +2 -2
- data/app/views/rails_ai_admin/assistant/_transcript.html.erb +5 -1
- data/app/views/rails_ai_admin/audit/index.html.erb +1 -1
- data/app/views/rails_ai_admin/deleted_records/index.html.erb +3 -2
- data/app/views/rails_ai_admin/fields/_input.html.erb +2 -1
- data/app/views/rails_ai_admin/selections/confirm_destroy.html.erb +1 -0
- data/config/locales/en.yml +2 -0
- data/db/assistant_migrate/20261103000001_create_rails_ai_admin_assistant.rb +1 -0
- data/db/migrate/20261122000001_add_missing_indexes_to_rails_ai_admin_tables.rb +12 -0
- data/lib/generators/rails_ai_admin/assistant/assistant_generator.rb +4 -1
- data/lib/generators/rails_ai_admin/assistant/templates/admin_assistant.rb +6 -0
- data/lib/generators/rails_ai_admin/assistant/templates/assistant_eval.yml +25 -0
- data/lib/generators/rails_ai_admin/install/templates/admin_context_devise.rb.tt +2 -1
- data/lib/generators/rails_ai_admin/install/templates/admin_context_rails.rb.tt +5 -2
- data/lib/rails_ai_admin/assistant/budget.rb +4 -2
- data/lib/rails_ai_admin/assistant/coordinator.rb +18 -4
- data/lib/rails_ai_admin/assistant/evaluation.rb +124 -0
- data/lib/rails_ai_admin/assistant/records.rb +2 -1
- data/lib/rails_ai_admin/assistant/retention.rb +12 -1
- data/lib/rails_ai_admin/assistant/runtime.rb +49 -1
- data/lib/rails_ai_admin/assistant/tools.rb +58 -18
- data/lib/rails_ai_admin/assistant.rb +1 -0
- data/lib/rails_ai_admin/authorization/access.rb +34 -23
- data/lib/rails_ai_admin/authorization/pundit_adapter.rb +6 -3
- data/lib/rails_ai_admin/collections/dispatcher.rb +3 -3
- data/lib/rails_ai_admin/collections/queries.rb +1 -1
- data/lib/rails_ai_admin/configuration.rb +39 -8
- data/lib/rails_ai_admin/doctor.rb +44 -9
- data/lib/rails_ai_admin/field.rb +37 -22
- data/lib/rails_ai_admin/fields/association.rb +27 -10
- data/lib/rails_ai_admin/fields/type.rb +1 -1
- data/lib/rails_ai_admin/forms/plan.rb +4 -2
- data/lib/rails_ai_admin/input_schema.rb +4 -4
- data/lib/rails_ai_admin/integrations/propose_operation.rb +18 -4
- data/lib/rails_ai_admin/integrations/tool_views.rb +29 -3
- data/lib/rails_ai_admin/mcp.rb +23 -13
- data/lib/rails_ai_admin/navigation/list_state.rb +3 -3
- data/lib/rails_ai_admin/navigation/saved_views.rb +6 -5
- data/lib/rails_ai_admin/operations/restore.rb +25 -4
- data/lib/rails_ai_admin/operations/runner.rb +51 -15
- data/lib/rails_ai_admin/outbox/dispatcher.rb +6 -2
- data/lib/rails_ai_admin/presentation/association_panel.rb +6 -6
- data/lib/rails_ai_admin/presentation/audit_log.rb +8 -3
- data/lib/rails_ai_admin/presentation/csv_export.rb +21 -8
- data/lib/rails_ai_admin/presentation/csv_import.rb +9 -1
- data/lib/rails_ai_admin/presentation/dashboard.rb +5 -2
- data/lib/rails_ai_admin/presentation/history.rb +1 -1
- data/lib/rails_ai_admin/prune.rb +16 -5
- data/lib/rails_ai_admin/queries/list.rb +138 -38
- data/lib/rails_ai_admin/record_version.rb +1 -1
- data/lib/rails_ai_admin/registry.rb +5 -0
- data/lib/rails_ai_admin/request_log_filter.rb +4 -2
- data/lib/rails_ai_admin/resource.rb +4 -0
- data/lib/rails_ai_admin/schema_suggestions.rb +13 -5
- data/lib/rails_ai_admin/templates/catalog.rb +4 -2
- data/lib/rails_ai_admin/templates/inspector.rb +6 -2
- data/lib/rails_ai_admin/templates/safe_paths.rb +2 -1
- data/lib/rails_ai_admin/version.rb +1 -1
- data/lib/rails_ai_admin/work.rb +1 -1
- data/lib/rails_ai_admin.rb +10 -6
- data/lib/tasks/rails_ai_admin.rake +20 -0
- metadata +25 -2
|
@@ -76,6 +76,11 @@ module RailsAiAdmin
|
|
|
76
76
|
payload["id"]
|
|
77
77
|
rescue ActiveRecord::RecordNotUnique
|
|
78
78
|
payload["id"]
|
|
79
|
+
rescue ActiveRecord::LockWaitTimeout
|
|
80
|
+
raise AccessDenied, "CONFLICT", cause: nil
|
|
81
|
+
rescue ActiveRecord::StatementInvalid => error
|
|
82
|
+
raise unless defined?(SQLite3::BusyException) && error.cause.is_a?(SQLite3::BusyException)
|
|
83
|
+
raise AccessDenied, "CONFLICT", cause: nil
|
|
79
84
|
end
|
|
80
85
|
|
|
81
86
|
# Reopens an unconsumed native form after fresh authentication. No input,
|
|
@@ -194,7 +199,7 @@ module RailsAiAdmin
|
|
|
194
199
|
reject!("NOT_FOUND") if run.parent_run_id && !admission
|
|
195
200
|
check_origin!(origin)
|
|
196
201
|
reject!("APPROVAL_INVALIDATED") unless run.security_version == context.security_version
|
|
197
|
-
reject!("APPROVAL_EXPIRED") unless run.expires_at && run.expires_at > Time.current
|
|
202
|
+
reject!("APPROVAL_EXPIRED") unless run.status == "succeeded" || run.expires_at && run.expires_at > Time.current
|
|
198
203
|
check_binding!(run, binding) if binding
|
|
199
204
|
resource = RailsAiAdmin.registry.fetch(run.resource_key)
|
|
200
205
|
definition = RailsAiAdmin.operation_registry.fetch(resource, run.operation)
|
|
@@ -259,15 +264,19 @@ module RailsAiAdmin
|
|
|
259
264
|
# A password writer changes a digest column the resource never names.
|
|
260
265
|
changed_fields |= resource.fields.select { |field| field.scalar_type == :password && !values[field.name.to_s].nil? }.map { |field| field.name.to_s }
|
|
261
266
|
# Action Text keeps the content in its own record.
|
|
262
|
-
|
|
267
|
+
changed_rich_text = resource.fields.select { |field| field.scalar_type == :rich_text && values.key?(field.name.to_s) && record.public_send(field.column)&.changed? }.map { |field| field.name.to_s }
|
|
268
|
+
changed_fields |= changed_rich_text
|
|
263
269
|
# Active Storage keeps attachment changes outside the attributes.
|
|
264
|
-
if record.respond_to?(:attachment_changes)
|
|
265
|
-
|
|
270
|
+
changed_single_files = if record.respond_to?(:attachment_changes)
|
|
271
|
+
resource.fields.select { |field| field.scalar_type == :file && values.key?(field.name.to_s) && record.attachment_changes.key?(field.column) }.map { |field| field.name.to_s }
|
|
272
|
+
else
|
|
273
|
+
[]
|
|
266
274
|
end
|
|
275
|
+
changed_fields |= changed_single_files
|
|
267
276
|
changed_files = apply_files(access, record, values)
|
|
268
277
|
changed_fields |= changed_files
|
|
269
|
-
# Join rows change no owner column, so
|
|
270
|
-
# record
|
|
278
|
+
# Join rows, attachments and Action Text change no owner column, so
|
|
279
|
+
# advance the record version explicitly when only those changed.
|
|
271
280
|
changed_lists = lists.map { |field| field.name.to_s }.reject { |name| before_lists.fetch(name) == values.fetch(name) }
|
|
272
281
|
changed_fields |= changed_lists
|
|
273
282
|
persisted = if run.operation == "destroy" && resource.soft_delete?
|
|
@@ -276,7 +285,8 @@ module RailsAiAdmin
|
|
|
276
285
|
definition.handler_class.new.perform(nil, record, values)
|
|
277
286
|
end
|
|
278
287
|
reject!("VALIDATION_FAILED", errors: { "base" => [ "could not be saved" ] }) unless persisted
|
|
279
|
-
|
|
288
|
+
external_change = changed_lists.any? || changed_files.any? || changed_single_files.any? || changed_rich_text.any?
|
|
289
|
+
advance_version!(record) if run.operation == "update" && external_change && RecordVersion.of(record) == run.expected_version
|
|
280
290
|
end
|
|
281
291
|
unless run.operation == "destroy" || access.relation.where(resource.model_class.primary_key => record.id).exists?
|
|
282
292
|
reject!("NOT_FOUND")
|
|
@@ -305,14 +315,17 @@ module RailsAiAdmin
|
|
|
305
315
|
audit_failure(context, run, error.code, origin) unless admission
|
|
306
316
|
Result.new(code: error.code)
|
|
307
317
|
when ActiveRecord::RecordInvalid
|
|
308
|
-
|
|
309
|
-
|
|
318
|
+
same_model = access && error.record.is_a?(access.resource.model_class)
|
|
319
|
+
allowed = if !same_model
|
|
320
|
+
[]
|
|
321
|
+
elsif definition&.custom?
|
|
322
|
+
access.operation_fields(run.operation, error.record).map { |field| field.name.to_s }
|
|
310
323
|
else
|
|
311
|
-
access
|
|
324
|
+
access.fields(:write, error.record, operation: run.operation).map { |field| field.name.to_s }
|
|
312
325
|
end
|
|
313
326
|
names = definition&.custom? || !access ? {} : field_names_by_column(access.resource)
|
|
314
327
|
audit_failure(context, run, "VALIDATION_FAILED", origin) unless admission
|
|
315
|
-
invalid_model_result(error.record, names, allowed)
|
|
328
|
+
same_model ? invalid_model_result(error.record, names, allowed) : Result.new(code: "VALIDATION_FAILED", errors: { "base" => [ Field.error_message(:invalid, "is invalid") ] })
|
|
316
329
|
when ActiveRecord::RecordNotDestroyed
|
|
317
330
|
if run&.operation == "destroy" && !definition&.custom?
|
|
318
331
|
# dependent: :restrict_with_error (or a before_destroy abort) says
|
|
@@ -345,6 +358,9 @@ module RailsAiAdmin
|
|
|
345
358
|
# A references target deleted after its check fails the id writer.
|
|
346
359
|
ActiveRecord::RecordNotFound
|
|
347
360
|
Result.new(code: "CONFLICT")
|
|
361
|
+
when ActiveRecord::NotNullViolation
|
|
362
|
+
audit_failure(context, run, "VALIDATION_FAILED", origin) unless admission
|
|
363
|
+
Result.new(code: "VALIDATION_FAILED", errors: { "base" => [ Field.error_message(:blank, "cannot be blank") ] })
|
|
348
364
|
when ActiveRecord::StatementInvalid
|
|
349
365
|
if defined?(SQLite3::BusyException) && error.cause.is_a?(SQLite3::BusyException)
|
|
350
366
|
Result.new(code: "CONFLICT")
|
|
@@ -427,8 +443,25 @@ module RailsAiAdmin
|
|
|
427
443
|
end
|
|
428
444
|
|
|
429
445
|
def parse_version(value)
|
|
430
|
-
|
|
431
|
-
|
|
446
|
+
if value.is_a?(Integer)
|
|
447
|
+
raise AccessDenied, "CONFLICT" unless value >= 0 && value <= 2**63 - 1
|
|
448
|
+
return value
|
|
449
|
+
end
|
|
450
|
+
raise AccessDenied, "CONFLICT" unless value.is_a?(String) && value.valid_encoding? && value.match?(/\A(?:0|[1-9]\d{0,18})\z/)
|
|
451
|
+
number = Integer(value, 10)
|
|
452
|
+
raise AccessDenied, "CONFLICT" if number > 2**63 - 1
|
|
453
|
+
number
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
def advance_version!(record)
|
|
457
|
+
if RecordVersion.strategy(record.class) == :lock_version
|
|
458
|
+
pk = record.class.primary_key
|
|
459
|
+
updated = record.class.where(pk => record.id, lock_version: record.lock_version).update_all("lock_version = lock_version + 1")
|
|
460
|
+
raise ActiveRecord::StaleObjectError.new(record, "update") unless updated == 1
|
|
461
|
+
record.reload
|
|
462
|
+
else
|
|
463
|
+
record.touch
|
|
464
|
+
end
|
|
432
465
|
end
|
|
433
466
|
|
|
434
467
|
def ensure_connection!(resource, definition)
|
|
@@ -469,7 +502,10 @@ module RailsAiAdmin
|
|
|
469
502
|
elsif run.operation == "create"
|
|
470
503
|
access.relation.new
|
|
471
504
|
elsif definition.custom? && run.status == "succeeded"
|
|
472
|
-
access.
|
|
505
|
+
model = access.resource.model_class
|
|
506
|
+
locked = model.unscoped.lock.find_by(model.primary_key => run.record_id)
|
|
507
|
+
reject!("NOT_FOUND") unless locked && access.relation.where(model.primary_key => locked.id).exists?
|
|
508
|
+
locked
|
|
473
509
|
else
|
|
474
510
|
access.find!(run.target_id, action: run.operation, lock: true)
|
|
475
511
|
end
|
|
@@ -477,7 +513,7 @@ module RailsAiAdmin
|
|
|
477
513
|
|
|
478
514
|
def coerce!(access, record, definition, input, replay: false)
|
|
479
515
|
fields = replay && definition.custom? ? definition.handler_class.input : definition.fields(access, record)
|
|
480
|
-
result = InputSchema.new(fields, partial: definition.key == "update" && !definition.custom?).coerce(input, time_zone: access.context.time_zone)
|
|
516
|
+
result = InputSchema.new(fields, partial: definition.key == "update" && !definition.custom?).coerce(input, time_zone: access.context.time_zone, replay: replay)
|
|
481
517
|
reject!(replay ? "IDEMPOTENCY_CONFLICT" : "VALIDATION_FAILED", errors: replay ? {} : result.errors) unless result.valid?
|
|
482
518
|
check_references!(access, record, fields, result.values) unless replay
|
|
483
519
|
retain_hidden_references(access, record, fields, result.values)
|
|
@@ -156,8 +156,12 @@ module RailsAiAdmin
|
|
|
156
156
|
return Result.new(nil, "missing", "NOT_FOUND") unless item.is_a?(WorkItem)
|
|
157
157
|
current = WorkItem.find(item.id)
|
|
158
158
|
unknown = current.effect_started_at.present?
|
|
159
|
-
definition =
|
|
160
|
-
|
|
159
|
+
definition = begin
|
|
160
|
+
RailsAiAdmin.work_registry.fetch(item.kind)
|
|
161
|
+
rescue ConfigurationError, NameError
|
|
162
|
+
nil
|
|
163
|
+
end
|
|
164
|
+
terminal = if reconcile || definition.nil? || (unknown && (denied || !definition.handler_class.idempotent))
|
|
161
165
|
"reconciliation_required"
|
|
162
166
|
elsif denied
|
|
163
167
|
"cancelled"
|
|
@@ -59,20 +59,20 @@ module RailsAiAdmin
|
|
|
59
59
|
# Listing children by parent reveals each child's reference value.
|
|
60
60
|
return unless access.fields(:read, view: :index).include?(field)
|
|
61
61
|
model = child.model_class
|
|
62
|
-
records = access.relation.where(field.column => record.id)
|
|
62
|
+
records = Queries::List.unique_records(access.relation.where(field.column => record.id))
|
|
63
63
|
.reorder(model.arel_table[model.primary_key].asc).limit(definition.limit + 1).to_a
|
|
64
|
-
rows = records.first(definition.limit).
|
|
65
|
-
|
|
64
|
+
rows = records.first(definition.limit).filter_map do |child_record|
|
|
65
|
+
begin
|
|
66
66
|
access.authorize!(:show, child_record)
|
|
67
67
|
rescue AccessDenied
|
|
68
|
-
|
|
68
|
+
next
|
|
69
69
|
end
|
|
70
|
-
editable =
|
|
70
|
+
editable = begin
|
|
71
71
|
access.authorize!(:update, child_record)
|
|
72
72
|
rescue AccessDenied
|
|
73
73
|
false
|
|
74
74
|
end
|
|
75
|
-
Row.new(child_record.id.to_s.freeze, access.record_label(child_record, view: :index).freeze,
|
|
75
|
+
Row.new(child_record.id.to_s.freeze, access.record_label(child_record, view: :index).freeze, true, editable)
|
|
76
76
|
end
|
|
77
77
|
filter = access.fields(:filter).include?(field) ? { field.name.to_s => { "eq" => record.id.to_s } }.freeze : nil
|
|
78
78
|
Panel.new(definition.name, definition.caption, child.key, rows.freeze, records.size > definition.limit, filter,
|
|
@@ -28,9 +28,9 @@ module RailsAiAdmin
|
|
|
28
28
|
def page(resource: nil, operation: nil, status: nil, actor: nil, before: nil)
|
|
29
29
|
keys = @resources.map { |entry| entry.key.to_s }
|
|
30
30
|
valid = (resource.nil? || keys.include?(resource)) && (status.nil? || STATUSES.include?(status)) &&
|
|
31
|
-
(operation.nil? || operation.is_a?(String) && operation.match?(/\A[a-z][a-z0-9_]{0,63}\z/)) &&
|
|
32
|
-
(actor.nil? || actor.is_a?(String) && actor.bytesize <= 150 && !actor.match?(/[[:cntrl:]]/)) &&
|
|
33
|
-
(before.nil? || before.is_a?(String) && before.match?(CURSOR))
|
|
31
|
+
(operation.nil? || operation.is_a?(String) && operation.valid_encoding? && operation.match?(/\A[a-z][a-z0-9_]{0,63}\z/)) &&
|
|
32
|
+
(actor.nil? || actor.is_a?(String) && actor.valid_encoding? && actor.bytesize <= 150 && !actor.match?(/[[:cntrl:]]/)) &&
|
|
33
|
+
(before.nil? || before.is_a?(String) && before.valid_encoding? && before.match?(CURSOR) && Integer(before, 10) <= 2**63 - 1)
|
|
34
34
|
raise AccessDenied, "INVALID_QUERY" unless valid
|
|
35
35
|
scope = AuditEvent.where(tenant_ref: @context.tenant_ref, resource_key: resource || keys)
|
|
36
36
|
scope = scope.where(operation: operation) if operation
|
|
@@ -49,6 +49,11 @@ module RailsAiAdmin
|
|
|
49
49
|
Page.new(entries.freeze, events.size > PAGE_SIZE ? entries.last.id : nil)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
+
def actor_present?(actor)
|
|
53
|
+
return false unless actor.is_a?(String) && actor.valid_encoding? && actor.bytesize <= 150 && !actor.match?(/[[:cntrl:]]/)
|
|
54
|
+
AuditEvent.where(tenant_ref: @context.tenant_ref, resource_key: @resources.map { |entry| entry.key.to_s }, actor_ref: actor).exists?
|
|
55
|
+
end
|
|
56
|
+
|
|
52
57
|
private
|
|
53
58
|
|
|
54
59
|
def visible_labels(resource)
|
|
@@ -9,7 +9,7 @@ module RailsAiAdmin
|
|
|
9
9
|
BATCH = 500
|
|
10
10
|
# A spreadsheet runs a cell that starts with these as a formula
|
|
11
11
|
# (CSV injection); such cells get a leading apostrophe.
|
|
12
|
-
FORMULA = /\A[=+\-@\t\r]/
|
|
12
|
+
FORMULA = /\A(?:[=+\-@\t\r\n]|[[:space:]]+[=+\-@])/
|
|
13
13
|
# Number and time text is produced by coercion and cannot be a formula.
|
|
14
14
|
NUMERIC = %i[integer decimal date datetime time].freeze
|
|
15
15
|
Result = Data.define(:body, :rows, :fields, :too_many)
|
|
@@ -26,17 +26,31 @@ module RailsAiAdmin
|
|
|
26
26
|
fields = @access.fields(:read, nil, view: :show)
|
|
27
27
|
references = Fields::Association.new(@context, @resource)
|
|
28
28
|
lines = [ line(fields.map(&:label)) ]
|
|
29
|
-
#
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
# Preload associations for visible columns once per batch instead of per row.
|
|
30
|
+
model = @resource.model_class
|
|
31
|
+
preloads = fields.filter_map do |field|
|
|
32
|
+
case field.scalar_type
|
|
33
|
+
when :references then field.name if model.reflect_on_association(field.name)
|
|
34
|
+
when :file then { "#{field.column}_attachment": :blob } if model.reflect_on_association(:"#{field.column}_attachment")
|
|
35
|
+
when :rich_text then :"rich_text_#{field.column}" if model.reflect_on_association(:"rich_text_#{field.column}")
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
relation = relation.preload(*preloads) if preloads.any?
|
|
32
39
|
rows = 0
|
|
33
40
|
offset = 0
|
|
34
41
|
loop do
|
|
35
|
-
|
|
42
|
+
remaining = @limit - offset
|
|
43
|
+
break if remaining <= 0
|
|
44
|
+
records = relation.offset(offset).limit([ BATCH, remaining ].min).to_a
|
|
36
45
|
break if records.empty?
|
|
37
46
|
offset += records.size
|
|
38
47
|
fields.select { |field| field.scalar_type == :belongs_to }.each do |field|
|
|
39
|
-
|
|
48
|
+
ids = records.filter_map { |record| record.has_attribute?(field.column) ? record.read_attribute(field.column)&.to_s : nil }.uniq
|
|
49
|
+
ids.each_slice(Fields::Association::MAX_BATCH) { |slice| references.prime(field, slice) }
|
|
50
|
+
end
|
|
51
|
+
fields.select { |field| field.scalar_type == :references }.each do |field|
|
|
52
|
+
ids = records.flat_map { |record| Array(record.public_send(field.column)).map(&:to_s) }.uniq
|
|
53
|
+
ids.each_slice(Fields::Association::MAX_BATCH) { |slice| references.prime(field, slice) }
|
|
40
54
|
end
|
|
41
55
|
records.each do |record|
|
|
42
56
|
values = @access.project(record, view: :show)
|
|
@@ -67,14 +81,13 @@ module RailsAiAdmin
|
|
|
67
81
|
when :file then value.filename
|
|
68
82
|
when :files then Array(value).map(&:filename).join("; ")
|
|
69
83
|
when :references
|
|
70
|
-
references.prime(field, Array(value))
|
|
71
84
|
Array(value).map { |id| references.reference(field, id).label.to_s }.join("; ")
|
|
72
85
|
when :belongs_to then references.reference(field, value.to_s).label.to_s
|
|
73
86
|
else field.format(value, time_zone: @context.time_zone)
|
|
74
87
|
end
|
|
75
88
|
end
|
|
76
89
|
text = text.to_s
|
|
77
|
-
!NUMERIC.include?(field.scalar_type) && text.match?(FORMULA) ? "'#{text}" : text
|
|
90
|
+
(field.plugin? || !NUMERIC.include?(field.scalar_type)) && text.match?(FORMULA) ? "'#{text}" : text
|
|
78
91
|
end
|
|
79
92
|
|
|
80
93
|
def line(cells)
|
|
@@ -8,6 +8,7 @@ module RailsAiAdmin
|
|
|
8
8
|
# values without writing anything.
|
|
9
9
|
class CsvImport
|
|
10
10
|
MAX_BYTES = 2 * 1024 * 1024
|
|
11
|
+
MAX_COLUMNS = 200
|
|
11
12
|
# Types a CSV cell can carry. Files, passwords and lists are left out.
|
|
12
13
|
TYPES = %i[text textarea integer decimal boolean date datetime time enum select email url json belongs_to rich_text].freeze
|
|
13
14
|
TRUE_WORDS = %w[true yes y 1].freeze
|
|
@@ -32,6 +33,7 @@ module RailsAiAdmin
|
|
|
32
33
|
end
|
|
33
34
|
|
|
34
35
|
def template
|
|
36
|
+
@access.authorize_import!
|
|
35
37
|
headers = fields.map do |field|
|
|
36
38
|
header = matching_fields(field.label) == [ field ] ? field.label : field.name.to_s
|
|
37
39
|
%("#{header.gsub('"', '""')}")
|
|
@@ -49,10 +51,15 @@ module RailsAiAdmin
|
|
|
49
51
|
mapping = map_columns(header)
|
|
50
52
|
raise Invalid, I18n.t("rails_ai_admin.imports.no_columns") if mapping.empty?
|
|
51
53
|
runner = Operations::Runner.new
|
|
54
|
+
columns_hash = @resource.model_class.columns_hash
|
|
52
55
|
results = rows.each_with_index.map do |cells, index|
|
|
53
56
|
input = mapping.each_with_object({}) do |(position, field), values|
|
|
54
57
|
cell = cells[position].to_s
|
|
55
|
-
|
|
58
|
+
if !cell.strip.empty?
|
|
59
|
+
values[field.name.to_s] = normalize(field, cell)
|
|
60
|
+
elsif !field.options[:nullable] && (column = columns_hash[field.column]) && !column.null && column.default.nil? && !column.default_function
|
|
61
|
+
values[field.name.to_s] = ""
|
|
62
|
+
end
|
|
56
63
|
end
|
|
57
64
|
run_row(runner, index + 2, input, write)
|
|
58
65
|
end
|
|
@@ -138,6 +145,7 @@ module RailsAiAdmin
|
|
|
138
145
|
cell << char if state == :after_quote
|
|
139
146
|
state = :quoted
|
|
140
147
|
when ","
|
|
148
|
+
raise Invalid, I18n.t("rails_ai_admin.imports.too_large") if row.size >= MAX_COLUMNS
|
|
141
149
|
row << cell
|
|
142
150
|
cell = +""
|
|
143
151
|
state = :unquoted
|
|
@@ -94,7 +94,7 @@ module RailsAiAdmin
|
|
|
94
94
|
return unless named
|
|
95
95
|
relation = named.apply(relation)
|
|
96
96
|
end
|
|
97
|
-
total = relation.except(:order).limit(List::COUNT_LIMIT + 1).count
|
|
97
|
+
total = List.unique_records(relation).except(:order).limit(List::COUNT_LIMIT + 1).count
|
|
98
98
|
label = widget.label || (named ? "#{access.resource.label}: #{named.label}" : access.resource.label)
|
|
99
99
|
MetricValue.new(label, [ total, List::COUNT_LIMIT ].min, total > List::COUNT_LIMIT, named&.name&.to_s)
|
|
100
100
|
end
|
|
@@ -106,9 +106,12 @@ module RailsAiAdmin
|
|
|
106
106
|
zone = ActiveSupport::TimeZone[access.context.time_zone]
|
|
107
107
|
today = zone.today
|
|
108
108
|
first = today - (widget.days - 1)
|
|
109
|
+
tomorrow = today + 1
|
|
109
110
|
start = field.scalar_type == :date ? first : zone.local(first.year, first.month, first.day)
|
|
111
|
+
finish = field.scalar_type == :date ? tomorrow : zone.local(tomorrow.year, tomorrow.month, tomorrow.day)
|
|
110
112
|
column = access.resource.model_class.arel_table[field.column]
|
|
111
|
-
values = access.relation.where(column.gteq(start)
|
|
113
|
+
values = List.unique_records(access.relation).where(column.gteq(start).and(column.lt(finish)))
|
|
114
|
+
.reorder(column.desc).limit(TREND_LIMIT + 1).pluck(column)
|
|
112
115
|
counts = Hash.new(0)
|
|
113
116
|
values.first(TREND_LIMIT).compact.each { |value| counts[value.respond_to?(:in_time_zone) && !value.is_a?(Date) ? value.in_time_zone(zone).to_date : value.to_date] += 1 }
|
|
114
117
|
days = (first..today).map { |day| [ day, counts[day] ] }
|
|
@@ -17,7 +17,7 @@ module RailsAiAdmin
|
|
|
17
17
|
|
|
18
18
|
# before: the id of the oldest entry already shown (canonical integer text).
|
|
19
19
|
def page(before: nil)
|
|
20
|
-
unless before.nil? || before.is_a?(String) && before.match?(/\A[1-9][0-9]{0,18}\z/)
|
|
20
|
+
unless before.nil? || before.is_a?(String) && before.valid_encoding? && before.match?(/\A[1-9][0-9]{0,18}\z/) && Integer(before, 10) <= 2**63 - 1
|
|
21
21
|
raise AccessDenied, "INVALID_QUERY"
|
|
22
22
|
end
|
|
23
23
|
scope = AuditEvent.where(tenant_ref: @access.context.tenant_ref, resource_key: @access.resource.key.to_s, record_id: @record.id.to_s)
|
data/lib/rails_ai_admin/prune.rb
CHANGED
|
@@ -27,23 +27,34 @@ module RailsAiAdmin
|
|
|
27
27
|
|
|
28
28
|
def prune_intents
|
|
29
29
|
runs = OperationRun.arel_table
|
|
30
|
-
candidates = OperationRun.where(status: "proposed", parent_run_id: nil,
|
|
30
|
+
candidates = OperationRun.where(status: "proposed", parent_run_id: nil, approved_digest: nil, confirmed_at: nil)
|
|
31
31
|
.where(runs[:expires_at].lt(@cutoff))
|
|
32
32
|
.where.not(id: WorkItem.select(:operation_run_id))
|
|
33
33
|
.where.not(id: BatchItem.select(:operation_run_id))
|
|
34
34
|
.where.not(id: BatchItem.select(:parent_run_id))
|
|
35
35
|
.where.not(id: OperationRun.where.not(parent_run_id: nil).select(:parent_run_id))
|
|
36
|
-
|
|
36
|
+
# Re-assert the safety predicates inside DELETE so a concurrent resume or
|
|
37
|
+
# execution cannot have its committed receipt removed.
|
|
38
|
+
delete_in_batches(candidates) do |ids|
|
|
39
|
+
OperationRun.where(id: ids, status: "proposed", parent_run_id: nil, approved_digest: nil, confirmed_at: nil)
|
|
40
|
+
.where(runs[:expires_at].lt(@cutoff)).delete_all
|
|
41
|
+
end
|
|
37
42
|
end
|
|
38
43
|
|
|
39
44
|
def prune_selections
|
|
40
|
-
|
|
45
|
+
selections = Selection.arel_table
|
|
46
|
+
candidates = Selection.where(selections[:expires_at].lt(@cutoff))
|
|
41
47
|
.where.not(id: OperationRun.where.not(selection_id: nil).select(:selection_id))
|
|
42
48
|
delete_in_batches(candidates) do |ids|
|
|
43
49
|
Selection.transaction do
|
|
44
|
-
|
|
45
|
-
|
|
50
|
+
locked = Selection.lock.where(id: ids).where(selections[:expires_at].lt(@cutoff))
|
|
51
|
+
.where.not(id: OperationRun.where.not(selection_id: nil).select(:selection_id)).pluck(:id)
|
|
52
|
+
next 0 if locked.empty?
|
|
53
|
+
SelectionItem.where(selection_id: locked).delete_all
|
|
54
|
+
Selection.where(id: locked).delete_all
|
|
46
55
|
end
|
|
56
|
+
rescue ActiveRecord::InvalidForeignKey
|
|
57
|
+
0
|
|
47
58
|
end
|
|
48
59
|
end
|
|
49
60
|
|