Sha256: 0759cbb3c8e6746085bafc0757a31e23723f6cd0806563fde1a99b278ac2d759

Contents?: true

Size: 1.89 KB

Versions: 21

Compression:

Stored size: 1.89 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.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

21 entries across 21 versions & 1 rubygems

Version Path
foreman_remote_execution-1.3.7 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.3.6 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.4.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.3.5 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.3.4 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.3.3 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.3.2 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.3.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.3.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.2.2 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.2.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.2.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.1.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.1.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-1.0.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-0.3.2 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-0.3.1 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-0.3.0 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-0.2.3 app/models/concerns/foreman_remote_execution/errors_flattener.rb
foreman_remote_execution-0.2.2 app/models/concerns/foreman_remote_execution/errors_flattener.rb