2021-06-24 01:48:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BioEddieService
|
|
|
|
class << self
|
|
|
|
def url
|
|
|
|
ApplicationSettings.instance.values['bio_eddie_url']
|
|
|
|
end
|
|
|
|
|
2021-07-20 03:33:28 +08:00
|
|
|
def enabled?(current_user)
|
|
|
|
url.present? && current_user.settings.fetch('bio_eddie_enabled', false)
|
2021-06-24 01:48:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_molecule(params, current_user, current_team)
|
|
|
|
file = image_io(params)
|
|
|
|
|
|
|
|
asset = Asset.new(created_by: current_user,
|
2021-07-15 18:37:04 +08:00
|
|
|
last_modified_by: current_user,
|
|
|
|
team_id: current_team.id)
|
|
|
|
attach_file(asset.file, file, params, current_user)
|
2021-06-24 01:48:44 +08:00
|
|
|
asset.save!
|
|
|
|
asset.post_process_file(current_team)
|
|
|
|
connect_asset(asset, params, current_user)
|
|
|
|
end
|
|
|
|
|
2021-07-15 18:37:04 +08:00
|
|
|
def update_molecule(params, current_user, current_team)
|
2021-06-24 01:48:44 +08:00
|
|
|
asset = current_team.assets.find(params[:id])
|
|
|
|
attachment = asset&.file
|
|
|
|
|
|
|
|
return unless attachment
|
|
|
|
|
|
|
|
file = image_io(params)
|
2021-07-15 18:37:04 +08:00
|
|
|
attach_file(attachment, file, params, current_user)
|
2021-06-24 01:48:44 +08:00
|
|
|
asset
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def connect_asset(asset, params, current_user)
|
|
|
|
case params[:object_type]
|
|
|
|
when 'Step'
|
|
|
|
object = params[:object_type].constantize.find(params[:object_id])
|
|
|
|
asset.update!(view_mode: object.assets_view_mode)
|
|
|
|
object.assets << asset
|
|
|
|
when 'Result'
|
|
|
|
my_module = MyModule.find_by(id: params[:object_id])
|
|
|
|
return unless my_module
|
|
|
|
|
|
|
|
Result.create(user: current_user,
|
|
|
|
my_module: my_module,
|
|
|
|
name: prepare_name(params[:name]),
|
|
|
|
asset: asset,
|
|
|
|
last_modified_by: current_user)
|
|
|
|
end
|
|
|
|
asset
|
|
|
|
end
|
|
|
|
|
|
|
|
def image_io(params)
|
|
|
|
StringIO.new(params[:image])
|
|
|
|
end
|
|
|
|
|
2021-07-22 15:50:58 +08:00
|
|
|
def attach_file(attachment, file, params)
|
2021-06-24 01:48:44 +08:00
|
|
|
attachment.attach(
|
|
|
|
io: file,
|
|
|
|
filename: "#{prepare_name(params[:name])}.svg",
|
|
|
|
content_type: 'image/svg+xml',
|
|
|
|
metadata: {
|
|
|
|
name: prepare_name(params[:name]),
|
|
|
|
description: params[:description],
|
2021-07-15 18:37:04 +08:00
|
|
|
schedule_for_registration: params[:schedule_for_registration] == 'true',
|
2021-06-24 01:48:44 +08:00
|
|
|
asset_type: 'bio_eddie'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_name(sketch_name)
|
|
|
|
sketch_name.presence || I18n.t('bio_eddie.new_molecule')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|