From 239b974fa049eac42bbef8aa2868bc43d16944b3 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 11:29:56 +0200 Subject: [PATCH] Fix GET /tasks/:tid/protocols, GET /tasks/:tid/protocols/:id endpts --- app/controllers/api/v1/protocols_controller.rb | 6 +++--- .../api/v1/protocol_keyword_serializer.rb | 10 ++++++++++ app/serializers/api/v1/protocol_serializer.rb | 14 ++++++++------ config/routes.rb | 4 +--- 4 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 app/serializers/api/v1/protocol_keyword_serializer.rb diff --git a/app/controllers/api/v1/protocols_controller.rb b/app/controllers/api/v1/protocols_controller.rb index 8b8dfbe30..f4cd1a8be 100644 --- a/app/controllers/api/v1/protocols_controller.rb +++ b/app/controllers/api/v1/protocols_controller.rb @@ -10,7 +10,7 @@ module Api before_action :load_protocol, only: :show def index - protocols = @my_module.protocols + protocols = @task.protocols .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) @@ -44,11 +44,11 @@ module Api end def load_task - @my_module = @experiment.my_modules.find(params.require(:task_id)) + @task = @experiment.my_modules.find(params.require(:task_id)) end def load_protocol - @protocol = @my_module.protocols.find( + @protocol = @task.protocols.find( params.require(:id) ) end diff --git a/app/serializers/api/v1/protocol_keyword_serializer.rb b/app/serializers/api/v1/protocol_keyword_serializer.rb new file mode 100644 index 000000000..1f8f838d6 --- /dev/null +++ b/app/serializers/api/v1/protocol_keyword_serializer.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProtocolKeywordSerializer < ActiveModel::Serializer + type :protocol_keywords + attributes :id, :name + end + end +end diff --git a/app/serializers/api/v1/protocol_serializer.rb b/app/serializers/api/v1/protocol_serializer.rb index 9f255b377..5e60d2e7e 100644 --- a/app/serializers/api/v1/protocol_serializer.rb +++ b/app/serializers/api/v1/protocol_serializer.rb @@ -4,12 +4,14 @@ module Api module V1 class ProtocolSerializer < ActiveModel::Serializer type :protocols - attributes :id, :name, :authors, :description, - :team_id, :protocol_type, - :nr_of_linked_children - attribute :my_module_id, key: :task_id - - belongs_to :my_module, serializer: TaskSerializer + attributes :id, :name, :authors, :description, :protocol_type + has_many :protocol_keywords, + key: :keywords, + serializer: ProtocolKeywordSerializer, + class_name: 'ProtocolKeyword', + unless: -> { object.protocol_keywords.empty? } + belongs_to :parent, serializer: ProtocolSerializer, + if: -> { object.parent.present? } end end end diff --git a/config/routes.rb b/config/routes.rb index c0b1bf7bc..497770cfc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -575,9 +575,7 @@ Rails.application.routes.draw do resources :task_tags, only: %i(index show), path: 'tags', as: :tags - resources :protocols, only: %i(index show), - path: 'protocols', - as: :protocols + resources :protocols, only: %i(index show) resources :results, only: %i(index create show), path: 'results', as: :results