From 66f43803c083220c4b3d90243c68ffc592fbdce6 Mon Sep 17 00:00:00 2001 From: zmagod Date: Mon, 24 Apr 2017 16:22:25 +0200 Subject: [PATCH] refactor, fix user annotation bug --- .../javascripts/sitewide/tiny_mce.js.erb | 4 ---- .../tiny_mce_file_upload_plugin.js.erb | 20 ++++++++++++------- app/controllers/result_texts_controller.rb | 2 +- app/controllers/steps_controller.rb | 3 +-- app/controllers/tiny_mce_assets_controller.rb | 19 +++++++++--------- app/helpers/application_helper.rb | 8 ++++---- app/helpers/input_sanitize_helper.rb | 2 +- app/helpers/tiny_mce_helper.rb | 15 ++++++++++---- app/models/step.rb | 2 +- app/models/tiny_mce_asset.rb | 18 +++++++++-------- config/locales/en.yml | 1 + ...add_attachment_image_to_tiny_mce_assets.rb | 2 -- db/schema.rb | 9 +++------ 13 files changed, 55 insertions(+), 50 deletions(-) diff --git a/app/assets/javascripts/sitewide/tiny_mce.js.erb b/app/assets/javascripts/sitewide/tiny_mce.js.erb index fe11c992f..a0a3d2440 100644 --- a/app/assets/javascripts/sitewide/tiny_mce.js.erb +++ b/app/assets/javascripts/sitewide/tiny_mce.js.erb @@ -13,10 +13,6 @@ var TinyMCE = (function() { }); } - function getTinyMceAssetsUploadPath() { - return $('textarea.tinymce').attr('tiny_mce_upload_path'); - } - // returns a public API for TinyMCE editor return Object.freeze({ init : function() { diff --git a/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb b/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb index f83034106..fb0a1f588 100644 --- a/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb +++ b/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb @@ -30,7 +30,8 @@ {text: "<%= I18n.t 'tiny_mce.insert_btn' %>", onclick: insertImage, subtype: 'primary'}, - {text: "<%= I18n.t 'general.cancel' %>", onclick: ed.windowManager.close} + {text: "<%= I18n.t 'general.cancel' %>", + onclick: ed.windowManager.close} ], }, { plugin_url: url @@ -42,7 +43,10 @@ iframe = win.find('iframe')[0]; form = createElement('form', { action: ed.getParam('customimageuploader_form_url', - '/tinymce_assets'), + '<%= Rails.application + .routes + .url_helpers + .tiny_mce_assets_path %>'), target: iframe._id, method: 'POST', enctype: 'multipart/form-data', @@ -108,7 +112,7 @@ function insertImage() { if(getInputValue('file') == '') { - return handleError("<%= I18n.t 'tiny_mce.error_message'"); + return handleError("<%= I18n.t 'tiny_mce.error_message' %>"); } throbber = new top.tinymce.ui.Throbber(win.getEl()); @@ -142,7 +146,7 @@ var doc = target.contentDocument || target.contentWindow.document; handleResponse(doc.getElementsByTagName("body")[0].innerHTML); } else { - handleError("Didn't get a response from the server"); + handleError("<%= I18n.t 'tiny_mce.server_not_respond' %>"); } } @@ -157,7 +161,9 @@ ed.windowManager.close(); } } catch(e) { - handleError('Got a bad response from the server'); + // hack that get's the server error message + var error_json = JSON.parse($(ret).text()); + handleError(error_json['error'][0]); } } @@ -215,14 +221,14 @@ // Add a button that opens a window editor.addButton('customimageuploader', { - tooltip: ed.translate('Insert an image from your computer'), + tooltip: "<%= I18n.t 'tiny_mce.upload_window_label' %>", icon: 'image', onclick: showDialog }); // Adds a menu item to the tools menu editor.addMenuItem('customimageuploader', { - text: ed.translate('Insert an image from your computer'), + text: "<%= I18n.t 'tiny_mce.upload_window_label' %>", icon: 'image', context: 'insert', onclick: showDialog diff --git a/app/controllers/result_texts_controller.rb b/app/controllers/result_texts_controller.rb index ccc8ffb3c..e6ff26d45 100644 --- a/app/controllers/result_texts_controller.rb +++ b/app/controllers/result_texts_controller.rb @@ -43,7 +43,7 @@ class ResultTextsController < ApplicationController respond_to do |format| if (@result.save and @result_text.save) then - #link tiny_mce_assets to the result text + # link tiny_mce_assets to the result text link_tiny_mce_assets(@result_text.text, @result_text) result_annotation_notification diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index e20e64d63..78b3f103b 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -54,13 +54,12 @@ class StepsController < ApplicationController respond_to do |format| if @step.save - # Post process all assets @step.assets.each do |asset| asset.post_process_file(@protocol.team) end - #link tiny_mce_assets to the step + # link tiny_mce_assets to the step link_tiny_mce_assets(@step.description, @step) create_annotation_notifications(@step) diff --git a/app/controllers/tiny_mce_assets_controller.rb b/app/controllers/tiny_mce_assets_controller.rb index 4c33f5ade..d604f356d 100644 --- a/app/controllers/tiny_mce_assets_controller.rb +++ b/app/controllers/tiny_mce_assets_controller.rb @@ -3,19 +3,18 @@ class TinyMceAssetsController < ApplicationController def create image = params.fetch(:file) { render_404 } - tiny_img = TinyMceAsset.new(image: image, - reference: @obj, - editing: true, - team_id: current_team.id) + tiny_img = TinyMceAsset.new(image: image, reference: @obj) if tiny_img.save render json: { - image: { - url: view_context.image_url(tiny_img.url(:original)), - token: Base62.encode(tiny_img.id) - } + image: { + url: view_context.image_url(tiny_img.url(:large)), + token: Base62.encode(tiny_img.id) + } }, content_type: 'text/html' else - render_404 + render json: { + error: tiny_img.errors.full_messages + }, status: :unprocessable_entity end end @@ -24,7 +23,7 @@ class TinyMceAssetsController < ApplicationController def find_object obj_type = params.fetch(:object_type) { render_404 } obj_id = params.fetch(:object_id) { render_404 } - render_404 unless ['step', 'result_text'].include? obj_type + render_404 unless %w(step result_text).include? obj_type @obj = obj_type.classify.constantize.find_by_id(obj_id) end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c53c5b4e0..bc0fbcc40 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -103,6 +103,7 @@ module ApplicationHelper end def smart_annotation_parser(text, team = nil) + team = nil unless team.kind_of? Team new_text = smart_annotation_filter_resources(text) new_text = smart_annotation_filter_users(new_text, team) new_text @@ -187,10 +188,9 @@ module ApplicationHelper if user && team && UserTeam.user_in_team(user, team).any? - user_t, = user - .user_teams - .where('user_teams.team_id = ?', team) - .first + user_t = user.user_teams + .where('user_teams.team_id = ?', team) + .first end user_description = %(