Sha256: 1aa2db19b9a489e12736499fa813b5a6d1ed0846ccf51af62fa5063b4d459a1d

Contents?: true

Size: 1.98 KB

Versions: 95

Compression:

Stored size: 1.98 KB

Contents

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

    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.keys.each do |key|
        messages = self.errors[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

    private

    def invalid_objects_for_attribute(attribute)
      if self.respond_to?(attribute)
        invalid_object = self.public_send(attribute)
        if invalid_object.respond_to? :each_with_index
          invalid_object.select { |o| o.respond_to?(:invalid?) && o.invalid? }
        else
          [invalid_object]
        end
      end
    end

    def flattened_error_key(key, object)
      mapping = if defined? self.class::FLATTENED_ERRORS_MAPPING
                  self.class::FLATTENED_ERRORS_MAPPING
                else
                  {}
                end
      mapped_key = mapping.fetch(key, key)
      if mapped_key.is_a? Proc
        mapped_key.call(object)
      else
        mapped_key
      end
    end

    def sub_object_errors(key, object, original_message)
      key = flattened_error_key(key, object)
      errors = if object.respond_to? :flattened_errors
                 object.flattened_errors
               elsif object.respond_to? :errors
                 object.errors.full_messages
               else
                 original_message
               end
      return { "#{key}:" => errors }
    end
  end
end

Version data entries

95 entries across 95 versions & 1 rubygems

Version Path
foreman_remote_execution-14.0.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.2.6 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-14.0.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.2.5 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.2.4 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.2.3 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.2.2 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-12.0.7 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.2.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.2.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-10.1.3 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-13.0.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-12.0.5 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-12.0.4 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-12.0.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-12.0.2 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-11.1.3 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-10.1.2 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-12.0.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-10.1.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb