Module: ForemanRemoteExecution::ErrorsFlattener

Included in:
JobInvocation, TemplateInvocation
Defined in:
app/models/concerns/foreman_remote_execution/errors_flattener.rb

Instance Method Summary collapse

Instance Method Details

#flattened_errorsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/concerns/foreman_remote_execution/errors_flattener.rb', line 7

def flattened_errors
  errors = Hash.new { |h, k| h[k] = [] }
  # self.errors is ActiveModel::Errors, not Hash and doesn't have the #each_key method
  self.errors.attribute_names.each do |key|
    messages = self.errors.messages_for(key)
    invalid_objects = invalid_objects_for_attribute(key)
    if invalid_objects.blank?
      errors[key] = messages
    else
      invalid_objects.each do |invalid_object|
        errors.merge!(sub_object_errors(key, invalid_object, messages))
      end
    end
  end
  errors.map { |key, messages| self.errors.full_message(key, messages.join(', ')) }
end

#flattened_validation_exceptionObject



3
4
5
# File 'app/models/concerns/foreman_remote_execution/errors_flattener.rb', line 3

def flattened_validation_exception
  ActiveRecord::RecordNotSaved.new(I18n.t('activerecord.errors.messages.record_invalid', :errors => flattened_errors.join(', ')))
end