Skip to content

Commit

Permalink
correctly include events. #388
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 2, 2020
1 parent 35501d0 commit 63add18
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,13 @@ def set_include
@include = params[:include].split(",").map { |i| i.downcase.underscore.to_sym }

if params[:events].present?
@include = @include & [:client, :media, :views, :downloads]
@include = @include & [:client, :media, :references, :citations, :parts, :versions]
else
@include = @include & [:client, :media]
end
else
if params[:events].present?
@include = [:client, :media, :views, :downloads]
@include = [:client, :media, :references, :citations, :parts, :versions]
else
@include = [:client, :media]
end
Expand Down
8 changes: 4 additions & 4 deletions app/serializers/doi_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class DoiSerializer

belongs_to :client, record_type: :clients
has_many :media, record_type: :media, id_method_name: :uid, if: Proc.new { |object, params| params && params[:detail] && !params[:is_collection]}
has_many :references, record_type: :references, id_method_name: :uid, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }
has_many :citations, record_type: :citations, id_method_name: :uid, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }
has_many :parts, record_type: :parts, id_method_name: :uid, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }
has_many :versions, record_type: :versions, id_method_name: :uid, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }
has_many :references, record_type: :references, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }
has_many :citations, record_type: :citations, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }
has_many :parts, record_type: :parts, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }
has_many :versions, record_type: :versions, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] }

attribute :xml, if: Proc.new { |object, params| params && params[:detail] } do |object|
begin
Expand Down
25 changes: 17 additions & 8 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,16 @@
end

it "has references" do
get "/dois/#{doi.doi}", nil, headers
get "/dois/#{doi.doi}?events=true", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 'attributes', 'referenceCount')).to eq(3)
expect(json.dig('data', 'relationships', 'references')).to eq("data"=>{"id"=>references.first.source_doi, "type"=>"references"})
expect(json.dig('data', 'relationships', 'references', 'data').length).to eq(3)
expect(json.dig('included').length).to eq(4)
expect(json.dig('included', 1, 'attributes', 'relationTypeId')).to eq("references")
end
end

Expand All @@ -253,14 +255,16 @@
end

it "has citations" do
get "/dois/#{doi.doi}", nil, headers
get "/dois/#{doi.doi}?events=true", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 'attributes', 'citationCount')).to eq(1)
expect(json.dig('data', 'relationships', 'citations')).to eq("data"=>{"id"=>citations.first.target_doi, "type"=>"citations"})
expect(json.dig('data', 'relationships', 'citations', 'data').length).to eq(1)
expect(json.dig('included').length).to eq(2)
expect(json.dig('included', 0, 'attributes', 'relationTypeId')).to eq("is-referenced-by")
end
end

Expand All @@ -275,14 +279,16 @@
end

it "has parts" do
get "/dois/#{doi.doi}", nil, headers
get "/dois/#{doi.doi}?events=true", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 'attributes', 'partCount')).to eq(3)
expect(json.dig('data', 'relationships', 'parts')).to eq("data"=>{"id"=>parts.first.source_doi, "type"=>"parts"})
expect(json.dig('data', 'relationships', 'parts', 'data').length).to eq(3)
expect(json.dig('included').length).to eq(4)
expect(json.dig('included', 1, 'attributes', 'relationTypeId')).to eq("has-part")
end
end

Expand All @@ -297,14 +303,17 @@
end

it "has versions" do
get "/dois/#{doi.doi}", nil, headers
get "/dois/#{doi.doi}?events=true", nil, headers

puts last_response.body
expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 'attributes', 'versionCount')).to eq(3)
expect(json.dig('data', 'relationships', 'versions')).to eq("data"=>{"id"=>versions.first.source_doi, "type"=>"versions"})
expect(json.dig('data', 'relationships', 'versions', 'data').length).to eq(3)
expect(json.dig('included').length).to eq(4)
expect(json.dig('included', 1, 'attributes', 'relationTypeId')).to eq("has-version")
end
end

Expand Down

0 comments on commit 63add18

Please sign in to comment.