From d60cc2cd1ca84e8fb5192c1d845e89b4682608dc Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Thu, 21 Mar 2019 08:47:52 +0100 Subject: [PATCH] Refactor create_wopi_file --- .../assets/wopi/create_wopi_file.js | 9 +--- app/controllers/assets_controller.rb | 44 +++++-------------- 2 files changed, 12 insertions(+), 41 deletions(-) diff --git a/app/assets/javascripts/assets/wopi/create_wopi_file.js b/app/assets/javascripts/assets/wopi/create_wopi_file.js index 62555124d..14c6fca73 100644 --- a/app/assets/javascripts/assets/wopi/create_wopi_file.js +++ b/app/assets/javascripts/assets/wopi/create_wopi_file.js @@ -42,16 +42,11 @@ function initCreateWopiFileModal() { }); } -// I couldn't make this work through CSS function applyImageChangeOnButtons() { - var resetToInActive = function(modal) { - modal.find('img.act').hide(); - modal.find('img.inactive').show(); - }; - var modal = $('#new-office-file-modal'); modal.find('.btn-group label').off().click(function() { - resetToInActive(modal); + modal.find('img.act').hide(); + modal.find('img.inactive').show(); $(this).find('img.act').show(); $(this).find('img.inactive').hide(); diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index 89d933b4c..fe96f274a 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -176,18 +176,7 @@ class AssetsController < ApplicationController # POST: create_wopi_file_path def create_wopi_file # Presence validation - create_wopi_params - - unless params[:file_name].present? - respond_to do |format| - format.json do - render json: { - error: true, - message: { file: [t('general.text.not_blank')] } - }, status: 400 - end - end and return - end + params.require(%i(element_type element_id file_type)) # File type validation render_403 && return unless %w(docx xlsx pptx).include?(params[:file_type]) @@ -198,15 +187,10 @@ class AssetsController < ApplicationController file.content_type = wopi_content_type(params[:file_type]) asset = Asset.new(file: file, created_by: current_user, file_present: true) - unless asset.valid? - respond_to do |format| - format.json do - render json: { - error: true, - message: asset.errors - }, status: 400 - end - end and return + unless asset.valid?(:wopi_file_creation) + render json: { + message: asset.errors + }, status: 400 and return end # Create file depending on the type @@ -229,18 +213,14 @@ class AssetsController < ApplicationController edit_url = edit_asset_url(result_asset.asset_id) else - render_404 + render_404 and return end # Return edit url - respond_to do |format| - format.json do - render json: { - success: true, - edit_url: edit_url - } - end - end + render json: { + success: true, + edit_url: edit_url + }, status: :ok end private @@ -296,10 +276,6 @@ class AssetsController < ApplicationController url + wd_params end - def create_wopi_params - params.require(%i(element_type element_id file_type)) - end - def asset_params params.permit( :file