Module: JSONAPIonify::Api::Resource::Includer

Extended by:
ActiveSupport::Concern
Includes:
Structure
Included in:
JSONAPIonify::Api::Resource
Defined in:
lib/jsonapionify/api/resource/includer.rb

Constant Summary

Constants included from Structure

Structure::ValidationError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.includes_to_hashes(path) ⇒ Object



59
60
61
62
63
64
# File 'lib/jsonapionify/api/resource/includer.rb', line 59

def self.includes_to_hashes(path)
  path.to_s.split(',').each_with_object({}) do |p, obj|
    rel, *sub_path = p.split('.')
    obj[rel]       = includes_to_hashes(sub_path.join('.'))
  end
end

Instance Method Details

#append_included(context, included) ⇒ Object



33
34
35
36
37
# File 'lib/jsonapionify/api/resource/includer.rb', line 33

def append_included(context, included)
  if included.present?
    context.response_object[:included] |= included
  end
end

#fetch_included(context, **overrides) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jsonapionify/api/resource/includer.rb', line 39

def fetch_included(context, **overrides)
  context.includes.reduce(Collections::IncludedResources.new) do |lv, (name, _)|
    if self.class.include_definitions.keys.include?(name.to_sym)
      response_object = JSONAPIonify.new_object
      res = self.class.relationship(name)
      overrides = overrides.merge includes:         context.includes[name],
                                  errors:           context.errors,
                                  root_request?:    false,
                                  action_name:      context.action_name,
                                  response_object:  response_object
      action_name = res.rel.is_a?(Relationship::One) ? :read : :list
      res.call_action(action_name, context.request, context_overrides: overrides)
      lv | (Array.wrap(response_object[:data]).compact + response_object[:included])
    else
      error :relationship_not_includable, res.rel.name
      lv
    end
  end
end