lib/restpack_serializer/serializable/paging.rb in restpack_serializer-0.4.7 vs lib/restpack_serializer/serializable/paging.rb in restpack_serializer-0.4.8
- old
+ new
@@ -26,11 +26,12 @@
end
end
side_load_data = side_loads(page, options)
result[:meta].merge!(side_load_data[:meta] || {})
- result.merge side_load_data.except(:meta)
+ result = result.merge side_load_data.except(:meta)
+ inject_to_many_links result
end
private
def serialize_page(page)
@@ -65,8 +66,35 @@
params << "include=#{options.include.join(',')}" if options.include.any?
params << options.filters_as_url_params if options.filters.any?
url += '?' + params.join('&') if params.any?
url
+ end
+
+ def inject_to_many_links(result) #TODO: GJ: extract this into a result class and refactor
+ keys = result.keys - [:meta, :links]
+
+ keys.each do |key|
+ result[key].each do |item|
+ if item[:links]
+ item[:links].each do |link_key, link_value|
+ unless link_value.is_a? Array
+ plural_linked_key = "#{link_key}s".to_sym
+
+ if result[plural_linked_key]
+ linked_resource = result[plural_linked_key].find { |i| i[:id] == link_value }
+ if linked_resource
+ linked_resource[:links] ||= {}
+ linked_resource[:links][key] ||= []
+ linked_resource[:links][key] << item[:id]
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+
+ result
end
end
end