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.



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

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.



475
476
477
# File 'lib/view_model/deserialization_error.rb', line 475

def attribute
  @attribute
end

#detailsObject (readonly)

Returns the value of attribute details.



475
476
477
# File 'lib/view_model/deserialization_error.rb', line 475

def details
  @details
end

#reasonObject (readonly)

Returns the value of attribute reason.



475
476
477
# File 'lib/view_model/deserialization_error.rb', line 475

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.



494
495
496
497
498
499
500
501
# File 'lib/view_model/deserialization_error.rb', line 494

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



484
485
486
# File 'lib/view_model/deserialization_error.rb', line 484

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

#metaObject



488
489
490
# File 'lib/view_model/deserialization_error.rb', line 488

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