Update Result API for new tiny mce image format (#1674)

This commit is contained in:
aignatov-bio 2019-04-19 14:44:16 +02:00 committed by GitHub
parent f47d0e2932
commit a75c4c2f91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View file

@ -1,5 +1,6 @@
# frozen_string_literal: true
# rubocop:disable Metrics/LineLength
module Api
module V1
class ResultsController < BaseController
@ -44,6 +45,7 @@ module Api
end
def create_text_result
result_text_params[:text] = convert_old_tiny_mce_format(result_text_params[:text])
result_text = ResultText.new(text: result_text_params[:text])
result_text.transaction do
if tiny_mce_asset_params.present?
@ -56,12 +58,13 @@ module Api
end
image = Paperclip.io_adapters.for(image_params[:file_data])
image.original_filename = image_params[:file_name]
TinyMceAsset.create!(
tiny_image = TinyMceAsset.create!(
image: image,
team: @team,
object: result_text,
saved: true
)
result_text.text.sub!("data-mce-token=\"#{token}\"", "data-mce-token=\"#{Base62.encode(tiny_image.id)}\"")
end
end
@result = Result.new(user: current_user,
@ -105,6 +108,16 @@ module Api
end
prms
end
def convert_old_tiny_mce_format(text)
text.scan(/\[~tiny_mce_id:(\w+)\]/).flatten.each do |token|
old_format = /\[~tiny_mce_id:#{token}\]/
new_format = "<img src=\"\" class=\"img-responsive\" data-mce-token=\"#{token}\"/>"
text.sub!(old_format, new_format)
end
text
end
end
end
end
# rubocop:enable Metrics/LineLength

View file

@ -1,8 +1,9 @@
# frozen_string_literal: true
# rubocop:disable Metrics/LineLength
require 'rails_helper'
RSpec.describe "Api::V1::ResultsController", type: :request do
RSpec.describe 'Api::V1::ResultsController', type: :request do
before :all do
@user = create(:user)
@teams = create_list(:team, 2, created_by: @user)
@ -187,6 +188,21 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
include: :text)
.as_json[:included]
)
expect(ResultText.last.text).to include "data-mce-token=\"#{Base62.encode(TinyMceAsset.last.id)}\""
end
it 'Response correct with old TinyMCE images' do
hash_body = nil
@valid_tinymce_hash_body[:included][0][:attributes][:text] = 'Result text 1 [~tiny_mce_id:a1]'
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: @valid_task
), params: @valid_tinymce_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception
expect(ResultText.last.text).to include "data-mce-token=\"#{Base62.encode(TinyMceAsset.last.id)}\""
end
it 'When invalid request, mismatching file token' do
@ -314,3 +330,4 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
end
end
end
# rubocop:enable Metrics/LineLength