mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-01 01:14:30 +08:00
Update Result API for new tiny mce image format (#1674)
This commit is contained in:
parent
f47d0e2932
commit
a75c4c2f91
2 changed files with 32 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# rubocop:disable Metrics/LineLength
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class ResultsController < BaseController
|
class ResultsController < BaseController
|
||||||
|
@ -44,6 +45,7 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_text_result
|
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 = ResultText.new(text: result_text_params[:text])
|
||||||
result_text.transaction do
|
result_text.transaction do
|
||||||
if tiny_mce_asset_params.present?
|
if tiny_mce_asset_params.present?
|
||||||
|
@ -56,12 +58,13 @@ module Api
|
||||||
end
|
end
|
||||||
image = Paperclip.io_adapters.for(image_params[:file_data])
|
image = Paperclip.io_adapters.for(image_params[:file_data])
|
||||||
image.original_filename = image_params[:file_name]
|
image.original_filename = image_params[:file_name]
|
||||||
TinyMceAsset.create!(
|
tiny_image = TinyMceAsset.create!(
|
||||||
image: image,
|
image: image,
|
||||||
team: @team,
|
team: @team,
|
||||||
object: result_text,
|
object: result_text,
|
||||||
saved: true
|
saved: true
|
||||||
)
|
)
|
||||||
|
result_text.text.sub!("data-mce-token=\"#{token}\"", "data-mce-token=\"#{Base62.encode(tiny_image.id)}\"")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@result = Result.new(user: current_user,
|
@result = Result.new(user: current_user,
|
||||||
|
@ -105,6 +108,16 @@ module Api
|
||||||
end
|
end
|
||||||
prms
|
prms
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
# rubocop:enable Metrics/LineLength
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# rubocop:disable Metrics/LineLength
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe "Api::V1::ResultsController", type: :request do
|
RSpec.describe 'Api::V1::ResultsController', type: :request do
|
||||||
before :all do
|
before :all do
|
||||||
@user = create(:user)
|
@user = create(:user)
|
||||||
@teams = create_list(:team, 2, created_by: @user)
|
@teams = create_list(:team, 2, created_by: @user)
|
||||||
|
@ -187,6 +188,21 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
|
||||||
include: :text)
|
include: :text)
|
||||||
.as_json[:included]
|
.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
|
end
|
||||||
|
|
||||||
it 'When invalid request, mismatching file token' do
|
it 'When invalid request, mismatching file token' do
|
||||||
|
@ -314,3 +330,4 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
# rubocop:enable Metrics/LineLength
|
||||||
|
|
Loading…
Add table
Reference in a new issue