Sha256: ac49dd24e7a3a01ce90bc0564312036e699172485f93fabefc8d008cff4ed201

Contents?: true

Size: 1.79 KB

Versions: 43

Compression:

Stored size: 1.79 KB

Contents

# We need to know two things in the response of a persistence call:
#
#   * The model we just tried to persist
#   * Was the persistence successful?
#
# This object wraps those bits of data. The call is considered
# unsuccessful when it adheres to the ActiveModel#errors interface,
# and #errors is not blank. In other words, it is not successful if
# there were validation errors.
#
# @attr_reader object the object we are saving
class JsonapiCompliable::Util::ValidationResponse
  attr_reader :object

  # @param object the model instance we tried to save
  # @param deserialized_params see Base#deserialized_params
  def initialize(object, deserialized_params)
    @object = object
    @deserialized_params = deserialized_params
  end

  # Check to ensure no validation errors.
  # @return [Boolean] did the persistence call succeed?
  def success?
    all_valid?(object, @deserialized_params.relationships)
  end

  # @return [Array] the object and success state
  def to_a
    [object, success?]
  end

  private

  def valid_object?(object)
    !object.respond_to?(:errors) ||
      (object.respond_to?(:errors) && object.errors.blank?)
  end

  def all_valid?(model, deserialized_params)
    valid = true
    return false unless valid_object?(model)
    deserialized_params.each_pair do |name, payload|
      if payload.is_a?(Array)
        related_objects = model.send(name)
        related_objects.each do |r|
          valid = valid_object?(r)

          if valid
            valid = all_valid?(r, deserialized_params[:relationships] || {})
          end
        end
      else
        related_object = model.send(name)
        valid = valid_object?(related_object)
        if valid
          valid = all_valid?(related_object, deserialized_params[:relationships] || {})
        end
      end
    end
    valid
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
jsonapi_compliable-0.11.22 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.21 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.20 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.19 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.18 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.17 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.16 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.15 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.14 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.13 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.12 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.11 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.10 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.8 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.7 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.6 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.5 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.4 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.3 lib/jsonapi_compliable/util/validation_response.rb
jsonapi_compliable-0.11.2 lib/jsonapi_compliable/util/validation_response.rb