lib/jsonapi/response_document.rb in jsonapi-resources-0.4.0 vs lib/jsonapi/response_document.rb in jsonapi-resources-0.4.1
- old
+ new
@@ -11,10 +11,13 @@
hash = results_to_hash
meta = top_level_meta
hash.merge!(meta: meta) unless meta.empty?
+ links = top_level_links
+ hash.merge!(links: links) unless links.empty?
+
hash
end
def status
if @operation_results.has_errors?
@@ -35,19 +38,55 @@
key_formatter: @key_formatter,
route_formatter: @options.fetch(:route_formatter, JSONAPI.configuration.route_formatter)
)
end
+ # Rolls up the top level meta data from the base_meta, the set of operations,
+ # and the result of each operation. The keys are then formatted.
def top_level_meta
meta = @options.fetch(:base_meta, {})
meta.merge!(@operation_results.meta)
@operation_results.results.each do |result|
meta.merge!(result.meta)
+
+ if JSONAPI.configuration.top_level_meta_include_record_count
+ meta[JSONAPI.configuration.top_level_meta_record_count_key] = result.record_count
+ end
end
meta.deep_transform_keys { |key| @key_formatter.format(key) }
+ end
+
+ # Rolls up the top level links from the base_links, the set of operations,
+ # and the result of each operation. The keys are then formatted.
+ def top_level_links
+ links = @options.fetch(:base_links, {})
+
+ links.merge!(@operation_results.links)
+
+ @operation_results.results.each do |result|
+ links.merge!(result.links)
+
+ # Build pagination links
+ if result.is_a?(JSONAPI::ResourcesOperationResult)
+ result.pagination_params.each_pair do |link_name, params|
+ query_params = {}
+ query_params[:page] = params
+
+ request = @options[:request]
+ query_params[:fields] = request.params[:fields] if request.params[:fields]
+ query_params[:include] = request.params[:include] if request.params[:include]
+ query_params[:sort] = request.params[:sort] if request.params[:sort]
+ query_params[:filter] = request.params[:filter] if request.params[:filter]
+
+ links[link_name] = serializer.find_link(query_params)
+ end
+ end
+ end
+
+ links.deep_transform_keys { |key| @key_formatter.format(key) }
end
def results_to_hash
if @operation_results.has_errors?
{errors: @operation_results.all_errors}
\ No newline at end of file