Class: AbcJsonapi::IncludedResource

Inherits:
Object
  • Object
show all
Defined in:
lib/abc_jsonapi/included_resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, includes:, serializer_namespace:) ⇒ IncludedResource

Returns a new instance of IncludedResource.



5
6
7
8
9
10
# File 'lib/abc_jsonapi/included_resource.rb', line 5

def initialize(resource:, includes:, serializer_namespace:)
  @includes_result = []
  @resource = resource
  @includes = includes
  @serializer_namespace = serializer_namespace
end

Instance Attribute Details

#includesObject (readonly)

Returns the value of attribute includes.



3
4
5
# File 'lib/abc_jsonapi/included_resource.rb', line 3

def includes
  @includes
end

#includes_resultObject (readonly)

Returns the value of attribute includes_result.



3
4
5
# File 'lib/abc_jsonapi/included_resource.rb', line 3

def includes_result
  @includes_result
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/abc_jsonapi/included_resource.rb', line 3

def resource
  @resource
end

#serializer_namespaceObject (readonly)

Returns the value of attribute serializer_namespace.



3
4
5
# File 'lib/abc_jsonapi/included_resource.rb', line 3

def serializer_namespace
  @serializer_namespace
end

Instance Method Details

#get_included_records(resource, include_chain) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/abc_jsonapi/included_resource.rb', line 20

def get_included_records(resource, include_chain)
  return if resource.nil? || include_chain.empty?

  # Get first include name of include_chain and delete it from include_chain
  inc_resource_name = include_chain.shift

  # Check if include name is exist in relationships array. Take it or return nil
  resource_class_name = resource.is_a?(Enumerable) ? resource[0].class.name : resource.class.name
  relationship = serializer(resource_class_name).relationships.find { |h| h[:name] == inc_resource_name.to_sym }
  return if relationship.nil?

  # Get included resource
  if resource.is_a?(Enumerable)
    resource = collection.map{ |res| res.public_send(inc_resource_name) }.flatten.reject(&:nil?).uniq{ |item| item.id }
  else
    resource = resource.public_send(inc_resource_name)
  end
  return if resource.nil?
  
  @includes_result << serializer(inc_resource_name).new(resource).serializable_hash[:data]
  
  # If resource is a collection call get_included_records for each. Otherwise send whole resource
  if resource.is_a?(Enumerable)
    resource.each do |single_res|
      get_included_records(single_res, include_chain.dup)
    end
  else
    get_included_records(resource, include_chain)
  end
end

#serializable_hashObject



12
13
14
15
16
17
18
# File 'lib/abc_jsonapi/included_resource.rb', line 12

def serializable_hash
  includes.each do |include_path|
    include_chain = include_path.split('.')
    get_included_records(resource, include_chain.dup)
  end
  includes_result.flatten
end

#serializer(included_resource_name) ⇒ Object



51
52
53
# File 'lib/abc_jsonapi/included_resource.rb', line 51

def serializer(included_resource_name)
  (serializer_namespace + included_resource_name.to_s.classify + 'Serializer').constantize
end