Skip to content

Commit

Permalink
include events in works serializer #crosscite/doi-metadata-search#164
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 20, 2020
1 parent 7947415 commit a2dc62b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion app/serializers/work_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
5 changes: 4 additions & 1 deletion spec/requests/works_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit a2dc62b

Please sign in to comment.