Skip to content

Commit

Permalink
support for sparse fieldsets for providers and clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 2, 2019
1 parent f7ceb60 commit e6c4925
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ 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
}.compact
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)

Expand Down
13 changes: 13 additions & 0 deletions app/controllers/concerns/fieldable.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/controllers/concerns/paginatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def page_from_params(params)
page
end
end
end
end
12 changes: 4 additions & 8 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e6c4925

Please sign in to comment.