From e6c49250856cfd3c0c1d0d835ff066f66924c918 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 2 Jun 2019 08:06:57 +0200 Subject: [PATCH] support for sparse fieldsets for providers and clients --- app/controllers/application_controller.rb | 3 +++ app/controllers/clients_controller.rb | 8 ++++++-- app/controllers/concerns/fieldable.rb | 13 +++++++++++++ app/controllers/concerns/paginatable.rb | 2 +- app/controllers/dois_controller.rb | 12 ++++-------- app/controllers/providers_controller.rb | 9 +++++++-- 6 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 app/controllers/concerns/fieldable.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e0deab059..7252fef37 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,6 +14,9 @@ class ApplicationController < ActionController::API # include helper module for pagination include Paginatable + # include helper module for sparse fieldsets + include Fieldable + attr_accessor :current_user # pass ability into serializer diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 444c536cb..4e0cb7c30 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -54,7 +54,6 @@ def index "provider-id" => params[:provider_id], software: params[:software], year: params[:year], - fields: params[:fields], "page[number]" => page[:number] + 1, "page[size]" => page[:size], sort: params[:sort] }.compact.to_query @@ -62,7 +61,12 @@ def index options[:include] = @include options[:is_collection] = true - render json: ClientSerializer.new(@clients, options).serialized_json, status: :ok + fields = fields_from_params(params) + if fields + render json: ClientSerializer.new(@clients, options.merge(fields: fields)).serialized_json, status: :ok + else + render json: ClientSerializer.new(@clients, options).serialized_json, status: :ok + end rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception Raven.capture_exception(exception) diff --git a/app/controllers/concerns/fieldable.rb b/app/controllers/concerns/fieldable.rb new file mode 100644 index 000000000..fa1839ebb --- /dev/null +++ b/app/controllers/concerns/fieldable.rb @@ -0,0 +1,13 @@ +module Fieldable + extend ActiveSupport::Concern + + included do + def fields_from_params(params) + fields = params.to_unsafe_h.dig(:fields) + return nil unless fields.is_a?(Hash) + + fields.each { |k, v| fields[k] = v.split(",") } + fields + end + end +end diff --git a/app/controllers/concerns/paginatable.rb b/app/controllers/concerns/paginatable.rb index 045f70793..1d0192cf5 100644 --- a/app/controllers/concerns/paginatable.rb +++ b/app/controllers/concerns/paginatable.rb @@ -28,4 +28,4 @@ def page_from_params(params) page end end -end \ No newline at end of file +end diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 08eb6691b..1d951edc7 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -190,14 +190,10 @@ def index } bmr = Benchmark.ms { - logger.info params[:fields] - # support sparse fieldsets - if params[:fields] - f = params.to_unsafe_h.dig(:fields) - f = { dois: "doi" } unless f.is_a?(Hash) - f.each { |k, v| f[k] = v.split(",") } - - render json: DoiSerializer.new(results, options.merge(fields: f)).serialized_json, status: :ok + # sparse fieldsets + fields = fields_from_params(params) + if fields + render json: DoiSerializer.new(results, options.merge(fields: fields)).serialized_json, status: :ok else render json: DoiSerializer.new(results, options).serialized_json, status: :ok end diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 230f2f16c..b237e3f40 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -61,7 +61,6 @@ def index "member_type" => params[:member_type], "organization_type" => params[:organization_type], "focus-area" => params[:focus_area], - fields: params[:fields], "page[number]" => page[:number] + 1, "page[size]" => page[:size], sort: sort }.compact.to_query @@ -71,7 +70,13 @@ def index options[:params] = { :current_ability => current_ability, } - render json: ProviderSerializer.new(@providers, options).serialized_json, status: :ok + + fields = fields_from_params(params) + if fields + render json: ProviderSerializer.new(@providers, options.merge(fields: fields)).serialized_json, status: :ok + else + render json: ProviderSerializer.new(@providers, options).serialized_json, status: :ok + end end header = %w( accountName