Module: JSONAPI::Serialization::IncludePathHelpers

Included in:
IncludesSerialization
Defined in:
lib/json_api/serialization/include_path_helpers.rb

Instance Method Summary collapse

Instance Method Details

#include_paths_to_relationship_names(include_paths, path_prefix) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/json_api/serialization/include_path_helpers.rb', line 17

def include_paths_to_relationship_names(include_paths, path_prefix)
  return [] if include_paths.blank?

  prefix = path_prefix.present? ? "#{path_prefix}." : ""
  include_paths.filter_map do |p|
    path = p.to_s
    next unless path == path_prefix || path.start_with?(prefix)

    segments = path.split(".")
    prefix_segments = path_prefix.split(".")
    next if segments.length <= prefix_segments.length

    segments[prefix_segments.length].to_sym
  end.uniq
end

#normalize_include_paths(include_param) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/json_api/serialization/include_path_helpers.rb', line 6

def normalize_include_paths(include_param)
  case include_param
  when Array
    include_param.flat_map { |s| s.to_s.split(",").map(&:strip) }.uniq
  when String
    include_param.split(",").map(&:strip)
  else
    []
  end
end