scinote-web/app/controllers/marvin_js_assets_controller.rb

92 lines
2.9 KiB
Ruby
Raw Normal View History

2019-04-27 04:59:38 +08:00
# frozen_string_literal: true
class MarvinJsAssetsController < ApplicationController
2019-07-16 19:40:54 +08:00
before_action :load_vars
before_action :check_read_permission
before_action :check_edit_permission, only: %i(update create)
2019-04-27 04:59:38 +08:00
def create
result = MarvinJsService.create_sketch(marvin_params, current_user)
if result[:asset] && marvin_params[:object_type] == 'Step'
2019-04-28 01:08:40 +08:00
render json: {
2019-04-29 01:11:41 +08:00
html: render_to_string(
2019-07-02 19:15:57 +08:00
partial: 'steps/attachments/item.html.erb',
locals: { asset: result[:asset],
i: 0,
assets_count: 0,
step: result[:object],
order_atoz: 0,
order_ztoa: 0 }
2019-04-29 01:11:41 +08:00
)
2019-04-28 01:08:40 +08:00
}
elsif result[:asset] && marvin_params[:object_type] == 'Result'
@my_module = result[:object].my_module
render json: {
html: render_to_string(
partial: 'my_modules/result.html.erb',
locals: { result: result[:object] }
)
}, status: :ok
elsif result[:asset]
render json: result[:asset]
2019-05-02 22:12:25 +08:00
else
render json: result[:asset].errors, status: :unprocessable_entity
2019-04-28 01:08:40 +08:00
end
2019-04-27 04:59:38 +08:00
end
2019-04-28 01:08:40 +08:00
def update
2019-07-02 19:15:57 +08:00
asset = MarvinJsService.update_sketch(marvin_params, current_user)
if asset
2019-07-16 19:40:54 +08:00
render json: { url: rails_representation_url(asset.medium_preview), id: asset.id, file_name: asset.file_name }
2019-05-02 22:12:25 +08:00
else
render json: { error: t('marvinjs.no_sketches_found') }, status: :unprocessable_entity
end
2019-04-28 01:08:40 +08:00
end
2019-04-27 04:59:38 +08:00
private
2019-07-16 19:40:54 +08:00
def load_vars
@asset = current_team.assets.find_by_id(params[:id])
2019-07-16 19:40:54 +08:00
if action_name == 'create'
@assoc ||= Step.find_by_id(marvin_params[:object_id]) if marvin_params[:object_type] == 'Step'
@assoc ||= MyModule.find_by_id(params[:object_id]) if marvin_params[:object_type] == 'Result'
else
return render_404 unless @asset
@assoc ||= @asset.step
@assoc ||= @asset.result
2019-07-16 19:40:54 +08:00
end
if @assoc.class == Step
@protocol = @assoc.protocol
elsif @assoc.class == MyModule
@my_module = @assoc
elsif @assoc.class == Result
@my_module = @assoc.my_module
end
end
def check_read_permission
if @assoc.class == Step
2019-07-16 19:40:54 +08:00
render_403 && return unless can_read_protocol_in_module?(@protocol) ||
can_read_protocol_in_repository?(@protocol)
elsif @assoc.class == Result || @assoc.class == MyModule
render_403 and return unless can_read_experiment?(@my_module.experiment)
end
end
def check_edit_permission
if @assoc.class == Step
2019-07-16 19:40:54 +08:00
render_403 && return unless can_manage_protocol_in_module?(@protocol) ||
can_manage_protocol_in_repository?(@protocol)
elsif @assoc.class == Result || @assoc.class == MyModule
render_403 and return unless can_manage_module?(@my_module)
end
end
2019-04-27 04:59:38 +08:00
def marvin_params
2019-04-29 01:11:41 +08:00
params.permit(:id, :description, :object_id, :object_type, :name, :image)
2019-04-27 04:59:38 +08:00
end
end