Skip to content

Commit

Permalink
include event only in detail views. #390
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 2, 2020
1 parent 6964797 commit 35501d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
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
has_many :citations, record_type: :citations, id_method_name: :uid, serializer: EventSerializer
has_many :parts, record_type: :parts, id_method_name: :uid, serializer: EventSerializer
has_many :versions, record_type: :versions, id_method_name: :uid, serializer: EventSerializer
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] }

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

it "has views" do
get "/dois", nil, headers
get "/dois/#{doi.doi}", nil, headers

expect(last_response.status).to eq(200)
expect(json['data'].size).to eq(1)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 0, 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 0, 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 0, 'attributes', 'viewCount')).to eq(75)
expect(json.dig('data', 0, 'attributes', 'viewsOverTime')).to eq([{"total"=>25, "year_month"=>"2015-06"}, {"total"=>25, "year_month"=>"2015-06"}, {"total"=>25, "year_month"=>"2015-06"}])
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', 'viewCount')).to eq(75)
expect(json.dig('data', 'attributes', 'viewsOverTime')).to eq([{"total"=>25, "year_month"=>"2015-06"}, {"total"=>25, "year_month"=>"2015-06"}, {"total"=>25, "year_month"=>"2015-06"}])
end
end

Expand All @@ -211,16 +209,14 @@
end

it "has downloads" do
get "/dois", nil, headers
get "/dois/#{doi.doi}", nil, headers

expect(last_response.status).to eq(200)
expect(json['data'].size).to eq(1)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 0, 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 0, 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 0, 'attributes', 'downloadCount')).to eq(30)
expect(json.dig('data', 0, 'attributes', 'downloadsOverTime')).to eq([{"total"=>10, "year_month"=>"2015-06"}, {"total"=>10, "year_month"=>"2015-06"}, {"total"=>10, "year_month"=>"2015-06"}])
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', 'downloadCount')).to eq(30)
expect(json.dig('data', 'attributes', 'downloadsOverTime')).to eq([{"total"=>10, "year_month"=>"2015-06"}, {"total"=>10, "year_month"=>"2015-06"}, {"total"=>10, "year_month"=>"2015-06"}])
end
end

Expand All @@ -235,16 +231,14 @@
end

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

expect(last_response.status).to eq(200)
expect(json['data'].size).to eq(1)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 0, 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 0, 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 0, 'attributes', 'referenceCount')).to eq(3)
expect(json.dig('data', 0, 'relationships', 'references')).to eq("data"=>{"id"=>references.first.source_doi, "type"=>"references"})
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"})
end
end

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

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

expect(last_response.status).to eq(200)
expect(json['data'].size).to eq(1)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 0, 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 0, 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 0, 'attributes', 'citationCount')).to eq(1)
expect(json.dig('data', 0, 'relationships', 'citations')).to eq("data"=>{"id"=>citations.first.target_doi, "type"=>"citations"})
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"})
end
end

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

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

expect(last_response.status).to eq(200)
expect(json['data'].size).to eq(1)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 0, 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 0, 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 0, 'attributes', 'partCount')).to eq(3)
expect(json.dig('data', 0, 'relationships', 'parts')).to eq("data"=>{"id"=>parts.first.source_doi, "type"=>"parts"})
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"})
end
end

Expand All @@ -307,16 +297,14 @@
end

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

expect(last_response.status).to eq(200)
expect(json['data'].size).to eq(1)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'url')).to eq(doi.url)
expect(json.dig('data', 0, 'attributes', 'doi')).to eq(doi.doi.downcase)
expect(json.dig('data', 0, 'attributes', 'titles')).to eq(doi.titles)
expect(json.dig('data', 0, 'attributes', 'versionCount')).to eq(3)
expect(json.dig('data', 0, 'relationships', 'versions')).to eq("data"=>{"id"=>versions.first.source_doi, "type"=>"versions"})
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"})
end
end

Expand Down

0 comments on commit 35501d0

Please sign in to comment.