Exception: ViewModel::DeserializationError::Validation

Inherits:
ViewModel::DeserializationError show all
Defined in:
lib/view_model/deserialization_error.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractErrorWithBlame

#nodes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ViewModel::DeserializationError

#code

Methods inherited from AbstractError

#aggregation?, #causes, #code, #exception, #status, #title, #to_s, #view

Constructor Details

#initialize(attribute, reason, details, node) ⇒ Validation

Returns a new instance of Validation.



460
461
462
463
464
465
# File 'lib/view_model/deserialization_error.rb', line 460

def initialize(attribute, reason, details, node)
  @attribute = attribute
  @reason    = reason
  @details   = details
  super([node])
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



458
459
460
# File 'lib/view_model/deserialization_error.rb', line 458

def attribute
  @attribute
end

#detailsObject (readonly)

Returns the value of attribute details.



458
459
460
# File 'lib/view_model/deserialization_error.rb', line 458

def details
  @details
end

#reasonObject (readonly)

Returns the value of attribute reason.



458
459
460
# File 'lib/view_model/deserialization_error.rb', line 458

def reason
  @reason
end

Class Method Details

.from_active_model(errors, node) ⇒ Object

Return Validation errors for each error in the the provided ActiveModel::Errors, wrapped in a Collection if necessary.



477
478
479
480
481
482
483
484
# File 'lib/view_model/deserialization_error.rb', line 477

def self.from_active_model(errors, node)
  causes = errors.messages.each_key.flat_map do |attr|
    errors.messages[attr].zip(errors.details[attr]).map do |message, details|
      self.new(attr.to_s, message, details, node)
    end
  end
  Collection.for_errors(causes)
end

Instance Method Details

#detailObject



467
468
469
# File 'lib/view_model/deserialization_error.rb', line 467

def detail
  "Validation failed: '#{attribute}' #{reason}"
end

#metaObject



471
472
473
# File 'lib/view_model/deserialization_error.rb', line 471

def meta
  super.merge(attribute: attribute, message: reason, details: details)
end