Skip to content

Commit

Permalink
Merge pull request #835 from datacite/add_fields_to_pagination
Browse files Browse the repository at this point in the history
Add fields to pagination
  • Loading branch information
digitaldogsbody authored Jul 6, 2022
2 parents 787dde0 + f334847 commit a60441e
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def index
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort],
fields: fields_hash_from_params(params)
}.compact.
to_query
end,
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/concerns/fieldable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ def fields_from_params(params)
fields.each { |k, v| fields[k] = v.to_s.split(",") }
fields
end

def fields_hash_from_params(params)
fields = params.to_unsafe_h.dig(:fields)
return nil unless fields.is_a?(Hash)

fields.each { |k, v| fields[k] = v }
fields
end
end
end
1 change: 1 addition & 0 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def index
page[:number] + 1
end,
"page[size]" => page[:size],
fields: fields_hash_from_params(params)
}.compact.
to_query
end,
Expand Down
1 change: 1 addition & 0 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def index
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: sort,
fields: fields_hash_from_params(params)
}.compact.
to_query
end,
Expand Down
1 change: 1 addition & 0 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def index
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: sort,
fields: fields_hash_from_params(params)
}.compact.
to_query
end,
Expand Down
1 change: 1 addition & 0 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def index
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort],
fields: fields_hash_from_params(params)
}.compact.
to_query
end,
Expand Down
25 changes: 25 additions & 0 deletions spec/concerns/fieldable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "rails_helper"

describe "Dois", type: :controller do
subject { DataciteDoisController.new }

it "no params" do
params = ActionController::Parameters.new
expect(subject.fields_from_params(params)).to be_nil
expect(subject.fields_hash_from_params(params)).to be_nil
end

it "single value" do
params = ActionController::Parameters.new(fields: { dois: "id" })
expect(subject.fields_from_params(params)).to eq({ dois: ["id"] }.with_indifferent_access)
expect(subject.fields_hash_from_params(params)).to eq({ dois: "id" }.with_indifferent_access)
end

it "multiple values" do
params = ActionController::Parameters.new(fields: { dois: "id,subjects" })
expect(subject.fields_from_params(params)).to eq({ dois: ["id", "subjects"] }.with_indifferent_access)
expect(subject.fields_hash_from_params(params)).to eq({ dois: "id,subjects" }.with_indifferent_access)
end
end
47 changes: 47 additions & 0 deletions spec/requests/datacite_dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,53 @@
expect(doi.dig("relationships", "provider", "data", "id")).to eq(provider.symbol.downcase)
end
end

it "applies field filters for a single filter" do
get "/dois?fields[dois]=id", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(10)
expect(json.dig("meta", "total")).to eq(10)
json["data"].each do |doi|
expect(doi).to include("id")
expect(doi).to include("attributes")
expect(doi).to include("relationships")
expect(doi.dig("attributes")).to eq({})
expect(doi.dig("relationships")).to eq({})
end
end

it "applies field filters for multiple filters" do
get "/dois?fields[dois]=id,subjects", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(10)
expect(json.dig("meta", "total")).to eq(10)
json["data"].each do |doi|
expect(doi).to include("id")
expect(doi).to include("attributes")
expect(doi).to include("relationships")
expect(doi.dig("attributes")).to have_key("subjects")
expect(doi.dig("attributes")).to_not have_key("creators")
expect(doi.dig("relationships")).to eq({})
end
end

it "preserves field filters in pagination links" do
get "/dois?fields[dois]=id&page[size]=2&page[number]=1", nil, headers

expect(last_response.status).to eq(200)
next_link_absolute = Addressable::URI.parse(json.dig("links", "next"))
next_link = next_link_absolute.path + "?" + next_link_absolute.query
expect(next_link).to eq("/dois?fields%5Bdois%5D=id&page%5Bnumber%5D=2&page%5Bsize%5D=2")

get "/dois?fields[dois]=id,subjects&page[size]=2&page[number]=1", nil, headers

expect(last_response.status).to eq(200)
next_link_absolute = Addressable::URI.parse(json.dig("links", "next"))
next_link = next_link_absolute.path + "?" + next_link_absolute.query
expect(next_link).to eq("/dois?fields%5Bdois%5D=id%2Csubjects&page%5Bnumber%5D=2&page%5Bsize%5D=2")
end
end

describe "GET /dois with query", elasticsearch: true do
Expand Down

0 comments on commit a60441e

Please sign in to comment.