From 5a6202aea2c50e4ffafb63a30827491f5d87ef49 Mon Sep 17 00:00:00 2001 From: Giga Chubinidze Date: Tue, 5 Sep 2023 18:29:15 +0400 Subject: [PATCH 1/2] Unable to create a MarvinJS attachment [SCI-9178] --- .../marvin_js_assets_controller.rb | 42 ++++++++----------- app/serializers/result_serializer.rb | 2 +- app/services/marvin_js_service.rb | 16 ++----- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/app/controllers/marvin_js_assets_controller.rb b/app/controllers/marvin_js_assets_controller.rb index 7e20e5231..e6b065c85 100644 --- a/app/controllers/marvin_js_assets_controller.rb +++ b/app/controllers/marvin_js_assets_controller.rb @@ -15,25 +15,19 @@ class MarvinJsAssetsController < ApplicationController create_create_marvinjs_activity(result[:asset], current_user) - if result[:asset] && marvin_params[:object_type] == 'Step' - render json: { - html: render_to_string(partial: 'assets/asset', locals: { - asset: result[:asset], - gallery_view_id: marvin_params[:object_id] - }) - } - elsif result[:asset] && marvin_params[:object_type] == 'Result' - @my_module = result[:object].my_module - render json: { - html: render_to_string( - partial: 'my_modules/result', - locals: { result: result[:object] } - ) - }, status: :ok - elsif result[:asset] - render json: result[:asset] + if result[:asset] + if marvin_params[:object_type] == 'Step' || marvin_params[:object_type] == 'Result' + render json: { + html: render_to_string(partial: 'assets/asset', locals: { + asset: result[:asset], + gallery_view_id: marvin_params[:object_id] + }) + } + else + render json: result[:asset] + end else - render json: result[:asset].errors, status: :unprocessable_entity + render json: result[:asset]&.errors, status: :unprocessable_entity end end @@ -73,12 +67,12 @@ class MarvinJsAssetsController < ApplicationController def load_create_vars @assoc = Step.find_by(id: marvin_params[:object_id]) if marvin_params[:object_type] == 'Step' - @assoc = MyModule.find_by(id: params[:object_id]) if marvin_params[:object_type] == 'Result' + @assoc = Result.find_by(id: params[:object_id]) if marvin_params[:object_type] == 'Result' if @assoc.class == Step @protocol = @assoc.protocol - elsif @assoc.class == MyModule - @my_module = @assoc + elsif @assoc.class == Result + @my_module = @assoc.my_module end end @@ -86,7 +80,7 @@ class MarvinJsAssetsController < ApplicationController if @assoc.class == Step return render_403 unless can_read_protocol_in_module?(@protocol) || can_read_protocol_in_repository?(@protocol) - elsif @assoc.class == Result || @assoc.class == MyModule + elsif @assoc.class == Result return render_403 unless can_read_experiment?(@my_module.experiment) else render_403 @@ -96,8 +90,8 @@ class MarvinJsAssetsController < ApplicationController def check_edit_permission if @assoc.class == Step return render_403 unless can_manage_step?(@assoc) - elsif @assoc.class == Result || @assoc.class == MyModule - return render_403 unless can_manage_my_module?(@my_module) + elsif @assoc.class == Result + return render_403 unless can_manage_my_module?(@assoc.my_module) else render_403 end diff --git a/app/serializers/result_serializer.rb b/app/serializers/result_serializer.rb index dd8010d04..e8629dacc 100644 --- a/app/serializers/result_serializer.rb +++ b/app/serializers/result_serializer.rb @@ -8,7 +8,7 @@ class ResultSerializer < ActiveModel::Serializer include InputSanitizeHelper attributes :name, :id, :urls, :updated_at, :created_at_formatted, :updated_at_formatted, :user, - :my_module_id, :attachments_manageble, :marvinjs_enabled, :marvinjs_context, + :my_module_id, :attachments_manageble, :marvinjs_enabled, :type, :marvinjs_context, :wopi_enabled, :wopi_context def marvinjs_enabled diff --git a/app/services/marvin_js_service.rb b/app/services/marvin_js_service.rb index 8bc8d890c..f180a6ab2 100644 --- a/app/services/marvin_js_service.rb +++ b/app/services/marvin_js_service.rb @@ -47,20 +47,10 @@ class MarvinJsService private def connect_asset(asset, params, current_user) - if params[:object_type] == 'Step' - object = params[:object_type].constantize.find(params[:object_id]) - asset.update!(view_mode: object.assets_view_mode) - object.assets << asset - elsif params[:object_type] == 'Result' - my_module = MyModule.find_by(id: params[:object_id]) - return unless my_module + object = params[:object_type].constantize.find(params[:object_id]) + asset.update!(view_mode: object.assets_view_mode) + object.assets << asset - object = Result.create(user: current_user, - my_module: my_module, - name: prepare_name(params[:name]), - asset: asset, - last_modified_by: current_user) - end { asset: asset, object: object } end From 44cb0db41a317be38abfcd197bfea54c366c103f Mon Sep 17 00:00:00 2001 From: Giga Chubinidze Date: Wed, 6 Sep 2023 16:36:26 +0400 Subject: [PATCH 2/2] removed object constantization --- app/services/marvin_js_service.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/services/marvin_js_service.rb b/app/services/marvin_js_service.rb index f180a6ab2..965b6f7a6 100644 --- a/app/services/marvin_js_service.rb +++ b/app/services/marvin_js_service.rb @@ -47,7 +47,12 @@ class MarvinJsService private def connect_asset(asset, params, current_user) - object = params[:object_type].constantize.find(params[:object_id]) + object = case params[:object_type] + when 'Step' + Step.find(params[:object_id]) + when 'Result' + Result.find(params[:object_id]) + end asset.update!(view_mode: object.assets_view_mode) object.assets << asset