2019-04-27 04:59:38 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MarvinJsAssetsController < ApplicationController
|
|
|
|
def create
|
2019-04-29 01:11:41 +08:00
|
|
|
new_asset = MarvinJsAsset.add_sketch(marvin_params, current_team)
|
2019-04-28 01:08:40 +08:00
|
|
|
if new_asset.object_type == 'Step'
|
|
|
|
render json: {
|
2019-04-29 01:11:41 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'assets/marvinjs/marvin_sketch_card.html.erb',
|
|
|
|
locals: { sketch: new_asset, i: 0, assets_count: 0, step: new_asset.object }
|
|
|
|
)
|
2019-04-28 01:08:40 +08:00
|
|
|
}
|
2019-04-28 04:54:59 +08:00
|
|
|
elsif new_asset.object_type == 'TinyMceAsset'
|
|
|
|
tiny_img = TinyMceAsset.find(new_asset.object_id)
|
|
|
|
render json: {
|
|
|
|
image: {
|
|
|
|
url: view_context.image_url(tiny_img.url(:large)),
|
2019-04-29 01:11:41 +08:00
|
|
|
token: Base62.encode(tiny_img.id),
|
|
|
|
source_id: new_asset.id,
|
|
|
|
source_type: new_asset.class.name
|
2019-04-28 04:54:59 +08:00
|
|
|
}
|
|
|
|
}, content_type: 'text/html'
|
2019-05-02 22:12:25 +08:00
|
|
|
elsif new_asset
|
2019-04-28 01:08:40 +08:00
|
|
|
render json: new_asset
|
2019-05-02 22:12:25 +08:00
|
|
|
else
|
|
|
|
render json: new_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 22:16:31 +08:00
|
|
|
def show
|
2019-05-02 22:12:25 +08:00
|
|
|
sketch = current_team.marvin_js_assets.find_by_id(params[:id])
|
|
|
|
if sketch
|
|
|
|
if sketch.object_type == 'Step'
|
|
|
|
editable = can_manage_protocol_in_module?(sketch.object.protocol) ||
|
|
|
|
can_manage_protocol_in_repository?(sketch.object.protocol)
|
|
|
|
render json: {
|
|
|
|
sketch: sketch,
|
|
|
|
editable: editable
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render json: sketch
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render json: { error: t('marvinjs.no_sketches_found') }, status: :unprocessable_entity
|
|
|
|
end
|
2019-04-28 22:16:31 +08:00
|
|
|
end
|
|
|
|
|
2019-04-27 19:51:35 +08:00
|
|
|
def destroy
|
2019-05-02 22:12:25 +08:00
|
|
|
sketch = current_team.marvin_js_assets.find_by_id(params[:id])
|
|
|
|
if sketch.destroy
|
|
|
|
render json: sketch
|
|
|
|
else
|
|
|
|
render json: { error: t('marvinjs.no_sketches_found') }, status: :unprocessable_entity
|
|
|
|
end
|
2019-04-27 19:51:35 +08:00
|
|
|
end
|
|
|
|
|
2019-04-28 01:08:40 +08:00
|
|
|
def update
|
2019-05-02 22:12:25 +08:00
|
|
|
sketch = MarvinJsAsset.update_sketch(marvin_params, current_team)
|
|
|
|
if sketch
|
|
|
|
render json: sketch
|
|
|
|
else
|
|
|
|
render json: { error: t('marvinjs.no_sketches_found') }, status: :unprocessable_entity
|
|
|
|
end
|
2019-04-28 01:08:40 +08:00
|
|
|
end
|
|
|
|
|
2019-04-30 23:32:55 +08:00
|
|
|
def team_sketches
|
|
|
|
result = ''
|
|
|
|
sketches = current_team.marvin_js_assets.where.not(object_type: 'TinyMceAsset')
|
|
|
|
sketches.each do |sketch|
|
|
|
|
result += render_to_string(
|
|
|
|
partial: 'shared/marvinjs_modal_sketch.html.erb',
|
|
|
|
locals: { sketch: sketch }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: { html: result, sketches: sketches.pluck(:id) }
|
|
|
|
end
|
|
|
|
|
2019-04-27 04:59:38 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
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
|