2020-02-25 20:46:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CreateMyModuleService
|
|
|
|
def initialize(user, team, params)
|
|
|
|
@params = params
|
|
|
|
@my_module_params = params[:my_module] || {}
|
|
|
|
@user = user
|
|
|
|
@team = team
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2020-03-10 20:16:56 +08:00
|
|
|
new_my_module = nil
|
2020-02-25 20:46:44 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-02-25 20:49:35 +08:00
|
|
|
unless @params[:experiment].class == Experiment
|
|
|
|
@params[:experiment][:project] = @params[:project]
|
|
|
|
@params[:experiment] = CreateExperimentService.new(@user, @team, @params[:experiment]).call
|
|
|
|
end
|
2020-03-10 20:16:56 +08:00
|
|
|
unless @params[:experiment]&.errors&.empty?
|
|
|
|
new_my_module = @params[:experiment]
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
2020-02-25 20:46:44 +08:00
|
|
|
|
|
|
|
@my_module_params[:x] ||= 0
|
|
|
|
@my_module_params[:y] ||= 0
|
|
|
|
@my_module_params[:name] ||= I18n.t('create_task_service.default_task_name')
|
|
|
|
|
2020-02-25 20:49:35 +08:00
|
|
|
@my_module = @params[:experiment].my_modules.new(@my_module_params)
|
2020-02-25 20:46:44 +08:00
|
|
|
|
|
|
|
new_pos = @my_module.get_new_position
|
|
|
|
@my_module.x = new_pos[:x]
|
|
|
|
@my_module.y = new_pos[:y]
|
2020-05-20 14:59:07 +08:00
|
|
|
@my_module.created_by = @user
|
|
|
|
@my_module.last_modified_by = @user
|
2020-02-25 20:46:44 +08:00
|
|
|
|
|
|
|
@my_module.save!
|
|
|
|
create_my_module_activity
|
2020-03-30 21:16:14 +08:00
|
|
|
|
|
|
|
@my_module.assign_user(@user)
|
|
|
|
|
2020-02-25 20:51:54 +08:00
|
|
|
@params[:experiment].generate_workflow_img
|
2020-03-10 20:16:56 +08:00
|
|
|
new_my_module = @my_module
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
2020-03-10 20:16:56 +08:00
|
|
|
new_my_module
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_my_module_activity
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :create_module,
|
|
|
|
owner: @user,
|
|
|
|
team: @team,
|
2020-02-25 20:49:35 +08:00
|
|
|
project: @params[:experiment].project,
|
2020-02-25 20:46:44 +08:00
|
|
|
subject: @my_module,
|
|
|
|
message_items: { my_module: @my_module.id })
|
|
|
|
end
|
|
|
|
end
|