Skip to content

Commit

Permalink
put loading of dois for events into a concern. datacite/datacite#796
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 23, 2019
1 parent 4bee2eb commit 12fdb12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ gem 'git', '~> 1.5'
gem 'graphql', '~> 1.9', '>= 1.9.4'
gem 'graphql-errors', '~> 0.3.0'
gem 'graphql-batch', '~> 0.4.0'
# gem 'batch-loader', '~> 1.4', '>= 1.4.1'
gem 'graphql-cache', '~> 0.6.0', git: "https://github.com/stackshareio/graphql-cache"
# gem 'ddtrace', '~> 0.23.3'

Expand Down
12 changes: 12 additions & 0 deletions app/models/concerns/batch_loader_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module BatchLoaderHelper
extend ActiveSupport::Concern

class_methods do
def load_doi(object)
Doi.find_by_id(object.doi).results
# BatchLoader.for(object.doi).batch do |dois, loader|
# Doi.find_by_id(object.doi).results.each { |doi| loader.call(doi.uid, doi) }
# end
end
end
end
4 changes: 3 additions & 1 deletion app/serializers/event_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
class EventSerializer
include FastJsonapi::ObjectSerializer
include BatchLoaderHelper

set_key_transform :camel_lower
set_type :events
set_id :uuid

attributes :subj_id, :obj_id, :source_id, :relation_type_id, :total, :message_action, :source_token, :license, :occurred_at, :timestamp

has_many :dois, record_type: :dois, serializer: DoiSerializer, id_method_name: :uid do |object|
Doi.find_by_id(object.doi).results
load_doi(object)
end

attribute :timestamp, &:updated_at
Expand Down

0 comments on commit 12fdb12

Please sign in to comment.