From 12fdb129196643f8091383d7030c92c143f2de5e Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 23 Jun 2019 19:19:49 +0200 Subject: [PATCH] put loading of dois for events into a concern. datacite/datacite#796 --- Gemfile | 1 + app/models/concerns/batch_loader_helper.rb | 12 ++++++++++++ app/serializers/event_serializer.rb | 4 +++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 app/models/concerns/batch_loader_helper.rb diff --git a/Gemfile b/Gemfile index fc9b3138e..96fe46fbd 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/app/models/concerns/batch_loader_helper.rb b/app/models/concerns/batch_loader_helper.rb new file mode 100644 index 000000000..b7d46a1bb --- /dev/null +++ b/app/models/concerns/batch_loader_helper.rb @@ -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 diff --git a/app/serializers/event_serializer.rb b/app/serializers/event_serializer.rb index ccfc21456..f124c5bca 100644 --- a/app/serializers/event_serializer.rb +++ b/app/serializers/event_serializer.rb @@ -1,5 +1,7 @@ class EventSerializer include FastJsonapi::ObjectSerializer + include BatchLoaderHelper + set_key_transform :camel_lower set_type :events set_id :uuid @@ -7,7 +9,7 @@ class EventSerializer 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