From 91d9f5d6ee18a29426cc486196f02459ecdd0ba9 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 7 Aug 2020 16:51:57 +0200 Subject: [PATCH] Fix rendering of RTE in API [SCI-4910] --- .../api/v1/protocols_controller.rb | 21 +++++++++++-------- app/controllers/api/v1/steps_controller.rb | 6 ++++-- app/controllers/api/v1/tasks_controller.rb | 4 ++-- app/serializers/api/v1/protocol_serializer.rb | 15 +++++++++++++ app/serializers/api/v1/step_serializer.rb | 5 ++++- app/serializers/api/v1/task_serializer.rb | 9 +++++++- config/routes.rb | 2 +- 7 files changed, 46 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/v1/protocols_controller.rb b/app/controllers/api/v1/protocols_controller.rb index 107e1ce61..7ad7a0836 100644 --- a/app/controllers/api/v1/protocols_controller.rb +++ b/app/controllers/api/v1/protocols_controller.rb @@ -3,23 +3,26 @@ module Api module V1 class ProtocolsController < BaseController - before_action :load_team - before_action :load_project - before_action :load_experiment - before_action :load_task + include Api::V1::ExtraParams + + before_action :load_team, :load_project, :load_experiment, :load_task + before_action only: :show do + load_protocol(:id) + end def index protocols = @task.protocols .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) render jsonapi: protocols, - each_serializer: ProtocolSerializer + each_serializer: ProtocolSerializer, rte_rendering: render_rte?, team: @team end - private - - def load_protocol - @protocol = @task.protocols.find(params.require(:id)) + def show + render jsonapi: @protocol, serializer: ProtocolSerializer, + include: include_params, + rte_rendering: render_rte?, + team: @team end end end diff --git a/app/controllers/api/v1/steps_controller.rb b/app/controllers/api/v1/steps_controller.rb index 367ad09e4..f211a984c 100644 --- a/app/controllers/api/v1/steps_controller.rb +++ b/app/controllers/api/v1/steps_controller.rb @@ -16,13 +16,15 @@ module Api render jsonapi: steps, each_serializer: StepSerializer, include: include_params, - rte_rendering: render_rte? + rte_rendering: render_rte?, + team: @team end def show render jsonapi: @step, serializer: StepSerializer, include: include_params, - rte_rendering: render_rte? + rte_rendering: render_rte?, + team: @team end def create diff --git a/app/controllers/api/v1/tasks_controller.rb b/app/controllers/api/v1/tasks_controller.rb index 0e304f7b3..ac4696d82 100644 --- a/app/controllers/api/v1/tasks_controller.rb +++ b/app/controllers/api/v1/tasks_controller.rb @@ -19,11 +19,11 @@ module Api .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: tasks, each_serializer: TaskSerializer, rte_rendering: render_rte? + render jsonapi: tasks, each_serializer: TaskSerializer, rte_rendering: render_rte?, team: @team end def show - render jsonapi: @task, serializer: TaskSerializer, rte_rendering: render_rte? + render jsonapi: @task, serializer: TaskSerializer, rte_rendering: render_rte?, team: @team end def create diff --git a/app/serializers/api/v1/protocol_serializer.rb b/app/serializers/api/v1/protocol_serializer.rb index 176814486..69dbeda20 100644 --- a/app/serializers/api/v1/protocol_serializer.rb +++ b/app/serializers/api/v1/protocol_serializer.rb @@ -3,6 +3,10 @@ module Api module V1 class ProtocolSerializer < ActiveModel::Serializer + include ApplicationHelper + include ActionView::Helpers::TextHelper + include InputSanitizeHelper + type :protocols attributes :id, :name, :authors, :description, :protocol_type has_many :protocol_keywords, @@ -12,6 +16,17 @@ module Api unless: -> { object.protocol_keywords.empty? } has_many :steps, serializer: StepSerializer, if: -> { object.steps.any? } belongs_to :parent, serializer: ProtocolSerializer, if: -> { object.parent.present? } + + def description + if instance_options[:rte_rendering] + custom_auto_link(object.tinymce_render(:description), + simple_format: false, + tags: %w(img), + team: instance_options[:team]) + else + object.description + end + end end end end diff --git a/app/serializers/api/v1/step_serializer.rb b/app/serializers/api/v1/step_serializer.rb index 9ed994531..8bc2fdd8d 100644 --- a/app/serializers/api/v1/step_serializer.rb +++ b/app/serializers/api/v1/step_serializer.rb @@ -18,7 +18,10 @@ module Api def description if instance_options[:rte_rendering] - custom_auto_link(object.tinymce_render(:description), simple_format: false, tags: %w(img)) + custom_auto_link(object.tinymce_render(:description), + simple_format: false, + tags: %w(img), + team: instance_options[:team]) else object.description end diff --git a/app/serializers/api/v1/task_serializer.rb b/app/serializers/api/v1/task_serializer.rb index ebf635928..6064c2a66 100644 --- a/app/serializers/api/v1/task_serializer.rb +++ b/app/serializers/api/v1/task_serializer.rb @@ -3,6 +3,10 @@ module Api module V1 class TaskSerializer < ActiveModel::Serializer + include ApplicationHelper + include ActionView::Helpers::TextHelper + include InputSanitizeHelper + type :tasks attributes :id, :name, :started_on, :due_date, :description, :state, :archived has_many :output_tasks, key: :outputs, @@ -22,7 +26,10 @@ module Api def description if instance_options[:rte_rendering] - custom_auto_link(object.tinymce_render(:description), simple_format: false, tags: %w(img)) + custom_auto_link(object.tinymce_render(:description), + simple_format: false, + tags: %w(img), + team: instance_options[:team]) else object.description end diff --git a/config/routes.rb b/config/routes.rb index 50c39fae5..e2b4c743e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -715,7 +715,7 @@ Rails.application.routes.draw do resources :task_tags, only: %i(index show), path: 'tags', as: :tags - resources :protocols, only: %i(index) do + resources :protocols, only: %i(index show) do resources :steps do resources :assets, only: %i(index show create), path: 'attachments' resources :checklists, path: 'checklists' do