Class: JSONAPI::Utils::Exceptions::ActiveRecord

Inherits:
Exceptions::Error
  • Object
show all
Defined in:
lib/jsonapi/utils/exceptions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, resource_klass, context) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



9
10
11
12
13
14
15
16
17
# File 'lib/jsonapi/utils/exceptions.rb', line 9

def initialize(object, resource_klass, context)
  @object = object
  @resource = resource_klass.new(object, context)

  # Need to reflect on resource's relationships for error reporting.
  @relationships     = resource_klass._relationships.values
  @relationship_keys = @relationships.map(&:name).map(&:to_sym)
  @foreign_keys      = @relationships.map(&:foreign_key).map(&:to_sym)
end

Instance Attribute Details

#foreign_keysObject (readonly)

Returns the value of attribute foreign_keys.



7
8
9
# File 'lib/jsonapi/utils/exceptions.rb', line 7

def foreign_keys
  @foreign_keys
end

#objectObject (readonly)

Returns the value of attribute object.



7
8
9
# File 'lib/jsonapi/utils/exceptions.rb', line 7

def object
  @object
end

#relationship_keysObject (readonly)

Returns the value of attribute relationship_keys.



7
8
9
# File 'lib/jsonapi/utils/exceptions.rb', line 7

def relationship_keys
  @relationship_keys
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



7
8
9
# File 'lib/jsonapi/utils/exceptions.rb', line 7

def relationships
  @relationships
end

#resourceObject (readonly)

Returns the value of attribute resource.



7
8
9
# File 'lib/jsonapi/utils/exceptions.rb', line 7

def resource
  @resource
end

Instance Method Details

#errorsObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jsonapi/utils/exceptions.rb', line 19

def errors
  object.errors.messages.flat_map do |key, messages|
    messages.map do |message|
      error_meta = error_base
        .merge(title: title_member(key, message))
        .merge(id: id_member(key))
        .merge(source_member(key))

      JSONAPI::Error.new(error_meta)
    end
  end
end