2018-09-13 16:37:21 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-19 20:44:16 +08:00
|
|
|
# rubocop:disable Metrics/LineLength
|
2018-09-13 16:37:21 +08:00
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class ResultsController < BaseController
|
2018-10-23 23:52:48 +08:00
|
|
|
before_action :load_team
|
|
|
|
before_action :load_project
|
|
|
|
before_action :load_experiment
|
|
|
|
before_action :load_task
|
2018-09-25 00:10:37 +08:00
|
|
|
before_action :load_result, only: %i(show)
|
|
|
|
before_action :check_manage_permissions, only: %i(create)
|
2018-09-13 16:37:21 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
results = @task.results
|
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
|
|
|
render jsonapi: results, each_serializer: ResultSerializer,
|
2018-10-07 18:36:58 +08:00
|
|
|
include: %i(text table file)
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
create_text_result if result_text_params.present?
|
|
|
|
render jsonapi: @result,
|
|
|
|
serializer: ResultSerializer,
|
2018-10-07 18:36:58 +08:00
|
|
|
include: %i(text table file),
|
2018-09-13 16:37:21 +08:00
|
|
|
status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render jsonapi: @result, serializer: ResultSerializer,
|
2018-10-07 18:36:58 +08:00
|
|
|
include: %i(text table file)
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_result
|
|
|
|
@result = @task.results.find(params.require(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
2018-11-07 22:43:44 +08:00
|
|
|
unless can_manage_module?(@task)
|
|
|
|
raise PermissionError.new(MyModule, :manage)
|
|
|
|
end
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_text_result
|
2019-09-04 19:41:06 +08:00
|
|
|
Result.transaction do
|
|
|
|
@result = Result.create!(
|
|
|
|
user: current_user,
|
|
|
|
my_module: @task,
|
|
|
|
name: result_params[:name],
|
|
|
|
last_modified_by: current_user
|
|
|
|
)
|
|
|
|
|
|
|
|
result_text = ResultText.create!(
|
|
|
|
result: @result,
|
|
|
|
text: convert_old_tiny_mce_format(result_text_params[:text])
|
|
|
|
)
|
|
|
|
|
2018-09-13 16:37:21 +08:00
|
|
|
if tiny_mce_asset_params.present?
|
|
|
|
tiny_mce_asset_params.each do |t|
|
|
|
|
image_params = t[:attributes]
|
|
|
|
token = image_params[:file_token]
|
2019-04-02 19:41:32 +08:00
|
|
|
unless result_text.text["data-mce-token=\"#{token}\""]
|
2018-11-07 23:39:00 +08:00
|
|
|
raise ActiveRecord::RecordInvalid,
|
|
|
|
I18n.t('api.core.errors.result_wrong_tinymce.detail')
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
image = Paperclip.io_adapters.for(image_params[:file_data])
|
|
|
|
image.original_filename = image_params[:file_name]
|
2019-04-19 20:44:16 +08:00
|
|
|
tiny_image = TinyMceAsset.create!(
|
2019-03-11 20:43:50 +08:00
|
|
|
image: image,
|
|
|
|
team: @team,
|
|
|
|
object: result_text,
|
|
|
|
saved: true
|
|
|
|
)
|
2019-04-19 20:44:16 +08:00
|
|
|
result_text.text.sub!("data-mce-token=\"#{token}\"", "data-mce-token=\"#{Base62.encode(tiny_image.id)}\"")
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
2019-09-04 19:41:06 +08:00
|
|
|
result_text.save!
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def result_params
|
2018-10-23 23:52:48 +08:00
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'results'
|
2019-03-11 20:43:50 +08:00
|
|
|
|
2018-09-13 16:37:21 +08:00
|
|
|
params.require(:data).require(:attributes).require(:name)
|
2018-09-25 00:10:37 +08:00
|
|
|
params.permit(data: { attributes: :name })[:data][:attributes]
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Partially implement sideposting draft
|
|
|
|
# https://github.com/json-api/json-api/pull/1197
|
|
|
|
def result_text_params
|
2018-09-25 00:10:37 +08:00
|
|
|
prms =
|
|
|
|
params[:included]&.select { |el| el[:type] == 'result_texts' }&.first
|
|
|
|
prms.require(:attributes).require(:text)
|
|
|
|
prms[:attributes]
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def tiny_mce_asset_params
|
|
|
|
prms = params[:included]&.select { |el| el[:type] == 'tiny_mce_assets' }
|
|
|
|
prms.each do |p|
|
|
|
|
p.require(:attributes).require(%i(file_data file_name file_token))
|
|
|
|
end
|
2018-09-25 00:10:37 +08:00
|
|
|
file_tokens = prms.map { |p| p[:attributes][:file_token] }
|
|
|
|
result_text_params[:text].scan(
|
2019-04-02 22:04:50 +08:00
|
|
|
/data-mce-token="(\w+)"/
|
2018-09-25 00:10:37 +08:00
|
|
|
).flatten.each do |token|
|
|
|
|
unless file_tokens.include?(token)
|
2018-11-07 23:39:00 +08:00
|
|
|
raise ActiveRecord::RecordInvalid,
|
|
|
|
I18n.t('api.core.errors.result_missing_tinymce.detail')
|
2018-09-25 00:10:37 +08:00
|
|
|
end
|
|
|
|
end
|
2018-09-13 16:37:21 +08:00
|
|
|
prms
|
|
|
|
end
|
2019-04-19 20:44:16 +08:00
|
|
|
|
|
|
|
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
|
2018-09-13 16:37:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-04-19 20:44:16 +08:00
|
|
|
# rubocop:enable Metrics/LineLength
|