mirror of
https://github.com/zammad/zammad
synced 2026-05-24 09:48:36 +00:00
Co-authored-by: Dominik Klein <dk@zammad.com> Co-authored-by: Florian Liebe <fl@zammad.com>
67 lines
1.7 KiB
Ruby
67 lines
1.7 KiB
Ruby
# Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
|
|
|
|
class Validations::ObjectManager::AttributeValidator < ActiveModel::Validator
|
|
include ::Mixin::HasBackends
|
|
|
|
def validate(record)
|
|
return if !validation_needed?
|
|
|
|
@record = record
|
|
sanitize_memory_cache
|
|
|
|
return if attributes_unchanged?
|
|
|
|
model_attributes.select(&:editable).each do |attribute|
|
|
perform_validation(attribute)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :record
|
|
|
|
def validation_needed?
|
|
return false if Setting.get('import_mode')
|
|
|
|
return false if ApplicationHandleInfo.context_without_custom_attributes?
|
|
|
|
%w[application_server ai_agent_execution].include?(ApplicationHandleInfo.current)
|
|
end
|
|
|
|
def attributes_unchanged?
|
|
model_attributes.none? do |attribute|
|
|
record.will_save_change_to_attribute?(attribute.name)
|
|
end
|
|
end
|
|
|
|
def model_attributes
|
|
@model_attributes ||= begin
|
|
object_lookup_id = ObjectLookup.by_name(record.class.name)
|
|
@active_attributes.select { |attribute| attribute.object_lookup_id == object_lookup_id }
|
|
end
|
|
end
|
|
|
|
def perform_validation(attribute)
|
|
backends.each do |backend|
|
|
backend.validate(
|
|
record: record,
|
|
attribute: attribute
|
|
)
|
|
end
|
|
end
|
|
|
|
def sanitize_memory_cache
|
|
@model_attributes = nil
|
|
|
|
active_attributes = active_attributes_in_db
|
|
latest_cache_key = active_attributes.cache_key + active_attributes.cache_version
|
|
return if @previous_cache_key == latest_cache_key
|
|
|
|
@previous_cache_key = latest_cache_key
|
|
@active_attributes = active_attributes_in_db.to_a
|
|
end
|
|
|
|
def active_attributes_in_db
|
|
ObjectManager::Attribute.where(active: true)
|
|
end
|
|
end
|