From a2dc62b3baef5468f9f54441d1c04da7c5040522 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 20 Feb 2020 21:26:03 +0100 Subject: [PATCH] include events in works serializer #crosscite/doi-metadata-search#164 --- app/controllers/works_controller.rb | 4 +++- app/serializers/work_serializer.rb | 4 +++- spec/requests/works_spec.rb | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 2bbc21f3d..19154242d 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -133,7 +133,9 @@ def set_include include_keys = { "data_center" => :client, "member" => :provider, - "resource_type" => :resource_type + "resource_type" => :resource_type, + "reference_events" => :reference_events, + "citation_events" => :citation_events, } @include = params[:include].split(",").reduce([]) do |sum, i| k = include_keys[i.downcase.underscore] diff --git a/app/serializers/work_serializer.rb b/app/serializers/work_serializer.rb index f123c7cd3..7262d8756 100644 --- a/app/serializers/work_serializer.rb +++ b/app/serializers/work_serializer.rb @@ -10,7 +10,9 @@ class WorkSerializer belongs_to :client, key: "data-center", record_type: "data-centers", serializer: :DataCenter belongs_to :provider, key: :member, record_type: :members, serializer: :Member belongs_to :resource_type, record_type: "resource-types", serializer: :ResourceType - + has_many :reference_events, record_type: :events, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] } + has_many :citation_events, record_type: :events, serializer: EventSerializer, if: Proc.new { |object, params| params && params[:detail] } + attribute :author do |object| Array.wrap(object.creators).map do |c| if (c["givenName"].present? || c["familyName"].present?) diff --git a/spec/requests/works_spec.rb b/spec/requests/works_spec.rb index 658fd9a1c..a2d878ce7 100644 --- a/spec/requests/works_spec.rb +++ b/spec/requests/works_spec.rb @@ -41,7 +41,7 @@ end it "has citations" do - get "/works/#{doi.doi}" + get "/works/#{doi.doi}?include=citation-events" puts last_response.body expect(last_response.status).to eq(200) expect(json.dig('data', 'attributes', 'url')).to eq(doi.url) @@ -52,6 +52,9 @@ expect(json.dig('data', 'attributes', 'views-over-time')).to eq([]) expect(json.dig('data', 'attributes', 'download-count')).to eq(0) expect(json.dig('data', 'attributes', 'downloads-over-time')).to eq([]) + expect(json.dig('data', 'relationships', 'citation-events', 'data')).to eq([{"id" => citation_event.uuid, "type"=>"events"}]) + expect(json.dig('included').length).to eq(1) + expect(json.dig('included', 0, 'attributes', 'relationTypeId')).to eq("is-referenced-by") end it "has citations list" do